如何在没有依赖注入的情况下从 2 个单独的 ViewModel 访问相同的 属性?
How do I access the same property from 2 separate ViewModels without dependency injection?
所以我以这种方式设置了我的应用程序。
我的 App.xaml
包含我的视图的数据模板 UserControl
s
<Application.Resources>
<ResourceDictionary>
<DataTemplate DataType="{x:Type vms:ProtectionViewModel}">
<view:ProtectionView />
</DataTemplate>
<DataTemplate DataType="{x:Type vms:CloudViewModel}">
<view:CloudView />
</DataTemplate>
</ResourceDictionary>
</Application.Resources>
然后我有我的 MainWindow.xaml
,它由 2 个按钮和一个 ContentPresenter
组成
<RadioButton Content="Y"
FontSize="16"
Foreground="LightGray"
Padding="0,0,0,1"
Command="{Binding ShowProtectionViewCommand}"/>
<RadioButton Content="Z"
FontSize="16"
Foreground="LightGray"
Padding="0,0,0,1"
Command="{Binding ShowCloudViewCommand}"/>
<ContentPresenter Content="{Binding CurrentView}" />
现在,当我单击其中一个按钮时,它会将 CurrentView
更改为 ViewModel,然后根据 ViewModel 更改视图,如先前在 App.xaml
[=26 中所示=]
通过这样做
private object _currentView;
public object CurrentView
{
get { return _currentView; }
set
{
_currentView = value;
OnPropertyChanged();
}
}
public ProtectionViewModel ProtectionViewModel { get; set; } = new ProtectionViewModel();
public CloudViewModel CloudViewModel { get; set; } = new CloudViewModel();
ShowProtectionViewCommand = new RelayCommand(o => { CurrentView = ProtectionViewModel; });
ShowCloudViewCommand = new RelayCommand(o => { CurrentView = CloudViewModel; });
一切正常,我可以切换视图并且它保持相同的 DataContext,这很棒。问题是,如果我想要一个可以从多个 ViewModel 访问的全局 属性,我该怎么办?
查看我发现的这张图片 over here
我想按照这种风格创建一些东西,但我不知道如何实现“通用 VM”。
我所有的视图都有自己的 ViewModel,MainWindow
有 MainViewModel
,ProtectionView
有 ProtectionViewModel
等
我认为不需要答案,但我们 运行 无法在问题下发表评论 space 所以让我说明一下。
这样做 像这样(你必须假装我实现了 INotifyPropertyChanged
)
public class CommonVm : INotifyPropertyChanged
{
// Static instance property
public static CommonVm Instance { get; } = new CommonVm();
// Non static properties against which you can bind.
// Imagine I wrote full implementations with INPC
public int PropertyA { get; set; }
public string PropertyB { get; set; }
}
然后
public class CloudViewModel
{
public CommonVm CommonObject => CommonVm.Instance;
}
然后在 XAML 中假设您的 DataContext 是 CloudViewModel
的一个实例
<TextBox Text="{Binding CommonObject.PropertyB}"/>
不管你想说什么,它肯定不是依赖注入
所以我以这种方式设置了我的应用程序。
我的 App.xaml
包含我的视图的数据模板 UserControl
s
<Application.Resources>
<ResourceDictionary>
<DataTemplate DataType="{x:Type vms:ProtectionViewModel}">
<view:ProtectionView />
</DataTemplate>
<DataTemplate DataType="{x:Type vms:CloudViewModel}">
<view:CloudView />
</DataTemplate>
</ResourceDictionary>
</Application.Resources>
然后我有我的 MainWindow.xaml
,它由 2 个按钮和一个 ContentPresenter
<RadioButton Content="Y"
FontSize="16"
Foreground="LightGray"
Padding="0,0,0,1"
Command="{Binding ShowProtectionViewCommand}"/>
<RadioButton Content="Z"
FontSize="16"
Foreground="LightGray"
Padding="0,0,0,1"
Command="{Binding ShowCloudViewCommand}"/>
<ContentPresenter Content="{Binding CurrentView}" />
现在,当我单击其中一个按钮时,它会将 CurrentView
更改为 ViewModel,然后根据 ViewModel 更改视图,如先前在 App.xaml
[=26 中所示=]
通过这样做
private object _currentView;
public object CurrentView
{
get { return _currentView; }
set
{
_currentView = value;
OnPropertyChanged();
}
}
public ProtectionViewModel ProtectionViewModel { get; set; } = new ProtectionViewModel();
public CloudViewModel CloudViewModel { get; set; } = new CloudViewModel();
ShowProtectionViewCommand = new RelayCommand(o => { CurrentView = ProtectionViewModel; });
ShowCloudViewCommand = new RelayCommand(o => { CurrentView = CloudViewModel; });
一切正常,我可以切换视图并且它保持相同的 DataContext,这很棒。问题是,如果我想要一个可以从多个 ViewModel 访问的全局 属性,我该怎么办?
查看我发现的这张图片 over here
我想按照这种风格创建一些东西,但我不知道如何实现“通用 VM”。
我所有的视图都有自己的 ViewModel,MainWindow
有 MainViewModel
,ProtectionView
有 ProtectionViewModel
等
我认为不需要答案,但我们 运行 无法在问题下发表评论 space 所以让我说明一下。
这样做 像这样(你必须假装我实现了 INotifyPropertyChanged
)
public class CommonVm : INotifyPropertyChanged
{
// Static instance property
public static CommonVm Instance { get; } = new CommonVm();
// Non static properties against which you can bind.
// Imagine I wrote full implementations with INPC
public int PropertyA { get; set; }
public string PropertyB { get; set; }
}
然后
public class CloudViewModel
{
public CommonVm CommonObject => CommonVm.Instance;
}
然后在 XAML 中假设您的 DataContext 是 CloudViewModel
<TextBox Text="{Binding CommonObject.PropertyB}"/>
不管你想说什么,它肯定不是依赖注入