简体   繁体   English

在ViewportControl WP8中移动对象

[英]Move object in ViewportControl WP8

I'm using the ViewportControl to scroll around and zoom in and out of my Map. 我正在使用ViewportControl滚动并放大和缩小我的地图。 In this map I've got a green ellipse which I wish to move around. 在这张地图中,我有一个绿色的椭圆形,我希望它能够移动。 Previously I used a ScrollViewer where I set the manipulationMode of the ScrollViewer to control, and thus making it capable of moving my ellipse around. 以前我使用ScrollViewer设置ScrollViewer的manipulationMode来控制,从而使它能够移动我的椭圆。 However I can't find a similar way for the ViewportControl. 但是我找不到类似于ViewportControl的方法。 So is there a way to move my ellipse around? 那么有一种方法可以移动我的椭圆吗?

The code I've got so far is: 我到目前为止的代码是:

The xaml part where I have my ViewportControl around my map 我在我的地图周围有我的ViewportControl的xaml部分

   <ViewportControl x:Name="ViewPortTestTest" Bounds="0,0,1271,1381.5" Height="480" Width="800" Canvas.ZIndex="1" Grid.Row="1">
        <ViewportControl.RenderTransform>
            <CompositeTransform x:Name="myTransformTest"/>
        </ViewportControl.RenderTransform>

        <View:Map x:Name="ZoomableContent" >
            <View:Map.RenderTransform>
                <CompositeTransform x:Name="myTransform" />
                <!-- ScaleX="{Binding Map.imageScale}" ScaleY="{Binding Map.imageScale}"/>-->
            </View:Map.RenderTransform>
        </View:Map>
    </ViewportControl>

It is in the map where I add the ellipse. 它是在我添加椭圆的地图中。 The viewModel where I manipulate my ellipse 我操纵椭圆的viewModel

public void ManStart(ManipulationStartedEventArgs e)
    {

        e.Handled = true;

        ViewportControl VP = FindParentOfType<ViewportControl>(ChampViewModelSel);


       }
    }

    public void ManDelta(ManipulationDeltaEventArgs e)
    {
        e.Handled = true;


        Point fingerPosition = e.DeltaManipulation.Translation;

        Temp.x = fingerPosition.X;
        Temp.y = fingerPosition.Y;

        }
    }

Where Temp.x and Temp.y is the new position of the ellipse. 其中Temp.x和Temp.y是椭圆的新位置。

I think you could try to use TouchPanel from XNA and Touch.FrameReported event for this purpose. 我认为您可以尝试使用XNA中的TouchPanelTouch.FrameReported事件来实现此目的。 Probably Map and VieportControl handle the manipulation event so it won't fire with your code. 可能Map和VieportControl处理操作事件,因此它不会触发您的代码。

In my proposal Touch_FrameReported will be fired every time you touch the screen, so we have to check only for situations we need - here comes IsGestureAvailable and TouchAction . 在我的提议中, Touch_FrameReported将在每次触摸屏幕时被触发,因此我们只需要检查我们需要的情况 - 这里是IsGestureAvailableTouchAction Simple code can look like this: 简单的代码可能如下所示:

public MainPage()
{
    InitializeComponent();
    TouchPanel.EnabledGestures = GestureType.FreeDrag;
    Touch.FrameReported += Touch_FrameReported;
}

private void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
    if (TouchPanel.IsGestureAvailable) // check only dragging 
    {
       // get point relative to Viewport
       TouchPoint mainTouch = e.GetPrimaryTouchPoint(ViewPortTestTest);
       // check if drag has completed (key up)
       if (mainTouch.Action == TouchAction.Up)
       {
          Temp.x = mainTouch.Position.X;
          Temp.y = mainTouch.Position.Y;
       }
    }
}

You can read more about Gestures here at MSDN and on this blog . 您可以在MSDN此博客上阅读有关手势的更多信息。

You can also check other GestureTypes and for example check if TouchAction.Down and user clicked on your Ellipse. 您还可以检查其他GestureTypes,例如检查TouchAction.Down和用户是否单击了您的Ellipse。

These methods give you many possibilities to read TouchPoints and their Actions so maybe it will help. 这些方法为您提供了阅读TouchPoints及其Actions许多可能性,因此它可能会有所帮助。


It took me a while to find a way how to disable Vieport. 我花了一段时间才找到了如何禁用Vieport的方法。 I've found a little 'hack' for that, it will be easier if your VieportControl was only horizontal or vertical (there is a lock property, but doesn't work for both). 我已经找到了一点“黑客”,如果你的VieportControl只是水平或垂直(有锁属性,但两者都不起作用)会更容易。 Here is this little 'hack' which should disable both horizontal and vertical scrolling: 这是一个小的'hack',它应该禁用水平和垂直滚动:

Rect originalBounds = new Rect();
private void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
    TouchPoint myTouchPoint = e.GetPrimaryTouchPoint(ViewPortTestTest);
   // disable scrolling
   if (myTouchPoint.Action == TouchAction.Down) // here probably some other statement like if user hit ellipse
   {
       originalBounds = ViewPortTestTest.Bounds;
       ViewPortTestTest.Bounds = ViewPortTestTest.Viewport; // set current view as Bounds
   }

    // more logic

   // enable once again
   if (myTouchPoint.Action == TouchAction.Up)
        ViewPortTestTest.Bounds = originalBounds;
}

When I want to disable scrolling - I set bounds of the VieportControl to the current view (and I remember original ones). 当我想禁用滚动时 - 我将VieportControl的边界设置为当前视图(我记得原始视图)。 When I want to enable scrolling - I bring back original bounds. 当我想启用滚动时 - 我带回原始边界。

As I've tested it - it is working - of course it needs a little more logic in locking if statement, so that it won't lock every time you touch the screen. 正如我测试它 - 它正在工作 - 当然它需要更多的逻辑来锁定if语句,以便它不会每次触摸屏幕时锁定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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