views:

725

answers:

2

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" 
ObjectType="{x:Type local:UserProfile}" x:Key="CurrentUserProfile"/>

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

A: 

Silverlight has no ObjectDataProvider.

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

Application.DataContext = UserProfile.GetInstance();
Muad'Dib
What happens if you then need to assign DataContext on UserControls etc to some real application data as opposed to the peripheral User info?
AnthonyWJones
+2  A: 

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();
 }
AnthonyWJones
I was really hoping to accomplish this in pure xaml but if I dont get a xaml based solution from someone soon then I will give you the check mark...thanks.
keithwarren7