简体   繁体   中英

WP8 WebBrowser as Panorama/Pivot Item

I added a WebBrowser as the content of one of the panorama items. The WebBrowser gets rendered with no issues. If I swipe the panorama by touching the area outside the WebBrowser the swipe happens. But when I try to swipe the panorama by touching the WebBrowser, the swipe does not happen, instead the WebBrowser scrolls vertically. Any idea how this can be fixed ?

I didn't downvote, but probably, because its a bad idea. By design, those items should not be combined. However, if you really want to keep browser inside pivot, you can take a glance here

While you'll undoubtedly come to find that this is not recommended by the UI guidelines, this was a necessary requirement in my case and I was able to work around this by subscribing directly to the Touch events and manually detecting a swipe:

    // controls "swipe" behavior
    private Point touchDownPosition;  // last position of touch down
    private int touchDownTime;        // last time of touch down
    private int touchUpTime;          // last time of touch up
    private int swipeMaxTime = 1000;  // time (in milliseconds) that a swipe must occur in
    private int swipeMinDistance = 25;// distance (in pixels) that a swipe must cover
    private int swipeMinBounceTime = 500; // time (in milliseconds) between multiple touch events (minimizes "bounce")

    // handler for touch events
    void Touch_FrameReported(object sender, TouchFrameEventArgs e) 
    {
        var item = MyPivot.SelectedItem as PivotItem;

        // ignore touch if we are not on the browser pivot item
        if (item != BrowserPivotItem) 
            return;

        var point = e.GetPrimaryTouchPoint(item);
        switch (point.Action) 
        {
            case TouchAction.Down:
                touchDownTime = e.Timestamp;
                touchDownPosition = point.Position;
                touchUpTime = 0;
                break;

            case TouchAction.Up:
                // often multiple touch up events are fired, ignore re-fired events
                if (touchUpTime != 0 && touchUpTime - e.Timestamp < swipeMinBounceTime)
                    return;

                touchUpTime = e.Timestamp;
                var xDelta = point.Position.X - touchDownPosition.X;
                var yDelta = point.Position.Y - touchDownPosition.Y;
                // ensure touch event meets the requirements for a "swipe"
                if (touchUpTime - touchDownTime < swipeMaxTime && Math.Abs(xDelta) > swipeMinDistance && Math.Abs(xDelta) > Math.Abs(yDelta)) 
                {
                    // advance to next pivot item depending on swipe direction
                    var iNext = MyPivot.SelectedIndex + (delta > 0 ? -1 : 1);
                    iNext = iNext < 0 || iNext == MyPivot.Items.Count ? 0 : iNext;
                    MyPivot.SelectedIndex = iNext;
                }
                break;
        }
    }

Then subscribe to the Touch.FrameReported where desired, or for better optimization, subscribe to the event handler only while the pivot item containing the browser is selected:

    private void MyPivot_OnSelectionChanged(object sender, SelectionChangedEventArgs e) 
    {
        if ((sender as Pivot).SelectedItem == BrowserPivotItem) 
          Touch.FrameReported += Touch_FrameReported;
        else 
          Touch.FrameReported -= Touch_FrameReported;
    }


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM