我有一个在多个视图中引用的类,但我希望它们之间只共享该类的一个实例。我已经像这样实现了我的课程:
using System;
public class Singleton
{
private static Singleton instance;
private Singleton() {}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
有没有办法可以将 Singleton.Instance 作为资源添加到我的资源字典中?
我想写一些类似的东西
<Window.Resources>
<my:Singleton.Instance x:Key="MySingleton"/>
</Window.Resources>
而不必写{x:static my:Singleton.Instance}
每次我需要参考的时候。
在 XAML 中是可能的:
<!-- assuming the 'my' namespace contains your singleton -->
<Application.Resources>
<x:StaticExtension Member="my:Singleton.Instance" x:Key="MySingleton"/>
</Application.Resources>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)