【问题标题】:MVVM: Binding changable ModelMVVM:绑定可变模型
【发布时间】: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;
}

【问题讨论】:

  • 你不能只更新模型的属性而不是重新创建整个视图模型吗?
  • 您的视图模型应该有一个公共的Model INPC 属性。当你给它一个新的Model时,在setter中提出PropertyChanged("Model")。在视图中,绑定到{Binding Model.SomeText}
  • 并删除您的Logic 课程。这在其中没有任何作用。您的主要视图模型“就是程序”。 MainWindow 在其构造函数中(或在 XAML 中)创建主视图模型的实例,并从那里开始在主视图模型中负责。主虚拟机负责后台进程,主虚拟机使用它来更新自己的一些属性。任何其他 VM 都是主 VM 的子级。
  • Logic 类是其中的主要部分,视图应该只是某种用户友好的交互(也可以通过控制台进行交互)。我对此的理解是,VM 位于模型和视图之间并且控制视图并且只负责这个,没有别的,所以主程序是 - 我在这个例子中调用的 - Logic 类并且正在做主要工作。如果新的执行开始启用某些用户交互,则只会通知 VM --> View。因此,VM 获取新模型并在必要时向用户显示一些内容 - 仅此而已。还是我错了?
  • 请参阅编辑。它的工作,但不知道这是否是一种有效的方法。

标签: c# wpf mvvm


【解决方案1】:

除非您有充分的理由不通过 VM 属性提供 MainModel,否则我建议您这样做。然后,您可以直接跳过 SomeText 属性并直接绑定到 Model.SomeText,因为您的模型已经实现了 INotifyPropertyChanged。

如果你想加载一个全新的模型实例,你可以直接在 VM 属性上设置它。

如果您不想为视图提供完整的模型,那么您的方法是可行的。但我也会从模型构造函数中调用 ReloadModel,以便将代码放在一个地方。此外,您应该在将旧模型更新为新模型之后从旧模型中取消注册 PropertyChanged 事件(首先检查 _mainModel == null)。

在您的 PropertyChanged 调用中,考虑使用 nameof(SomeProperty) 而不是像现在这样的固定字符串“SomeProperty”。如果您在某个时候重命名该属性,则代码不会中断。

看看 MVVM Light,该框架将使您免于一遍又一遍地编写一些样板代码,并且它们提供的 Messenger 是从模型向 VM 发出信号的好方法。它将这个示例简化为几行代码。

【讨论】:

    猜你喜欢
    • 2012-07-31
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    相关资源
    最近更新 更多