我正在尝试开发一个简单的MVVM项目,它有两个窗口:
第一个窗口是文本编辑器,我在其中绑定一些属性,如:FontSize或BackgroundColor:
<TextBlock FontSize="{Binding EditorFontSize}"></TextBlock>
它DataContext是MainWindowViewModel:
public class MainWindowViewModel : BindableBase
{
public int EditorFontSize
{
get { return _editorFontSize; }
set { SetProperty(ref _editorFontSize, value); }
}
.....
Run Code Online (Sandbox Code Playgroud)
<Slider Maximum="30" Minimum="10" Value="{Binding EditorFontSize }" ></Slider>
它DataContext是OptionViewModel:
public class OptionViewModel: BindableBase
{
public int EditorFontSize
{
get { return _editorFontSize; }
set { SetProperty(ref _editorFontSize, value); }
}
.....
Run Code Online (Sandbox Code Playgroud)
我的问题是我必须在选项窗口中获取滑块的值,然后我必须使用此值修改我的TextBlock的FontSize属性.但我不知道如何将OptionViewModel的字体大小发送到MainViewModel.
我认为我应该使用:
我希望你能帮助我.这是我的第一个MVVM项目,英语不是我的主要语言:S
谢谢
另一种选择是将这种"共享"变量存储在SessionContext某种类中:
public interface ISessionContext: INotifyPropertyChanged
{
int EditorFontSize { get;set; }
}
Run Code Online (Sandbox Code Playgroud)
然后,将它注入到您的视图模型中(您正在使用依赖注入,对吗?)并注册到该PropertyChanged事件:
public class MainWindowViewModel
{
public MainWindowViewModel(ISessionContext sessionContext)
{
sessionContext.PropertyChanged += OnSessionContextPropertyChanged;
}
private void OnSessionContextPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "EditorFontSize")
{
this.EditorFontSize = sessionContext.EditorFontSize;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在视图模型和许多要点之间进行通信的方法有很多,最重要的是这一点.你可以看到它是如何完成的:
在我看来,最好的方法是使用框架EventAggregator模式Prism.Prism简化了MVVM模式. 但是,如果您还没有使用过Prism,可以使用Rachel Lim的教程--Rachel Lim的EventAggregator模式的简化版本..我强烈推荐你Rachel Lim的方法.
如果您使用Rachel Lim的教程,那么您应该创建一个公共类:
public static class EventSystem
{...Here Publish and Subscribe methods to event...}
Run Code Online (Sandbox Code Playgroud)
并将事件发布到您的OptionViewModel:
eventAggregator.GetEvent<ChangeStockEvent>().Publish(
new TickerSymbolSelectedMessage{ StockSymbol = “STOCK0” });
Run Code Online (Sandbox Code Playgroud)
然后你在另一个你的构造函数中订阅MainViewModel一个事件:
eventAggregator.GetEvent<ChangeStockEvent>().Subscribe(ShowNews);
public void ShowNews(TickerSymbolSelectedMessage msg)
{
// Handle Event
}
Run Code Online (Sandbox Code Playgroud)
Rachel Lim的简化方法是我见过的最好的方法.但是,如果您想创建一个大型应用程序,那么您应该阅读Magnus Montin和CSharpcorner的这篇文章,并举例说明.
更新:对于版本Prism低于5的CompositePresentationEvent版本在版本6中已经过折旧并完全删除,因此您需要将其更改为PubSubEvent其他所有版本保持不变的版本.