commit 16dd8ddc891e1c2d997b296bf053c7426ad4e285 Author: zggsong Date: Thu Jul 20 16:49:25 2023 +0800 initial 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 @@ + + + + + + + + + + + + + + +