【发布时间】:2019-04-28 02:42:29
【问题描述】:
我有某种后台任务会定期执行,或者如果有人手动启动它。
现在我想要某种进度/结果视图,它显示已处理的数据。窗口应该一直显示。
问题是,每次后台任务启动时都会创建一个新的数据模型实例。那么即使重新实例化Model,如何保持Model->ViewModel的Binding呢?
我创建了一些非常基本的示例作为展示:
查看:
<Window x:Class="View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:View"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="300" Background="Black">
<Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" Foreground="White" HorizontalAlignment="Center" Content="{Binding MainModelText}"/>
</Grid>
</Grid>
视图模型:
public class ViewModel : INotifyPropertyChanged
{
MainModel _MainModel;
string _MainModelText;
public string MainModelText
{
get { return this._MainModelText; }
set
{
this._MainModelText = value;
OnNotifyPropertyChanged("MainModelText");
}
}
public ViewModel(MainModel mainModel)
{
this._MainModel = mainModel;
this._MainModel.PropertyChanged += _MainModel_PropertyChanged;
}
private void _MainModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if(string.Equals(e.PropertyName, "SomeText"))
{
this.MainModelText = _MainModel.SomeText + new Random().Next(1000);
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnNotifyPropertyChanged(string propName)
{
if(this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
型号:
public class MainModel : INotifyPropertyChanged
{
string _SomeText;
public string SomeText
{
get { return this._SomeText; }
set
{
this._SomeText = value;
OnNotifyPropertyChanged("SomeText");
}
}
public MainModel()
{
this.SomeText = "Its MainModel!";
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnNotifyPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
业务逻辑
public class Logic
{
MainModel _MainModel;
View.MainWindow _Window;
public Logic()
{
this._MainModel = new MainModel();
_Window = new View.MainWindow(new ViewModel(_MainModel));
}
public void Start()
{
_Window.ShowDialog();
}
public void NewAll()
{
this._MainModel = new MainModel();
//working...
this._MainModel.SomeText = "Finished";
}
}
显然“完成”没有显示在窗口上,因为它设置为另一个 MainModel 实例。
那么如何更新ViewModel中的Modelreference呢? 这样的最佳实践是什么?
编辑:
public class Logic
{
MainModel _MainModel;
ViewModel _ViewModel;
View.MainWindow _Window;
public Logic()
{
this._MainModel = new MainModel();
this._ViewModel = new ViewModel(this._MainModel);
_Window = new View.MainWindow(this._ViewModel);
}
public void Start()
{
_Window.ShowDialog();
}
public void NewAll()
{
this._MainModel = new MainModel();
this._ViewModel.Reload(this._MainModel);
//working...
this._MainModel.SomeText = "Finished";
}
}
在虚拟机中添加:
internal void Reload(MainModel mainModel)
{
this._MainModel = mainModel;
this._MainModel.PropertyChanged -= _MainModel_PropertyChanged;
this._MainModel.PropertyChanged += _MainModel_PropertyChanged;
}
【问题讨论】:
-
你不能只更新模型的属性而不是重新创建整个视图模型吗?
-
您的视图模型应该有一个公共的
ModelINPC 属性。当你给它一个新的Model时,在setter中提出PropertyChanged("Model")。在视图中,绑定到{Binding Model.SomeText}。 -
并删除您的
Logic课程。这在其中没有任何作用。您的主要视图模型“就是程序”。 MainWindow 在其构造函数中(或在 XAML 中)创建主视图模型的实例,并从那里开始在主视图模型中负责。主虚拟机负责后台进程,主虚拟机使用它来更新自己的一些属性。任何其他 VM 都是主 VM 的子级。 -
Logic类是其中的主要部分,视图应该只是某种用户友好的交互(也可以通过控制台进行交互)。我对此的理解是,VM 位于模型和视图之间并且控制视图并且只负责这个,没有别的,所以主程序是 - 我在这个例子中调用的 -Logic类并且正在做主要工作。如果新的执行开始启用某些用户交互,则只会通知 VM --> View。因此,VM 获取新模型并在必要时向用户显示一些内容 - 仅此而已。还是我错了? -
请参阅编辑。它的工作,但不知道这是否是一种有效的方法。