ViewModel 自动绑定
约 104 字小于 1 分钟
PrismViewModel自动绑定MVVM
2025-06-10
自动加载 ViewModel
XXXView.xaml:
prism:ViewModelLocator.AutoWireViewModel="True"在 View 的 xaml 中添加上面的属性,可以自动加载 ViewModel, 无需手动指定。
XXXViewMode.cs:
public class XXXViewModel : BindableBase
{
public XXXViewModel()
{
}
}继承 BindableBase 类,实现 ViewModel,其中 BindableBase 是 Prism 提供的一个实现了 INotifyPropertyChanged 接口的基类。
手动加载 ViewModel
XXXView.xaml.cs:
public XXXView()
{
InitializeComponent();
this.DataContext = new XXXViewModel();
}