简体   繁体   English

如何进一步优化Windows Store应用程序?

[英]How to further optimize Windows Store apps?

I have 2 levels deep collection that I render into something like a Header, Child Item. 我有2个级别的深层收藏,可以将其渲染为诸如Header,Child Item之类的东西。

Roughly, my code looks something like this: 大致来说,我的代码如下所示:

    <ScrollViewer>
        <ItemsControl IsEnabled="{Binding Path=IsEnabled}"
                      ItemsSource="{Binding Path=HeaderViewModels, Mode=OneTime}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemsTemplate>
                <DataTemplate>
                    <StackPanel Visibility="{Binding Path=ShouldShow, Converter={StaticResource SomeConverter}}">
                        <TextBlock Text="{Binding Path=Description, Mode=OneTime}" />
                        <ItemsControl ItemsPanel="{StaticResource WinRTXAMLWrapPanel}"
                                      ItemsSource="{Binding Path=ChildViewModels, Mode=OneTime}">
                            <i:Interaction.Behaviors>
                                <SomeClass:AdjustSizeBehavior SizeFromParentMode="Height" />
                            </i:Interaction.Behaviors>
                            <ItemsControl.ItemsTemplate>
                                <DataTemplate>
                                <ContentControl>
                                    <i:Interaction.Behaviors>
                                        <SomeClass:IsEnabledBehavior />
                                        <SomeClass:PointerPressBehavior />
                                        <SomeClass:PointerLeaveBehavior />
                                    </i:Interaction.Behaviors>
                                    <StackPanel>
                                        <TextBlock Text="X">
                                            <i:Interaction.Behaviors>
                                                <SomeClass:SetXMarkColor />
                                            </i:Interaction.Behaviors>
                                        </TextBlock>
                                        <TextBlock Text="{Binding Path=Description, Mode=OneTime}" />
                                    </StackPanel>
                                </ContentControl>
                                </DataTemplate>
                            </ItemsControl.ItemsTemplate>
                        </ItemsControl>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemsTemplate>
        </ItemsControl>
    </ScrollViewer>

Left out the styling, but its pretty much background, margins, font-size, etc. Also, you can see I'm using lots of Bindings and Behaviors. 省略样式,但背景,页边距,字体大小等都差不多。此外,您还可以看到我正在使用大量的绑定和行为。

I already did most of the optimization I can think of. 我已经做了我能想到的大部分优化。 Removed unnecessary UI elements (ex. rectangles used for 'backgrounds'), used StackPanel instead of Grid, set column / height to static instead of auto. 删除了不必要的UI元素(例如,用于“背景”的矩形),使用StackPanel而不是Grid,将column / height设置为static而不是auto。 Basically done what I can on this links: Twelve ways to improve wpf performance and Best practices for Windows Store Apps 基本上完成了我可以在此链接上做的事情: 十二种提高WPF性能的方法Windows Store Apps的最佳做法

With the optimizations I have done, I have actually reduced loading time of 10 headers + 8 child each (total of 80), from 1.8 sec to around 0.5 ms - 0.6 ms. 通过完成优化,我实际上减少了10个标头+ 8个子标头(总共80个)的加载时间,从1.8秒减少到大约0.5 ms-0.6 ms。

However, I would like it to reach less than 0.5 ms so it is like 'instant'. 但是,我希望它小于0.5毫秒,所以它就像“即时”。

Is there anything else I can do to improve performance? 我还有什么可以做以提高性能?

Thank you! 谢谢!

It looks like your XAML is quite clean. 看起来您的XAML很干净。 What about your C# code? 您的C#代码呢?

If you use async/await, and push tasks into a background thread, it can really boost performance. 如果您使用async / await,并将任务推送到后台线程,则可以真正提高性能。 Async/await is the secret to creating fast, ressponsive apps. 异步/等待是创建快速响应式应用程序的秘诀。

Visual Studio 2015 has some great improvements when it comes to identifying and fixing WPF performance bottlenecks. Visual Studio 2015在识别和修复WPF性能瓶颈方面进行了一些重大改进。

You can identify which lines of code are taking the longest, and are thus a candidate for pushing onto a background thread using async/await, using the profiler. 您可以使用分析器确定哪些代码行花费的时间最长,因此可以选择使用异步/等待将其推入后台线程。 The profiler is awesome: it displays how long each block or line of code has taken, in milliseconds, right in the editor. 探查器很棒:它在编辑器中以毫秒为单位显示每段代码或代码行所花费的时间。

More: 更多:

While we are at it, if Microsoft implements compile time bindings in WPF, it will dramatically boost performance. 在此情况下,如果Microsoft在WPF中实现编译时绑定,它将大大提高性能。 Binding speed can be as much as 10x faster as its no longer based on reflection. 绑定速度可能比不再基于反射的速度快10倍。 Add your vote to implement this feature here: https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/7810488-implement-x-bind-for-wpf 添加您的投票以实现此功能: https : //visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/7810488-implement-x-bind-for-wpf

The question of whether or not to use to use a master/detail grid is an interesting related question. 是否使用主/细节网格的问题是一个有趣的相关问题。 I was chatting to a dev who has been using WPF since it was released in 2006. He is a bit of a guru, and he was of the opinion that master/detail grids are not always the best thing from both a usability and speed point of view. 自2006年发布以来,我一直在与一位使用WPF的开发人员聊天。他是一位专家,他认为从可用性和速度角度来看,主/细节网格并不总是最好的选择看法。 The reason is that you almost always end up loading all of the details on startup, which is slow, unless you resort to some interesting tricks to lazy load in the background. 原因是您几乎总是在启动时最终加载所有详细信息,这很慢,除非您采取一些有趣的技巧在后台延迟加载。 And if you have a few of the detail grids expanded, the screen begins to look messy. 而且,如果您扩展了一些细节网格,屏幕将开始显得混乱。

Its almost as if master/detail grids are possible, but they are not a generally a good idea. 好像主/详细网格是可能的,但通常不是一个好主意。 Look around for successful, usable software that uses master/detail grids. 四处寻找使用主/详细网格的成功,可用的软件。 They are relatively rare on the evolutionary landscape, with the exception of Windows Explorer style interfaces. 除了Windows Explorer风格的界面外,它们在进化环境中相对较少。 They are not very common in Apple land, which is a hint that they are perhaps not optimum from a users point of view. 它们在Apple领域不是很常见,这暗示着从用户角度看它们可能不是最佳的。

And the really odd thing is that relatively experienced programmers tend to like master/detail grids - a lot. 真正奇怪的是,相对经验丰富的程序员往往非常喜欢主/细节网格。 They seem to have an intuitive appeal to programmers because they model the data so well. 它们似乎对程序员具有直观吸引力,因为它们对数据的建模是如此出色。 But from a users point of view, they would rather have a flat grid that loads up quickly, and a Properties panel below the grid which always displays details of the current row we have selected. 但是从用户的角度来看,他们宁愿拥有一个可以快速加载的平面网格,并且在网格下方的“ Properties面板始终显示我们选择的当前行的详细信息。

Yes, this answer doesn't address your exact question - it's too philosophical for that - but it sure as hell would solve your speed problems in one neat refactor, and potentially make your app more intuitive for your users. 是的,这个答案并不能解决您的确切问题-太哲学了-但是可以肯定,地狱将在一个整洁的重构中解决您的速度问题,并有可能使您的应用对用户来说更加直观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM