UWP中的MouseDragElementBehavior

时间:2016-07-07 09:08:45

标签: c# wpf uwp

我目前正在将用于Windows Phone 8的旧游戏在WPF / Silverlight中移植到通用Windows平台。我发了一封信,你可以使用MouseDragElementBehavior类在画布中移动。 UWP中有没有类似的课程?

1 个答案:

答案 0 :(得分:0)

在评论中建议了一个名为ManipulationDelta的事件。以下是如何使用它:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Canvas>
        <TextBlock
            FontSize="64"
            ManipulationDelta="LetterA_ManipulationDelta"
            ManipulationMode="All"
            RenderTransformOrigin="0.5,0.5"
            Text="A">
            <TextBlock.RenderTransform>
                <TranslateTransform x:Name="dragLetterA" />
            </TextBlock.RenderTransform>
        </TextBlock>

        <TextBlock
            FontSize="64"
            ManipulationDelta="LetterB_ManipulationDelta"
            ManipulationMode="All"
            RenderTransformOrigin="0.5,0.5"
            Text="B">
            <TextBlock.RenderTransform>
                <TranslateTransform x:Name="dragLetterB" />
            </TextBlock.RenderTransform>
        </TextBlock>

        <TextBlock
            FontSize="64"
            ManipulationDelta="LetterC_ManipulationDelta"
            ManipulationMode="All"
            RenderTransformOrigin="0.5,0.5"
            Text="C">
            <TextBlock.RenderTransform>
                <TranslateTransform x:Name="dragLetterC" />
            </TextBlock.RenderTransform>
        </TextBlock>
    </Canvas>
</Grid>

背后的代码如下:

private void LetterA_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    dragLetterA.X += e.Delta.Translation.X;
    dragLetterA.Y += e.Delta.Translation.Y;
}

private void LetterB_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    dragLetterB.X += e.Delta.Translation.X;
    dragLetterB.Y += e.Delta.Translation.Y;
}

private void LetterC_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    dragLetterC.X += e.Delta.Translation.X;
    dragLetterC.Y += e.Delta.Translation.Y;
}

就这么简单。

希望这有帮助。