覆盖用户控制的操纵Delta

时间:2015-03-04 04:37:13

标签: c# user-controls winrt-xaml windows-8.1 manipulationdelta

我有一个用户控件随着我的手指移动而移动。用户控件有自己的操作增量集。 我的问题是,当我的手指在用户控制器上并且我试图向上移动时,我有滞后因为它没有用我的手指向上移动。 当我长按我的屏幕时出现用户控件,用户控件的位置是我的手指被按下的位置。当我移动手指时,它会随着我的手指移动,但是一旦我的手指进入用户控制,我就没有平稳的动作。

任何帮助将不胜感激。 XAML

<Page x:Name="page1" 
    x:Class="controlMagnifier.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:controlMagnifier"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid x:Name="ParentGrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" PointerReleased="ParentGrid_OnPointerReleased"  >
        <Canvas x:Name="InkPresenter" Height="auto" Width="auto">

            <Image Stretch="None" x:Name="image2"  >
                <Image.Source >
                    <BitmapImage UriSource="/Assets/wallpaper.jpg" />
                </Image.Source>
            </Image>
        </Canvas>

        <local:MagnifierUsercontrol  x:Name="MagnifyTip"  Visibility="Collapsed" ManipulationDelta="MagnifyTip_ManipulationDelta" ManipulationMode="All" >
            <local:MagnifierUsercontrol.RenderTransform>
                <CompositeTransform/>
            </local:MagnifierUsercontrol.RenderTransform>
        </local:MagnifierUsercontrol>

    </Grid>
</Page>

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.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.Shapes;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Diagnostics;
using Windows.UI.Popups;
using Windows.UI.Input;
using Windows.Storage;
using Windows.UI.Xaml.Media.Imaging;



// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace controlMagnifier
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public const int XAxis = 105;
        public const int YAxis = 270;
        public Thickness margin;



        public WriteableBitmap CurrentBitmapObj, CurrentCroppedImage = null;
        public MainPage()
        {
            this.InitializeComponent();
            ParentGrid.Holding += Grid_Holding;
            image2.PointerMoved += InkCanvas_PointerMoved;
            image2.PointerReleased += ParentGrid_OnPointerReleased;
            image2.CacheMode=new BitmapCache();
            StoreCrrentImage();
        }



/// <summary>
/// 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
        private void Grid_Holding(object sender, HoldingRoutedEventArgs e)
        {
            //Point p;
            try
            {
                Point p = e.GetPosition(ParentGrid);

                double height = MagnifyTip.Height;
                double width = MagnifyTip.Width;

                margin = MagnifyTip.Margin;
                margin.Left = p.X - XAxis;
                margin.Right = p.Y - YAxis;
                MagnifyTip.Margin = margin;
               // MagnifyTip.Margin = new Thickness(p.X - XAxis, p.Y - YAxis, 0, 0);
                MagnifyTip.Visibility = Visibility.Visible;
            }
            catch (Exception)
            {

                throw;
            }


        }


        private  void InkCanvas_PointerMoved(object sender, PointerRoutedEventArgs e)
        {

            try
            {
                PointerPoint pt = e.GetCurrentPoint(image2);
                Point currentContactPt = pt.Position;


                double xValue = currentContactPt.X;
                double yValue = currentContactPt.Y;


                //  MagnifyTip.
                double height = MagnifyTip.Height;
                double width = MagnifyTip.Width;


                MagnifyTip.image1.ImageSource = CropBitmap(currentContactPt);

                MagnifyTip.Margin = new Thickness(xValue - XAxis, yValue - YAxis, 0, 0);

            }
            catch (Exception)
            {

                throw;
            }

            finally
            {e.Handled = true;}

        }

        private async void StoreCrrentImage()
        {

            try
            {
StorageFile storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/wallpaper.jpg", UriKind.RelativeOrAbsolute));

                using (
                    Windows.Storage.Streams.IRandomAccessStream fileStream =
                        await storageFile.OpenAsync(FileAccessMode.Read))
                {
                    BitmapImage bitmapImage = new BitmapImage();

                    await bitmapImage.SetSourceAsync(fileStream);
                    WriteableBitmap writeableBitmap =
                        new WriteableBitmap(bitmapImage.PixelWidth, bitmapImage.PixelHeight);
                    fileStream.Seek(0);
                    await writeableBitmap.SetSourceAsync(fileStream);
                    CurrentBitmapObj = writeableBitmap;
                    writeableBitmap.Invalidate();
                }
            }
            catch (Exception )
            {

                throw;
            }


            finally
            {}

            }

        /// <summary>
        /// This method crops the image by accepting x and y as Arguments
        /// </summary>
        /// <param name="point"></param>
        /// <returns>Cropped Image</returns>
        private WriteableBitmap CropBitmap(Point point)
        {
            try
            {
                CurrentCroppedImage = CurrentBitmapObj.Crop(Convert.ToInt32(point.X), Convert.ToInt32(point.Y), 100, 100);
               // var resized = CurrentCroppedImage.Resize(200, 300, WriteableBitmapExtensions.Interpolation.Bilinear);

            }
            catch (Exception)
            {

                throw;
            }


            return CurrentCroppedImage;


        }

        private void MagnifyTip_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            Point p;
            p.X = e.Delta.Translation.X;
            p.Y = e.Delta.Translation.Y;
            var Dragme = (CompositeTransform)MagnifyTip.RenderTransform;
            Dragme.TranslateX += p.X;
            Dragme.TranslateY += p.Y;
            // MagnifyTip.controlCanvas.SetValue(Canvas.SetLeft, p.X);
        }




        private void ParentGrid_OnPointerReleased(object sender, PointerRoutedEventArgs e)
        {
            MagnifyTip.Visibility = Visibility.Collapsed;
           // throw new NotImplementedException();
        }
    }

}

用户控件看起来像the user control comes up just at the place of tapping and holding. Any movement will only happen while the finger is still tapped and moved around.

1 个答案:

答案 0 :(得分:0)

如果您想避免在ManipulationDelta中有意实施的延迟以区别于Tap,请改用PointerMove

我已回答类似问题here