C# Silverlight - 何时创建资源字典?
我正在开发一个 Silverlight 4 项目,在该项目中我遇到了内存泄漏,因为每次加载一些 xaml 视图时,它们都会创建自己的类实例,然后该实例会与事件触发器挂钩,并且不会被垃圾收集。事实证明,在这种情况下删除事件侦听器对于我来说太棘手了,无法以这种方式处理。因此,我希望每次加载 xaml 视图时都重新使用该类的实例。
为此,我计划向发生此错误的每个项目添加资源字典。但我担心的是,我不知道何时创建此资源字典。当解决方案首次加载时,它会加载项目,然后加载资源字典,立即创建要使用的类吗?或者它会等到有需要时再创建它。实际发生的情况是否会创建该类的副本,并由访问该类的每个人重用(而不是在另一个副本中)?
我还意识到我可以将资源放在 app.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)?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 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.