操作uwp c#更新网格中的位置

时间:2017-11-27 15:38:04

标签: c# events uwp

我正在尝试创建一个系统,其中我有一个带有UI元素的网格,可以移动到位置以创建自定义表单。

在我正在使用Manipulation的时刻,这是我的代码

    void OBJManipulationStarted(object sender, ManipulationStartingRoutedEventArgs e)
    {
        forceManipulationsToEnd = false;

        if (sender is TextBlock)
        {
            TextBlock temp = sender as TextBlock;
            _transformOrginalColor = temp.Foreground;
            temp.RenderTransform = _transformGroup;
            temp.Opacity = 0.5;
            temp.Foreground = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 192, 57, 43));
            e.Handled = true;
        }

    }

    void OBJManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
    {
        if (sender is TextBlock)
        {
            TextBlock temp = sender as TextBlock;

            if (forceManipulationsToEnd)
            {
                e.Complete();
                return;
            }

            _previousTransform.Matrix = _transformGroup.Value;
            Windows.Foundation.Point center = _previousTransform.TransformPoint(new Windows.Foundation.Point(e.Position.X, e.Position.Y));

            _compositeTransform.CenterX = center.X;
            _compositeTransform.CenterY = center.Y;

            _compositeTransform.Rotation = (e.Delta.Rotation * 180) / Math.PI;
            _compositeTransform.ScaleX = _compositeTransform.ScaleY = e.Delta.Scale;
            _compositeTransform.TranslateX = e.Delta.Translation.X;
            _compositeTransform.TranslateY = e.Delta.Translation.Y;



            e.Handled = true;

        }

    }

    void OBJManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
    {
        e.Handled = true;
        if (sender is TextBlock)
        {
            TextBlock temp = sender as TextBlock;
            temp.Opacity = 1;
            temp.Foreground = _transformOrginalColor;
            //forceManipulationsToEnd = true;
            //temp.RenderTransform = null;
            //InitManipulationTransforms();
        }
    }


    private void UpdateQuote()
    {
        foreach(UIElement element in Base_Template.Children)
        {
            element.ManipulationMode = ManipulationModes.All;
            element.ManipulationStarting += new ManipulationStartingEventHandler(OBJManipulationStarted);
            element.ManipulationDelta += new ManipulationDeltaEventHandler(OBJManipulationDelta);
            element.ManipulationCompleted += new ManipulationCompletedEventHandler(OBJManipulationCompleted);
            UISEL_ELEMENTS.Add(element);
            if (element is TextBlock)
            {
                TextBlock temp = element as TextBlock;
                GridEditTool elementGET = new GridEditTool("TEXT", temp.Name);
                Hiarcey.Items.Add(elementGET);
            }

            if (element is Grid)
            {
                Grid temp = element as Grid;
                if ((string)temp.Tag != "INC")
                {
                    GridEditTool elementGET = new GridEditTool("SOLID BLOCK", temp.Name);
                    Hiarcey.Items.Add(elementGET);
                }
                else
                {
                    GridEditTool elementGET = new GridEditTool("INFO BLOCK", temp.Name);
                    Hiarcey.Items.Add(elementGET);
                }
            }


        }

使用它可以工作我可以点击UI元素它将跟随鼠标然后当你意识到鼠标时会停止,但是当我点击另一个UI元素时它会移动它们。我使用Padding Left和Top作为位置,当我移动对象后我看到这个值仍然是相同的,他们是一种方法我可以通过操作来做到这一点或者有人可以建议一个替代方案吗?

1 个答案:

答案 0 :(得分:0)

  

但是当我点击另一个UI元素时,它会移动它们。我使用Padding Left和Top作为位置,当我移动Object后我看到这个值时它仍然是相同的。

问题是您在_transformGroup事件处理程序中将UIElement设置为不同的ManipulationStarted。操纵了第一个元素后,_transformGroup保存了第一个元素位置。当操纵开始时,第二个元素将移动到相同的位置。

根据您的要求,您可以创建一个类来存储单独的TransformGroup,如下所示:

public class ElementTransform
{
    public MatrixTransform _previousTransform;
    public TransformGroup _transformGroup;
    public CompositeTransform _compositeTransform;

    public ElementTransform()
    {
        _transformGroup = new TransformGroup();
        _previousTransform = new MatrixTransform() { Matrix = Matrix.Identity };
        _compositeTransform = new CompositeTransform();
        _transformGroup.Children.Add(_previousTransform);
        _transformGroup.Children.Add(_compositeTransform);   
    }

}

UpdateQuote方法中,您可以使用全局DictionaryTransformGroupUIElement绑定,如下所示:

if (element is TextBlock)
{
    TextBlock temp = element as TextBlock;
    var EleTrs = new ElementTransform();

    temp.RenderTransform = EleTrs._transformGroup;
    if (_ElementTrsDir.ContainsKey(temp)) return;
    _ElementTrsDir.Add(temp, EleTrs);

}

<强>用法

void OBJManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    if (sender is TextBlock)
    {
        TextBlock temp = sender as TextBlock;

        if (forceManipulationsToEnd)
        {
            e.Complete();
            return;
        }

        var EleTrs = _ElementTrsDir[temp];

        EleTrs._previousTransform.Matrix = EleTrs._transformGroup.Value;
        Windows.Foundation.Point center = EleTrs._previousTransform.TransformPoint(new Windows.Foundation.Point(e.Position.X, e.Position.Y));

        EleTrs._compositeTransform.CenterX = center.X;
        EleTrs._compositeTransform.CenterY = center.Y;

        EleTrs._compositeTransform.Rotation = (e.Delta.Rotation * 180) / Math.PI;
        EleTrs._compositeTransform.ScaleX = EleTrs._compositeTransform.ScaleY = e.Delta.Scale;
        EleTrs._compositeTransform.TranslateX = e.Delta.Translation.X;
        EleTrs._compositeTransform.TranslateY = e.Delta.Translation.Y;

        e.Handled = true;
    }
}

请注意,为了提高性能,您无法在UIElement事件处理程序中设置RenderTransform OBJManipulationStarted