【发布时间】:2011-08-15 15:10:00
【问题描述】:
我有一个在多个视图中引用的类,但我希望它们之间只共享一个类的实例。我已经这样实现了我的课程:
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}。
【问题讨论】:
标签: wpf mvvm resourcedictionary