工程改Mvvm
- 导入CommunityToolKit vs2017只能导入7
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input;namespace WpfApp1.viewmodels {public partial class TestMvvm:ObservableObject{private string _name = "old";public string Name{get => _name;set => SetProperty(ref _name, value);}public IRelayCommand ChangeText { get; }public TestMvvm() { ChangeText = new RelayCommand(()=> { Name = "new"; }); }} }
属性得自己写 用不了[ObversableProperty] RelayCommand得自己实现
改StartUpUri
StartupUri="views/MainWindow.xaml">
用斜线不用点
加DataContext
<Window.DataContext><local:TestMvvm></local:TestMvvm></Window.DataContext>
改x:class
x:Class="WpfApp1.views.MainWindow"
总结 有mainwindow的地方都要改 路径多加一个views
在 C# 中,命名空间通常是
项目名.文件夹名
(如果文件夹有嵌套命名空间)。就不用viewmodels.WpfApp1.viewmodels 直接项目名.文件夹名即可
也就是命名空间不需要加文件夹名