【问题标题】:Improve the universal windows phone performance?提高通用windows phone 性能?
【发布时间】:2017-01-11 18:39:08
【问题描述】:

我在画布内有 10 个矩形,在 ManipulationDelta 事件中,我必须更改高度和宽度。它在 Windows 桌面上正常工作,但在通用 Windows 设备(手机)中操作矩形时需要一些时间。如何平滑地操作 Windows 设备中的 UI 元素。请建议我,有没有其他方法可以解决这个问题?

这是我的代码:

<Canvas Name="LayoutRoot"  Width="300" Height="500">
    <Rectangle Fill="Red" Height="100" Width="100"/>
    <Rectangle Fill="Red" Height="100" Width="100"/>
    <Rectangle Fill="Red" Height="100" Width="100"/>
    <Rectangle Fill="Red" Height="100" Width="100"/>
    <Rectangle Fill="Red" Height="100" Width="100"/>
    <Rectangle Fill="Red" Height="100" Width="100"/>
    <Rectangle Fill="Red" Height="100" Width="100"/>
    <Rectangle Fill="Red" Height="100" Width="100"/>
    <Rectangle Fill="Red" Height="100" Width="100"/>
    <Rectangle Fill="Green" Height="100" Width="100"/>
</Canvas>


private void MainPage_OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{      
    foreach (Rectangle rectAngle in LayoutRoot.Children)
    {
        rectAngle.Width += e.Cumulative.Scale;
        rectAngle.Height += e.Cumulative.Scale;
        Canvas.SetLeft(rectAngle, LayoutRoot.Width / 2 - rectAngle.ActualWidth / 2);
        Canvas.SetTop(rectAngle, LayoutRoot.Height / 2 - rectAngle.ActualHeight / 2);
     }
}

【问题讨论】:

  • 一个可能的解决方案是使用 Win2D 库。 Win2D 是一个带有 GPU 加速的图形渲染库。

标签: c# uwp uwp-xaml


【解决方案1】:

您必须像 TranslateTransform 一样使用 RenderTransform 来移动您的元素,因为它们不是依赖属性,而是以性能为导向的。因此,改为使用 Canvas Top 和 Left 属性设置 RenderTransformOrigin 和一个 TranslateTransform。

您会注意到性能确实提高了。

 private void MainPage_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
    {
        foreach (Rectangle child in LayoutRoot.Children)
        {
            child.Width += e.Cumulative.Scale;
            child.Height += e.Cumulative.Scale;

            //This is C#7 in case C#6 adapt
            if(child.RenderTransform is TranslateTransform tf)
            {
                tf.X = LayoutRoot.Width / 2 - child.ActualWidth / 2;
                tf.Y = LayoutRoot.Height / 2 - child.ActualHeight / 2;
            }
        }

    }

    private void Initialize()
    {
        var redbrush = new SolidColorBrush(Colors.Red);

        foreach (Rectangle child in LayoutRoot.Children)
        {
            child.Fill = redbrush;
            child.Height = 100;
            child.Width = 100;
            child.RenderTransformOrigin = new Point(0.5, 0.5);
            child.RenderTransform = new TranslateTransform();
        }
    }

【讨论】:

  • 谢谢..那么它在windows桌面上是如何工作的呢?为什么 windows 设备需要时间?
  • @Santhiya 手机上较低的功耗和处理能力意味着性能将低于台式机。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多