【问题标题】:Windows Phone - Avoid high resource usageWindows Phone - 避免高资源使用
【发布时间】:2013-04-20 09:33:28
【问题描述】:

我使用 Windows Store-Kit 测试了我的 Windows Phone 8 应用程序,但它失败了。它说“高资源使用率”。我正在使用 MVVM 模式,这意味着我正在将视图中的元素与 ViewModel 属性绑定。

高使用率示例:我有一个 MainView,它有一个 ContentControl。 ContentControl 的Content 是另一个View(我们称之为ChildView)。当我单击 ChildView 中的 TextBox 时,会弹出 InputScope(您可以在其中键入)并且 View 会上升,因此可以看到 TextBox。当 View 上升时(注意 TextBox 在 Pivot 中),它开始滞后。我不知道为什么,因为我只关注 TextBox 而 PivotItem 上升。我的问题是如何降低资源使用率?如果您需要什么,请写下来,我会在这里发布。

MainPage.xaml

 <Grid>
    <Grid x:Name="LayoutRoot"  HorizontalAlignment="Left">

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <ScrollViewer Name="MyScrollViewer" Height="Auto" HorizontalAlignment="Left" VerticalAlignment="Top" Width="Auto" HorizontalScrollBarVisibility="{Binding Horizontal}" VerticalScrollBarVisibility="{Binding Vertical}">
            <!--ContentPanel - zusätzliche Inhalte hier platzieren-->
            <StackPanel ScrollViewer.HorizontalScrollBarVisibility="Auto" Grid.Row="2" Opacity="50">
                <ContentControl Content="{Binding MyContent}" Name="MyContentControl" IsTabStop="False" VerticalContentAlignment="Stretch"  HorizontalContentAlignment="Stretch"/>
            </StackPanel>

        </ScrollViewer>
    </Grid>
</Grid>

ChildView.xaml http://textuploader.com/?p=6&id=zMDoD

Store-Kit 结果(德语,但我认为很清楚)http://i.imagebanana.com/img/j6z24o9a/Unbenannt.png

这是视图的 ViewModel 中的属性的样子(在 Store-Kit 结果中显示)

    private string _anlohnsteuer;
    public string ANLohnsteuer
    {
        get { return _anlohnsteuer; }
        set
        {
            _anlohnsteuer = value;
            RaisePropertyChanged(() => ANLohnsteuer);
        }
    }

【问题讨论】:

  • 请出示您的代码。
  • 最好知道它是用于 wp8 还是 wp7。我还记得一些关于 ui 总是需要响应的事情,但我不确定它是否仅适用于 windows8 应用程序或 wp8。因此,也许检查您的某些代码是否使应用程序挂起或冻结 50 毫秒(这似乎是微软的幻数)并尝试将其最小化。也许您可以调度一些东西并将这些长调用分发到多个帧。
  • 我正在使用 Windows Phone 8,很抱歉这么说,但我完全不知道你的意思是什么......当我点击时它似乎会“冻结”一个 TextBox 和 PivotItem 正在上升。

标签: c# performance memory-management windows-phone-8


【解决方案1】:

这个人的好文章:

http://fiercedesign.wordpress.com/2012/08/14/windows-phone-performance-best-practices/

阅读主题“重绘区域”

如果 UI 不流畅/流畅,您必须检查 UI 每次都重绘了哪些元素。您可以通过转到 App.xaml.cs 并启用重绘区域来执行此操作 Application.Current.Host.Settings.EnableRedrawRegions = true;

如果某个区域或元素闪烁,则说明有问题。尝试添加 CacheMode="BitmapCache"

到你的元素。

我的应用中有很多颜色闪烁。 UI 重新绘制,即我的 TextBlocks,即使我没有更改它。我所要做的就是像这样更改所有元素(尤其是 TextBlocks 和 Grids):

应用程序现在运行流畅。

编辑:我有一个 MainViewMode,它初始化了 5 个其他 ViewModel(MainView 包含 1 个 ContentControl,根据用户单击的按钮,ContentControl 获得一个新 View,而 View 将获得一个新 ViewModel)。由于我在启动时只加载了 1 个 View 和 1 个 ViewModel,因此我将其他 4 个 ViewModel 的初始化放在了 BackgroundWorker -> 应用程序启动时间之前约为 7 秒,现在只有 2 个!

【讨论】:

    【解决方案2】:

    我不确定,但您似乎正在尝试将可滚动控件放入另一个可滚动控件中。那是个坏主意。

    Pivot 通常会使用大量资源,因此当您处理超过 4 个项目(取决于内容)时,我会避免使用它们。

    Viel Glück mit deiner 应用程序。

    【讨论】:

    • 旧代码。 Scrollviewer 不见了,在“Grid x:Name="LayoutRoot" Horizo​​ntalAlignment="Left">”我使用了 'CacheMode="BitmapCache"' -> ContentControl 的内容现在正在流畅运行:)。顺便提一句。除了 Pivot 还有其他选择吗?例如像“TabItem”这样的东西?
    • 我不知道。不过,第三方可能会提供一些自定义控件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    • 2013-01-08
    • 2011-01-17
    • 1970-01-01
    相关资源
    最近更新 更多