C# 应用程序启动时实例化ViewModel
我正在使用MVVMLight创建一个应用程序 在我的应用程序中,我有一个“目录”视图和一个C# 应用程序启动时实例化ViewModel,c#,mvvm,mvvm-light,C#,Mvvm,Mvvm Light,我正在使用MVVMLight创建一个应用程序 在我的应用程序中,我有一个“目录”视图和一个下载视图,每个视图都与自己的ViewModelLocator中声明的ViewModel相关: public class ViewModelLocator { public ViewModelLocator() { ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); SimpleIo
下载
视图,每个视图都与自己的ViewModelLocator中声明的ViewModel相关:
public class ViewModelLocator
{
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<CatalogViewModel>();
SimpleIoc.Default.Register<CreatorViewModel>();
SimpleIoc.Default.Register<DownloadsViewModel>();
Messenger.Default.Register<NotificationMessage>(this, NotifyUserMethod);
}
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
public CatalogViewModel Catalog
{
get
{
return ServiceLocator.Current.GetInstance<CatalogViewModel>();
}
}
public DownloadsViewModel Downloads
{
get
{
return ServiceLocator.Current.GetInstance<DownloadsViewModel>();
}
}
public CreatorViewModel Creator
{
get
{
return ServiceLocator.Current.GetInstance<CreatorViewModel>();
}
}
private void NotifyUserMethod(NotificationMessage message )
{
MessageBox.Show(message.Notification);
}
public static void Cleanup()
{
// TODO Clear the ViewModels
}
}
公共类ViewModelLocator
{
公共ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(()=>SimpleIoc.Default);
SimpleIoc.Default.Register();
SimpleIoc.Default.Register();
SimpleIoc.Default.Register();
SimpleIoc.Default.Register();
Messenger.Default.Register(这是NotifyUserMethod);
}
公共主视图模型主视图
{
得到
{
返回ServiceLocator.Current.GetInstance();
}
}
公共目录视图模型目录
{
得到
{
返回ServiceLocator.Current.GetInstance();
}
}
公共下载查看模型下载
{
得到
{
返回ServiceLocator.Current.GetInstance();
}
}
公共创建者视图模型创建者
{
得到
{
返回ServiceLocator.Current.GetInstance();
}
}
私有void NotifyUserMethod(NotificationMessage)
{
MessageBox.Show(message.Notification);
}
公共静态无效清除()
{
//要清除ViewModels,请执行以下操作:
}
}
我计划使用消息传递将我的selectedCatalogItems
发送到downloads VM中的集合,但它仅在用户首次打开下载视图时才起作用。在另一种情况下,下载视图模型还没有创建,消息也没有发布
有没有办法在应用程序启动时调用Downdload VM的构造函数,或者我应该使用专用类来存储下载列表?在应用程序生命周期的早期获取视图模型的实例,因为服务定位器将在其缓存中保留它的实例
public class ViewModelLocator {
static ViewModelLocator() {
SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<CatalogViewModel>();
SimpleIoc.Default.Register<CreatorViewModel>();
SimpleIoc.Default.Register<DownloadsViewModel>();
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
//Default instance
//Done so an instance will be generated and held in cache
var defaultDownloadsViewModel = ServiceLocator.Current.GetInstance<DownloadsViewModel>();
}
public ViewModelLocator(){
Messenger.Default.Register<NotificationMessage>(this, NotifyUserMethod);
}
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
public CatalogViewModel Catalog
{
get
{
return ServiceLocator.Current.GetInstance<CatalogViewModel>();
}
}
public DownloadsViewModel Downloads
{
get
{
return ServiceLocator.Current.GetInstance<DownloadsViewModel>();
}
}
private void NotifyUserMethod(NotificationMessage message )
{
MessageBox.Show(message.Notification);
}
public static void Cleanup()
{
// TODO Clear the ViewModels
}
}
公共类ViewModelLocator{
静态ViewModelLocator(){
SimpleIoc.Default.Register();
SimpleIoc.Default.Register();
SimpleIoc.Default.Register();
SimpleIoc.Default.Register();
ServiceLocator.SetLocatorProvider(()=>SimpleIoc.Default);
//默认实例
//这样将生成一个实例并保存在缓存中
var defaultDownloadsViewModel=ServiceLocator.Current.GetInstance();
}
公共ViewModelLocator(){
Messenger.Default.Register(这是NotifyUserMethod);
}
公共主视图模型主视图
{
得到
{
返回ServiceLocator.Current.GetInstance();
}
}
公共目录视图模型目录
{
得到
{
返回ServiceLocator.Current.GetInstance();
}
}
公共下载查看模型下载
{
得到
{
返回ServiceLocator.Current.GetInstance();
}
}
私有void NotifyUserMethod(NotificationMessage)
{
MessageBox.Show(message.Notification);
}
公共静态无效清除()
{
//要清除ViewModels,请执行以下操作:
}
}
您还可以在应用程序的生命周期内注册视图模型的实例。这样,当您调用get instance时,每次都会得到相同的视图模型。听起来很棒!你对如何做到这一点有什么建议吗?由于我依赖于处理视图模型实例的mvvm light,所以我不知道应该在何处以及如何调用视图模型的构造函数。注册其他视图模型时也是在同一个位置。viewmodel定位器是否具有静态构造函数?您的代码段很不连贯,因此无法告诉您my Locator的构造函数,与mvvm light中的默认值相同,因此它不是静态的:公共类ViewModelLocator{public ViewModelLocator(){ServiceLocator.SetLocatorProvider(()=>SimpleIoc.default);SimpleIoc.default.Register();SimpleIoc.Default.Register();SimpleIoc.Default.Register();}