C# Silverlight应用程序资源,用于全局访问单例
我有一个singleton,一旦点击它就会加载用户配置文件信息,我想让它成为我的SL3应用程序中的应用程序级资源,这样应用程序中的元素就可以绑定到它 我的实例代码版本是一个简单的C# Silverlight应用程序资源,用于全局访问单例,c#,silverlight,data-binding,xaml,singleton,C#,Silverlight,Data Binding,Xaml,Singleton,我有一个singleton,一旦点击它就会加载用户配置文件信息,我想让它成为我的SL3应用程序中的应用程序级资源,这样应用程序中的元素就可以绑定到它 我的实例代码版本是一个简单的 UserProfile x = UserProfile.GetInstance(); 我希望能够在app.xaml文件中的xaml中实现这一点,并且在WPF中我们有ObjectDataProvider,因此我可以表达如下内容 <ObjectDataProvider MethodName="GetInstance
UserProfile x = UserProfile.GetInstance();
我希望能够在app.xaml文件中的xaml中实现这一点,并且在WPF中我们有ObjectDataProvider,因此我可以表达如下内容
<ObjectDataProvider MethodName="GetInstance"
ObjectType="{x:Type local:UserProfile}" x:Key="CurrentUserProfile"/>
我正努力在SL3中找到正确的实现方法。
也就是说,您可以使用Silverlight对象的DataContext
Application.DataContext = UserProfile.GetInstance();
正如您所指出的,Silverlight没有
ObjectDataProvider
。如果您需要它提供的特性,比如惰性实例化,那么您需要构建自己的类来处理它。如果您实际上不需要这些功能,只需在启动时将UserProfile
的实例添加到App.Resources
:-
private void Application_Startup(object sender, StartupEventArgs e)
{
Resources.Add("CurrentUserProfile", UserProfile.GetInstance());
RootVisual = new MainPage();
}
如果您需要将UserControls等上的DataContext分配给一些实际的应用程序数据,而不是外围用户信息,会发生什么情况?我真的希望用纯xaml实现这一点,但是如果我不能很快从某人那里获得基于xaml的解决方案,那么我将给您打勾…谢谢。