[UWP] [C#] Windows Phone 10 UWP,如何移动&调整矩形大小? [英] [UWP][C#]Windows Phone 10 UWP, How to move & resize a rectangle?

查看:43
本文介绍了[UWP] [C#] Windows Phone 10 UWP,如何移动&调整矩形大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!我对C#和XAML很陌生,我正在尝试创建一个应用程序,允许用户移动矩形,使用捏合调整矩形大小,并在触摸时更改颜色。我已经成功创建了Windows Phone 8.1
Silverlight的应用程序,但是我在为Windows Phone 10 Universal创建它时遇到了很多麻烦。关于在Windows Phone 10中处理操作事件的文献非常有限。

Hello! I am pretty new to C# and XAML, and I am trying to create an application that allows the user to move a rectangle, resize the rectangle using pinch, and change the color when touched. I have successfully created the application for Windows Phone 8.1 Silverlight, but I am having a lot of trouble creating it for Windows Phone 10 Universal. There is very limited literature out there in regards to handling manipulation events in Windows Phone 10.

有人可以直接告诉我相应的信息吗?这样做,或者你可以告诉我我的代码出错了吗?

Can someone please direct me to the appropriate information for doing this, or if you could show me where I went wrong in my code?

我会非常感激!

谢谢,

Page

这是我的MainPage.xaml.cs

Here is my MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Input;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
using System.Windows.Input;



namespace Gesture_App_UWP
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        // Global translation transform used for changing the position of 
        // the Rectangle based on input data from the touch contact.
        private TranslateTransform dragTranslation;
        private CompositeTransform deltaTransform;
        private TransformGroup rectangleTransforms;

        public MainPage()
        {
            this.InitializeComponent();

            // Pointer event listeners.
            touchRectangle.PointerPressed += touchRectangle_PointerPressed;
            touchRectangle.PointerReleased += touchRectangle_PointerReleased;

            // Listener for ManipulationDelta event.
            touchRectangle.ManipulationDelta += new ManipulationDeltaEventHandler(touchRectangle_ManipulationDelta);
            touchRectangle.ManipulationStarted += new ManipulationStartedEventHandler(touchRectangle_ManipulationStarted);
            touchRectangle.ManipulationCompleted += new ManipulationCompletedEventHandler(touchRectangle_ManipulationCompleted);

            dragTranslation = new TranslateTransform();
            deltaTransform = new CompositeTransform();
            rectangleTransforms = new TransformGroup();

            rectangleTransforms.Children.Add(dragTranslation);
            rectangleTransforms.Children.Add(deltaTransform);

            touchRectangle.RenderTransform = rectangleTransforms;
        }
        private async void button_Info_Click(object sender, RoutedEventArgs e)
        {
            var dialog_Info = new MessageDialog("Page Lynn Potter");
            await dialog_Info.ShowAsync();
        }
        private async void button_Instructions_Click(object sender, RoutedEventArgs e)
        {
            var dialog_Instructions = new MessageDialog("PINCH - Zoom In & Out, LONG TOUCH - Toggle Color, FLICK - Move Rectangle");
            await dialog_Instructions.ShowAsync();
        }
        //Handler for pointer released event.
        private void touchRectangle_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            //Change rectangles color
            SolidColorBrush greenBrush = new SolidColorBrush(Colors.Green);
            touchRectangle.Fill = greenBrush;
        }
        // Handler for pointer pressed event.
        private void touchRectangle_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            //Change rectangles color
            SolidColorBrush orangeBrush = new SolidColorBrush(Colors.Orange);
            touchRectangle.Fill = orangeBrush;
        }
        void touchRectangle_ManipulationInertiaStarting(object sender, ManipulationInertiaStartingRoutedEventArgs e)
        {
            //Change rectangles color
            SolidColorBrush redBrush = new SolidColorBrush(Colors.Red);
            touchRectangle.Fill = redBrush;
        }
        void touchRectangle_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
        {
            //Change rectangles color
            SolidColorBrush orangeBrush = new SolidColorBrush(Colors.Orange);
            touchRectangle.Fill = orangeBrush;
        }

        // Handler for ManipulationDelta event.
        // ManipulationDelta data loaded into translation transform & applied to Rectangle
        void touchRectangle_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY | ManipulationModes.Scale;

            // Move rectangle.
            dragTranslation.X += e.Delta.Translation.X;
            dragTranslation.Y += e.Delta.Translation.Y;


            // Resize rectangle.
            if (deltaTransform.ScaleX > 0 && deltaTransform.ScaleY > 0)
            {
                //Scale Rectangle
                deltaTransform.ScaleX *= e.Delta.Scale;
                deltaTransform.ScaleY *= e.Delta.Scale;
            }
        }
        //When manipulation ends, event handler restores original color to Rectangle
        void touchRectangle_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {
            //Change rectangles color
            SolidColorBrush greenBrush = new SolidColorBrush(Colors.Green);
            touchRectangle.Fill = greenBrush;
        }
    }
}




   



   

推荐答案

您好,

我已经尝试过您的代码并且运行良好。

我认为操纵事件几乎可以满足您的要求(移动或调整大小)

https://docs.microsoft.com/en-us/windows/uwp/input-and-devices/touch-interactions

那么你能检查一下你是否已经以正确的方式设置ManipulationMode?

这是我的代码:

<Rectangle Name="touchRectangle" Width="100" Height="100" Fill="Red" ManipulationMode="All"/>

祝你好运,

Best regards,

Barry

Barry

< span lang ="EN-US"style ="margin:0px">


这篇关于[UWP] [C#] Windows Phone 10 UWP,如何移动&amp;调整矩形大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆