From 16dd8ddc891e1c2d997b296bf053c7426ad4e285 Mon Sep 17 00:00:00 2001 From: zggsong Date: Thu, 20 Jul 2023 16:49:25 +0800 Subject: [PATCH] initial --- .gitignore | 9 ++++ MVVMDemo.CommunityToolKit.Mvvm/App.xaml | 9 ++++ MVVMDemo.CommunityToolKit.Mvvm/App.xaml.cs | 17 ++++++ .../AssemblyInfo.cs | 10 ++++ .../MVVMDemo.CommunityToolKit.Mvvm.csproj | 14 +++++ .../MainViewModel.cs | 39 ++++++++++++++ .../MainWindow.xaml | 54 +++++++++++++++++++ .../MainWindow.xaml.cs | 28 ++++++++++ MVVMDemo.sln | 31 +++++++++++ MVVMDemo/App.xaml | 9 ++++ MVVMDemo/App.xaml.cs | 17 ++++++ MVVMDemo/AssemblyInfo.cs | 10 ++++ MVVMDemo/BaseViewModel.cs | 41 ++++++++++++++ MVVMDemo/MVVMDemo.csproj | 10 ++++ MVVMDemo/MainViewModel.cs | 37 +++++++++++++ MVVMDemo/MainWindow.xaml | 54 +++++++++++++++++++ MVVMDemo/MainWindow.xaml.cs | 28 ++++++++++ MVVMDemo/RelayCommand.cs | 39 ++++++++++++++ 18 files changed, 456 insertions(+) create mode 100644 .gitignore create mode 100644 MVVMDemo.CommunityToolKit.Mvvm/App.xaml create mode 100644 MVVMDemo.CommunityToolKit.Mvvm/App.xaml.cs create mode 100644 MVVMDemo.CommunityToolKit.Mvvm/AssemblyInfo.cs create mode 100644 MVVMDemo.CommunityToolKit.Mvvm/MVVMDemo.CommunityToolKit.Mvvm.csproj create mode 100644 MVVMDemo.CommunityToolKit.Mvvm/MainViewModel.cs create mode 100644 MVVMDemo.CommunityToolKit.Mvvm/MainWindow.xaml create mode 100644 MVVMDemo.CommunityToolKit.Mvvm/MainWindow.xaml.cs create mode 100644 MVVMDemo.sln create mode 100644 MVVMDemo/App.xaml create mode 100644 MVVMDemo/App.xaml.cs create mode 100644 MVVMDemo/AssemblyInfo.cs create mode 100644 MVVMDemo/BaseViewModel.cs create mode 100644 MVVMDemo/MVVMDemo.csproj create mode 100644 MVVMDemo/MainViewModel.cs create mode 100644 MVVMDemo/MainWindow.xaml create mode 100644 MVVMDemo/MainWindow.xaml.cs create mode 100644 MVVMDemo/RelayCommand.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aca15c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.vs/ +bin/ +obj/ +*.user +.idea/ + +packages/ + +.DS_Store diff --git a/MVVMDemo.CommunityToolKit.Mvvm/App.xaml b/MVVMDemo.CommunityToolKit.Mvvm/App.xaml new file mode 100644 index 0000000..a8f1809 --- /dev/null +++ b/MVVMDemo.CommunityToolKit.Mvvm/App.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/MVVMDemo.CommunityToolKit.Mvvm/App.xaml.cs b/MVVMDemo.CommunityToolKit.Mvvm/App.xaml.cs new file mode 100644 index 0000000..cb42fb2 --- /dev/null +++ b/MVVMDemo.CommunityToolKit.Mvvm/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace MVVMDemo.CommunityToolKit.Mvvm +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/MVVMDemo.CommunityToolKit.Mvvm/AssemblyInfo.cs b/MVVMDemo.CommunityToolKit.Mvvm/AssemblyInfo.cs new file mode 100644 index 0000000..8b5504e --- /dev/null +++ b/MVVMDemo.CommunityToolKit.Mvvm/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/MVVMDemo.CommunityToolKit.Mvvm/MVVMDemo.CommunityToolKit.Mvvm.csproj b/MVVMDemo.CommunityToolKit.Mvvm/MVVMDemo.CommunityToolKit.Mvvm.csproj new file mode 100644 index 0000000..78d09ae --- /dev/null +++ b/MVVMDemo.CommunityToolKit.Mvvm/MVVMDemo.CommunityToolKit.Mvvm.csproj @@ -0,0 +1,14 @@ + + + + WinExe + net6.0-windows + enable + true + + + + + + + diff --git a/MVVMDemo.CommunityToolKit.Mvvm/MainViewModel.cs b/MVVMDemo.CommunityToolKit.Mvvm/MainViewModel.cs new file mode 100644 index 0000000..16857a0 --- /dev/null +++ b/MVVMDemo.CommunityToolKit.Mvvm/MainViewModel.cs @@ -0,0 +1,39 @@ +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Input; + +namespace MVVMDemo.CommunityToolKit.Mvvm +{ + /// + /// 使用分布类 partial 继承ObservableObject + /// 就不用写ViewModelBase RelayCommand了 + /// + public partial class MainViewModel : ObservableObject + { + [ObservableProperty] + [NotifyCanExecuteChangedFor(nameof(BtnCommand))] + private string textContent = "Default Content through CommunityToolkit.Mvvm"; + + private bool CanExecuteBtn() => !string.IsNullOrEmpty(TextContent); + + + [RelayCommand(CanExecute = nameof(CanExecuteBtn))] + private void Btn(object o) + { + // 获取CommandParameter绑定对象 + //var view = o as Window;//因为MainWindow继承自Window,所以可以这么写 + var view = o as MainWindow; + //举个例子: 点击就最大化窗口 + if (view != null) + view.WindowState = WindowState.Maximized; + + TextContent = "Default Content"; + } + } +} diff --git a/MVVMDemo.CommunityToolKit.Mvvm/MainWindow.xaml b/MVVMDemo.CommunityToolKit.Mvvm/MainWindow.xaml new file mode 100644 index 0000000..a1c6f39 --- /dev/null +++ b/MVVMDemo.CommunityToolKit.Mvvm/MainWindow.xaml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + +