C# Silverlight - 何时创建资源字典? [英] C# Silverlight - When do Resource Dictionaries get created?

查看:50
本文介绍了C# Silverlight - 何时创建资源字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个 Silverlight 4 项目,在该项目中出现内存泄漏,因为每次加载一些 xaml 视图时,它们都会创建自己的类实例,然后与事件触发器挂钩并且不会收集垃圾.在这种情况下,事实证明删除事件侦听器太棘手了,我无法以这种方式处理它.因此,我希望每次加载 xaml 视图时都重新使用该类的实例.

I am working on a Silverlight 4 project where I get a memory leak because every time some xaml views are loaded they create their own instance of a class that then gets hooked with event triggers and don't get garbage collected. Removing the event listeners has proved too tricky under the circumstances for me to handle it that way. So Instead I want the instance of the class to be re-used every time the xaml view loads.

为此,我计划向发生此错误的每个项目添加一个资源字典.但我担心的是,我不知道何时会创建此资源字典.当解决方案第一次加载时,它会加载项目然后是资源字典,立即创建类以供使用吗?或者它会等到有什么需要它,然后再创建它.实际发生的事情会创建一个类的副本,供访问它的每个人重复使用(而不是另一个副本)?

To do this I was planning on adding a Resource Dictionary to each project that this error is occurring in. My concern though is that I don't know when this Resource Dictionary will be created. When the solution first loads will it load the project then the resource dictionary, immediately creating the class for use? Or will it wait until something asks for it and then create it. Will what actually happens create a copy of the class that gets re-used by everyone that accesses it(as in not another copy)?

我也意识到我可以将资源放在 app.xaml 中,但我需要添加许多这样的类,它真的会把不应该被这个问题困扰的区域变得混乱.

I also realize I can place the resources in app.xaml but I need to add many such classes and it will really clutter up the area which shouldn't be bothered with this problem.

推荐答案

使用 Christian Moser 的 SharedResourceDictionary以避免加载字典本身或其包含的任何内容的第二个副本.这为我们节省了大量控件的加载时间.

Use Christian Moser's SharedResourceDictionary to avoid loading second copies of the dictionary itself or anything it contains. This has saved us a long amount of load time in many controls.

这篇关于C# Silverlight - 何时创建资源字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆