Silverlight启动参数传递给viewmodel
我通过参数将我的 webservice url 传递到我的 silverlight 应用程序。
当我的应用程序启动时,它会在触发 application_startup 事件之前为主页创建视图模型。
在我的 viewmodel 构造函数中,我调用了 serviceagent 以从 web 服务加载一些数据,但由于在引发 application_startup 事件之前构建了 viewmodel,因此 webservice url 尚未初始化。解决这个问题的最好方法是什么。这是一个星期五的晚上,我的大脑似乎非常忙碌,试图想出一个好的解决方案。
在 app.xaml 中创建 ViewModelLocator 的实例
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
然后在 ViewModelLocator 构造函数中调用创建主页
public ViewModelLocator()
{
CreateMain();
}
public static void CreateMain()
{
if (_main == null) _main = new MainViewModel();
}
,并在我的 MainViewModel 中调用我的服务代理
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了解决我的问题,我必须从初始化 MainViewModel 的 viewmodellocator 构造函数中删除代码行
to fix my issue i had to remove the line of code from the viewmodellocator constructor that was initialising the MainViewModel