MVVM
<h1>解决的问题:降低耦合,逻辑重用。</h1>
<h3>界面</h3>
<pre><code class="language-csharp">&lt;Grid.ColumnDefinitions&gt;
&lt;ColumnDefinition&gt;&lt;/ColumnDefinition&gt;
&lt;ColumnDefinition&gt;&lt;/ColumnDefinition&gt;
&lt;/Grid.ColumnDefinitions&gt;
&lt;StackPanel Grid.Column=&quot;0&quot;&gt;
&lt;TextBox Text=&quot;{Binding Path=addModel.value1}&quot;&gt;&lt;/TextBox&gt;
&lt;TextBox Text=&quot;{Binding Path=addModel.value2}&quot;&gt;&lt;/TextBox&gt;
&lt;TextBox Text=&quot;{Binding Path=addModel.value3}&quot;&gt;&lt;/TextBox&gt;
&lt;Button Command=&quot;{Binding ButtonCommand}&quot; Height=&quot;30&quot; Content=&quot;123&quot;&gt;&lt;/Button&gt;
&lt;/StackPanel&gt;
&lt;ListBox Grid.Column=&quot;1&quot; ItemsSource=&quot;{Binding FistList}&quot;&gt;&lt;/ListBox&gt;</code></pre>
<h3>Model层</h3>
<p>在Model层需要实现接口INotifyPropertyChanged,以便在属性变化时通知界面刷新:</p>
<pre><code class="language-csharp">using System.ComponentModel;
public class AddModel : INotifyPropertyChanged
{
private double _value1;
public double value1
{
get { return _value1; }
set
{
_value1 = value;
OnPropertyChanged(nameof(value1));
}
}
private double _value2;
public double value2
{
get { return _value2; }
set
{
_value2 = value;
OnPropertyChanged(nameof(value2));
}
}
private double _value3;
public double value3
{
get { return _value3; }
set
{
_value3 = value;
OnPropertyChanged(nameof(value3));
}
}
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}</code></pre>
<h3>ViewModel层</h3>
<p>在ViewModel层,主要是暴露数据和命令给View使用:</p>
<pre><code class="language-csharp">using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
public class AddViewModel
{
public AddModel addModel { get; set; } = new AddModel();
public ICommand ButtonCommand { get; set; }
public ObservableCollection&lt;string&gt; FistList { get; set; } = new ObservableCollection&lt;string&gt;();
public AddViewModel()
{
ButtonCommand = new Command(Add);
}
private void Add()
{
addModel.value3 = addModel.value1 + addModel.value2;
FistList.Add($&quot;{addModel.value3} = {addModel.value1} + {addModel.value2}&quot;);
}
}</code></pre>
<h3>Command 类</h3>
<p><code>Command</code>类用于实现ICommand接口,允许将方法绑定到控件的动作:</p>
<pre><code class="language-csharp">using System;
using System.Windows.Input;
public class Command : ICommand
{
public Action A { get; set; }
public Command(Action B)
{
A = B;
}
public event EventHandler? CanExecuteChanged;
public bool CanExecute(object? parameter)
{
return true;
}
public void Execute(object? parameter)
{
A?.Invoke();
}
}</code></pre>
<h3>总结</h3>
<ol>
<li><strong>Model</strong>负责数据存储,并在数据变化时通知绑定的视图更新。</li>
<li><strong>ViewModel</strong>负责定义暴露给视图的数据和命令,并处理业务逻辑。</li>
<li><strong>Command</strong>是用于把操作绑定到UI元素(如按钮)的接口实现。</li>
</ol>
<h3>提示</h3>
<ul>
<li><strong>View</strong>(即视图层) 应该通过数据绑定绑定到<code>ViewModel</code>中的属性和命令,这样就可以实现UI与数据和操作的解耦。</li>
<li>请确保添加必要的命名空间引用,如<code>System.ComponentModel</code>和<code>System.Windows.Input</code>。</li>
</ul>
<p>通过这些代码,你已经实现了一个遵循MVVM模式的基本结构。你可以在视图中通过数据绑定实现UI和业务逻辑的互动。如果有其他需要或者疑问,可以随时提出!</p>