简体   繁体   English

导航到某些页面后性能下降

[英]Performance degrades after navigating to some pages

i have this weird problem with my WinRT app (Windows Phone only). 我的WinRT应用程序有这个奇怪的问题(仅Windows Phone)。 The app is made of multiple pages, it uses an animation to show a side menu which mimics the hamburger menu in W10M. 该应用程序由多个页面组成,它使用动画来显示侧面菜单,该菜单类似于W10M中的汉堡菜单。 The animation and app performance are great when the app first launches, but after navigating to some pages (say 3 or 4 pages) the app performance gets worst and worst. 首次启动应用程序时,动画和应用程序性能非常好,但是导航到某些页面(例如3或4页)后,应用程序性能会越来越差。 I don't have any code that is running in the background on the UI thread or any other thread. 我没有UI线程或任何其他线程在后台运行的任何代码。

Is there something about navigation in WinRT i am missing? 我缺少WinRT中的导航功能吗? It is really weird and i don't know what is causing it. 这真的很奇怪,我不知道是什么原因造成的。

Here are some considerations that impact performance in XAML. 以下是一些影响XAML性能的注意事项。

  1. Depth of the visual tree. 可视树的深度。 You can drag on two controls, but those controls represent several nested controls - things like borders, buttons, and panels. 您可以在两个控件上拖动,但是这些控件代表多个嵌套控件-诸如边框,按钮和面板之类的东西。 Running your app and looking at it with the Live XAML Tree viewer can give you a hint if a control is more complex than you want. 运行控件并使用Live XAML Tree Viewer对其进行查看可以提示您控件是否比您想要的复杂。 Also, keep depth in mind when you build your UI. 另外,在构建UI时请牢记深度。 Favor simple controls like RelativePanel and standard, non-re-template control. 支持简单的控件,例如RelativePanel和标准的非重新模板控件。

  2. Construction versus loaded logic. 构造与加载逻辑。 It's not uncommon for developers not yet familiar with XAML to implement complex logic in inappropriate places. 对于尚不熟悉XAML的开发人员,在不适当的地方实现复杂的逻辑并不少见。 For example, you might, in the constructor of a sub-classed control, tell it to go get some data or test some expensive condition. 例如,您可能在子类控件的构造函数中,告诉它去获取一些数据或测试某些昂贵的条件。 Ideally, you will use Loaded of a control, or OnNavigatedTo of a Page if you have this type of logic, and use async if you have the option. 理想情况下,如果您具有这种类型的逻辑,则可以使用控件的Loaded或Page的OnNavigatedTo,而如果有此选项,则可以使用async

  3. Data binding. 数据绑定。 Data binding is awesome but it is also expensive. 数据绑定很棒,但也很昂贵。 You can use it in a few dozen places on your page an not likely to ever make a difference, but if you start to use it everywhere and for everything, it can be really expensive. 您可以在页面上的几十个地方使用它,这几乎不会有什么作用,但是如果您开始在任何地方使用它,那么它确实会很昂贵。 The easiest remediation is to reduce the bindings in your page. 最简单的补救措施是减少页面中的绑定。 Next, you can change your bindings from Mode=OneWay (which is the default) to Mode=OneTime wherever you can. 接下来,您可以将绑定从Mode=OneWay (默认设置)更改为Mode=OneTime Next, you can switch from {binding} to {x:bind} wherever possible. 接下来,您可以尽可能从{binding}切换到{x:bind}。 Remember, binding is not wrong, it's just expensive, so use it only when appropriate. 请记住,绑定并没有错,它只是很昂贵,因此仅在适当的时候使用它。

  4. Too many records. 记录太多。 If you have a repeater like a ListView or GridView then you might have too many items appearing at one time. 如果您有一个类似ListView或GridView的中继器,那么一次可能出现太多项目。 This is a big deal, especially on load. 这很重要,尤其是在负载方面。 It's rare that the real cost is fetching the data, it's typically rendering the data. 真正的花费很少是要获取数据,通常是渲染数据。 If your item data template is complex, then your visual tree depth may be multiplied several times with each record. 如果您的项目数据模板很复杂,那么可视化树的深度可能会与每条记录相乘数倍。 Loading fewer records at load and then showing more over time is one approach. 一种方法是在加载时加载较少的记录,然后随时间显示更多的记录。 Using x:Phase is another great approach. 使用x:Phase是另一种好方法。 Simplifying your item templates is another approach. 简化项目模板是另一种方法。 Minimizing data binding in your item templates can help. 最小化项目模板中的数据绑定会有所帮助。 But, the best you can do is to run the diagnostics tool and look to see what's causing it. 但是,您所能做的最好的就是运行诊断工具,并查看其原因。 It might not be your ListView at all. 可能根本不是您的ListView。

  5. Unused Visual States. 未使用的视觉状态。 Developers are smart to use visual states. 开发人员很聪明地使用视觉状态。 However, some states they define are for edge cases and show only in certain circumstances. 但是,它们定义的某些状态是针对极端情况的,并且仅在某些情况下显示。 The XAML for those views may never appear, so they mark their Visibility to Collapsed. 这些视图的XAML可能永远不会出现,因此它们会将其可见性标记为“已折叠”。 This is okay. 没关系 But if you go one more step and use x:DeferLoadingStrategy you can prevent the cost of that XAML parsing and rendering at startup. 但是,如果再走一步并使用x:DeferLoadingStrategy,则可以避免在启动时进行XAML解析和渲染的开销。 When the visual state reveals that XAML, it will automatically be realized by the platform. 当可视状态显示XAML时,平台会自动实现。 You're the winner, so is the user. 您是赢家,用户也是。

So, 5 helpful tips to help your pages load faster. 因此,有5条有用的提示可帮助您更快地加载页面。

Odds are, your reason is going to be something else :) 奇怪的是,您的原因将是其他原因:)

ONE MORE THING 还有一件事

Are you allowing your pages to be removed from memory? 您是否允许从内存中删除页面? Is it possible that in your implicit code in your code-behind you are subscribing to a static or global event? 是否有可能在您的代码背后的隐式代码中订阅了静态或全局事件? Maybe you need to unsubscribe in your OnNavigatedFrom override. 也许您需要取消订阅OnNavigatedFrom重写。 Since the problem gets worse over time, it sounds like it might be a memory issue. 由于问题随着时间的推移而变得越来越严重,因此听起来好像是内存问题。 Though, that is just a guess. 虽然,这只是一个猜测。 It's a common C# slip up. 这是常见的C#错误。

Best of luck 祝你好运

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

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