【问题标题】:Windows Phone 8.1 RT: how to prevent manipulation event throw object?Windows Phone 8.1 RT:如何防止操纵事件抛出对象?
【发布时间】:2015-07-03 15:58:57
【问题描述】:

我是 C# 新手。我想防止通过操作事件抛出 XAML 的对象。我的应用是在 Windows Phone 8.1 RT 中开发的。

我有 XAML 的矩形:

<Canvas x:Name="MyCanvas" Background="White">
        <Rectangle Name="TestRectangle"
          Width="100" Height="200" Fill="Blue" 
          ManipulationMode="All"/>
</Canvas>

在主页中:

public MainPage()
    {
        this.InitializeComponent();
        // Handle manipulation events.
        TestRectangle.ManipulationDelta += Drag_ManipulationDelta;
        dragTranslation = new TranslateTransform();
        TestRectangle.RenderTransform = this.dragTranslation;

        this.NavigationCacheMode = NavigationCacheMode.Required;
    }
void Drag_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
    {
        // Move the rectangle.
            dragTranslation.X += e.Delta.Translation.X;
            dragTranslation.Y += e.Delta.Translation.Y;
    }

    private void TestRectangle_PointerPressed(object sender,
PointerRoutedEventArgs e)
    {
        Rectangle rect = sender as Rectangle;

        // Change the size of the Rectangle
        if (null != rect)
        {
            rect.Width = 250;
            rect.Height = 150;
        }
    }
    private void TestRectangle_PointerReleased(object sender,
PointerRoutedEventArgs e)
    {
        Rectangle rect = sender as Rectangle;

        // Reset the dimensions on the Rectangle
        if (null != rect)
        {
            rect.Width = 200;
            rect.Height = 100;
        }
    }
    private void TestRectangle_PointerExited(object sender,
PointerRoutedEventArgs e)
    {
        Rectangle rect = sender as Rectangle;

        // Finger moved out of Rectangle before the pointer exited event
        // Reset the dimensions on the Rectangle
        if (null != rect)
        {
            rect.Width = 200;
            rect.Height = 100;
        }
    }

当用户退出事件时如何移动对象而不抛出?

【问题讨论】:

    标签: xaml windows-phone-8.1


    【解决方案1】:

    我假设“抛出”是指要移除矩形的惯性方面?在这种情况下,请从您的 XAML 代码中删除 ManipulationMode 设置器,然后将此行插入页面的构造函数 (C#):

    TestRectangle.ManipulationMode = ManipulationMode.TranslateX | ManipulationMode.TranslateY;
    

    这应该消除您所说的内部效应。不过附带说明一下,这种移动 UIElements 的方法会给您带来非常烦人的延迟。不久前我遇到了类似的问题,并且通过使用不同的方法解决了该问题:

    Delay in drag/drop of UIElement in Windows Phone 8.1

    【讨论】:

    • 我明白了。非常感谢,那个代码很好:))!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    相关资源
    最近更新 更多