main
zggsong 1 year ago
commit 16dd8ddc89

9
.gitignore vendored

@ -0,0 +1,9 @@
.vs/
bin/
obj/
*.user
.idea/
packages/
.DS_Store

@ -0,0 +1,9 @@
<Application x:Class="MVVMDemo.CommunityToolKit.Mvvm.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMDemo.CommunityToolKit.Mvvm"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

@ -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
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

@ -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)
)]

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
</ItemGroup>
</Project>

@ -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
{
/// <summary>
/// 使用分布类 partial 继承ObservableObject
/// 就不用写ViewModelBase RelayCommand了
/// </summary>
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";
}
}
}

@ -0,0 +1,54 @@
<Window x:Class="MVVMDemo.CommunityToolKit.Mvvm.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MVVMDemo.CommunityToolKit.Mvvm"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<!--// 有很多中绑定方式这里选择了UI代码中绑定//-->
<local:MainViewModel />
</Window.DataContext>
<Grid>
<StackPanel Orientation="Vertical"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<StackPanel Orientation="Horizontal"
Margin="2">
<Label Content="Origin Content: "
FontSize="20"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
<TextBox Text="{Binding TextContent, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap"
MinWidth="400"
MaxWidth="600"
FontSize="20"
Padding="4"/>
</StackPanel>
<StackPanel Orientation="Horizontal"
Margin="2">
<Label Content="Sync Content: "
FontSize="20"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
<TextBlock Text="{Binding TextContent}"
TextWrapping="Wrap"
MinWidth="400"
MaxWidth="600"
FontSize="20"
Padding="4"/>
</StackPanel>
<!--// CommandParameter: 绑定对象到BtnCmd上 //-->
<Button Content="Reset"
Command="{Binding BtnCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
FontSize="30" />
</StackPanel>
</Grid>
</Window>

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MVVMDemo.CommunityToolKit.Mvvm
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}

@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.6.33829.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVVMDemo", "MVVMDemo\MVVMDemo.csproj", "{02775190-3409-4FBE-8B19-D44AFB27487C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVVMDemo.CommunityToolKit.Mvvm", "MVVMDemo.CommunityToolKit.Mvvm\MVVMDemo.CommunityToolKit.Mvvm.csproj", "{5F94EC8F-DEF3-4F50-87FB-72F76DFDF4AC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{02775190-3409-4FBE-8B19-D44AFB27487C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{02775190-3409-4FBE-8B19-D44AFB27487C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{02775190-3409-4FBE-8B19-D44AFB27487C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{02775190-3409-4FBE-8B19-D44AFB27487C}.Release|Any CPU.Build.0 = Release|Any CPU
{5F94EC8F-DEF3-4F50-87FB-72F76DFDF4AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5F94EC8F-DEF3-4F50-87FB-72F76DFDF4AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5F94EC8F-DEF3-4F50-87FB-72F76DFDF4AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5F94EC8F-DEF3-4F50-87FB-72F76DFDF4AC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D91EF1E3-1DF4-4E1E-9C3D-41D340EAC84A}
EndGlobalSection
EndGlobal

@ -0,0 +1,9 @@
<Application x:Class="MVVMDemo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMDemo"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

@ -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
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

@ -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)
)]

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace MVVMDemo
{
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
/// <summary>
/// 更新方式,其实只要下面的就行,在这边封装一层,更方便
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="properValue"></param>
/// <param name="newValue"></param>
/// <param name="properName"></param>
protected void UpdateProperty<T>(ref T properValue, T newValue, [CallerMemberName] string properName = "")
{
//对比新旧值,如果相等就不更新
if (object.Equals(properValue, newValue))
return;
properValue = newValue;
NotifyPropertyChanged(properName);
}
/// <summary>
/// 最基础的方式
/// </summary>
/// <param name="propertyName"></param>
//public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)//参数加上此标记就可以不用传递参数了
public void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>

@ -0,0 +1,37 @@
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
{
public class MainViewModel : BaseViewModel
{
private string _textContent = "Default Content";
public string TextContent { get => _textContent; set => UpdateProperty(ref _textContent, value); }
public ICommand BtnCmd { get; private set; }
public MainViewModel()
{
BtnCmd = new RelayCommand((_) =>
{
//这个就是控制是否启用的条件
return string.IsNullOrEmpty(TextContent);
}, (o) =>
{
// 获取CommandParameter绑定对象
//var view = o as Window;//因为MainWindow继承自Window所以可以这么写
var view = o as MainWindow;
//举个例子: 点击就最大化窗口
if (view != null)
view.WindowState = WindowState.Maximized;
TextContent = "Default Content";
});
}
}
}

@ -0,0 +1,54 @@
<Window x:Class="MVVMDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MVVMDemo"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<!--// 有很多中绑定方式这里选择了UI代码中绑定//-->
<local:MainViewModel />
</Window.DataContext>
<Grid>
<StackPanel Orientation="Vertical"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<StackPanel Orientation="Horizontal"
Margin="2">
<Label Content="Origin Content: "
FontSize="20"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
<TextBox Text="{Binding TextContent, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap"
MinWidth="400"
MaxWidth="600"
FontSize="20"
Padding="4"/>
</StackPanel>
<StackPanel Orientation="Horizontal"
Margin="2">
<Label Content="Sync Content: "
FontSize="20"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
<TextBlock Text="{Binding TextContent}"
TextWrapping="Wrap"
MinWidth="400"
MaxWidth="600"
FontSize="20"
Padding="4"/>
</StackPanel>
<!--// CommandParameter: 绑定对象到BtnCmd上 //-->
<Button Content="Reset"
Command="{Binding BtnCmd}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
FontSize="30" />
</StackPanel>
</Grid>
</Window>

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MVVMDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace MVVMDemo
{
// 这个RelayCommand只要继承了ICommand实现接口就可以了
// 至于你怎么定义构造,怎么实现,都是你自己的事情
public class RelayCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _execute;
public RelayCommand(Predicate<object> canExecute, Action<object> execute)
{
this._canExecute = canExecute;
this._execute = execute;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
return _canExecute(parameter);
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
}
Loading…
Cancel
Save