im passing my webservice url to my silverlight application via the parameters.
when my application launches it creates the viewmodel for the mainpage before it application_startup event is fired.
in my viewmodel constructor i have a call to my serviceagent to load some data from the webservice, but the webservice url is not initialised yet due the the viewmodel being constructed before the application_startup event is raised. whats the best way to get around this. Its a friday evening and my brain seems to be pretty fried trying to think of a good solution.
An instance of the ViewModelLocator is created in the app.xaml
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
Then in the ViewModelLocator constructor there is a call to create the main page
public ViewModelLocator()
{
CreateMain();
}
public static void CreateMain()
{
if (_main == null) _main = new MainViewModel();
}
and in my MainViewModel i make a call to my serviceagent
public MainViewModel() : this(new MyServiceAgent()) { }
public MainViewModel(IMyServiceAgent myServiceAgent)
{
if (IsInDesignMode)
{
}
else
{
ServiceAgent = myServiceAgent;
ServiceAgent.GetData();
RegisterMessageListeners();
WireUpCommands();
}
}
App.xaml.cs
public App()
{
Startup += Application_Startup;
Exit += Application_Exit;
UnhandledException += Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
if (e.InitParams != null && e.InitParams.Count > 0)
ParseInitParams(e.InitParams);
RootVisual = new MainPage();
DispatcherHelper.Initialize();
}
Cheeers
to fix my issue i had to remove the line of code from the viewmodellocator constructor that was initialising the MainViewModel