开发者

Silverlight application resources for global access to a singleton

I have a singleton which once hit will load user profile information, I want to make it an application level resource in my SL3 application so that elements across the application can bind to it.

My code version of the instantiaion is a simple

UserProfile x = UserProfile.GetInstance();

I want to be able to do this in xaml in the app.xaml file and in WPF we have the ObjectDataProvider so I can express something like

<ObjectDataProvider MethodName="GetInstance" 
ObjectTyp开发者_开发问答e="{x:Type local:UserProfile}" x:Key="CurrentUserProfile"/>

I am struggling to find the right implementation for this in SL3.


As you point out Silverlight doesn't have ObjectDataProvider. If you need a feature it provides such as lazy instantiation you'll need to build a class of your own to handle it. If you don't actually need these features then simply add an instance of the UserProfile to App.Resources on start up:-

 private void Application_Startup(object sender, StartupEventArgs e)
 {
    Resources.Add("CurrentUserProfile", UserProfile.GetInstance());
    RootVisual = new MainPage();
 }


Silverlight has no ObjectDataProvider.

That said,you can use the DataContext of your Silverlight object.....

Application.DataContext = UserProfile.GetInstance();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜