簡體   English   中英

C#Windows 8 Phone Map禁用縮放和滾動

[英]C# Windows 8 Phone Map Disable Zoom and Scroll

我目前在Windows 8 Phone App中使用Microsoft.Phone.Map ,希望能夠停止交互以更改縮放級別和在地圖上移動(滾動)。

我曾嘗試禁用交互功能,但問題是我需要使用興趣點層來擴展信息,當我使用IsEnabled = True;禁用地圖時,該信息將無法正常工作IsEnabled = True;

縮放級別設置為this.BigMap.ZoomLevel = 16; 首先,然后嘗試阻止這種交互的變化,我這樣做是:

void BigMap_ZoomLevelChanged(object sender, MapZoomLevelChangedEventArgs e)
    {
        this.BigMap.ZoomLevel = 16;
    }

但這意味着我得到了相當不錯的效果-是否有更好的方法來禁用縮放?

有人知道如何停止移動地圖嗎?我只希望屏幕上適合的部分保持原樣,而不要讓用戶四處移動。

您可以找到地圖元素的網格,並停止其縮放和移動操作,如下所示:

xaml:

<Grid x:Name="LayoutRoot">
    <maps:Map ZoomLevel="10"
              x:Name="MyMap"
              Loaded="Map_Loaded"
              Tap="MyMap_Tap"/>
</Grid>

CS:

    private void Map_Loaded(object sender, RoutedEventArgs e)
    {
        Grid grid = FindChildOfType<Grid>(MyMap);
        grid.ManipulationCompleted += Map_ManipulationCompleted;
        grid.ManipulationDelta += Map_ManipulationDelta;
    }

    private void Map_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
    {
        // disable zoom
        if (e.DeltaManipulation.Scale.X != 0.0 ||
            e.DeltaManipulation.Scale.Y != 0.0)
            e.Handled = true;

        //disable moving
        if (e.DeltaManipulation.Translation.X != 0.0 ||
            e.DeltaManipulation.Translation.Y != 0.0)
            e.Handled = true;
    }

    private void Map_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
    {
        // disable zoom
        if (e.FinalVelocities.ExpansionVelocity.X != 0.0 ||
            e.FinalVelocities.ExpansionVelocity.Y != 0.0)
            e.Handled = true;

        //disable moving
        if (e.FinalVelocities.LinearVelocity.X != 0.0 ||
            e.FinalVelocities.LinearVelocity.Y != 0.0)
        {
            e.Handled = true;
        }
    }

    public static T FindChildOfType<T>(DependencyObject root) where T : class
    {
        var queue = new Queue<DependencyObject>();
        queue.Enqueue(root);

        while (queue.Count > 0)
        {
            DependencyObject current = queue.Dequeue();
            for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--)
            {
                var child = VisualTreeHelper.GetChild(current, i);
                var typedChild = child as T;
                if (typedChild != null)
                {
                    return typedChild;
                }
                queue.Enqueue(child);
            }
        }
        return null;
    }

    private void MyMap_Tap(object sender, GestureEventArgs e)
    {
        //This is still working
    }

由於僅禁用縮放和移動操作,因此點按並保持正常工作。

希望這會有所幫助!

編輯:請注意,當您調用FindChildOfType(MyMap)時,地圖元素應該可見。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM