WPF基于.Net Core

因为最近.net core的热门,所以想实现一下.net core框架下的WPF项目,还是MVVM模式,下面就开始吧,简单做一个计算器吧。

使用VS2019作为开发工具

实现MVVM模式

1、实现基础项目

使用VS2019新建WPF App项目

![image-20200710193416617](C:\Users\fangzhongwei\Desktop\WPF Net Core\image\image-20200710193416617.png)

项目名称Common

1.1、修改项目属性

删除项目MainWindow.xaml以及MainWindow.xaml.cs文件

删除App.xaml和App.xaml.cs文件

修改项目输出为类库

![image-20200710193806136](C:\Users\fangzhongwei\Desktop\WPF Net Core\image\image-20200710193806136.png)

添加Command文件夹

![image-20200710193823822](C:\Users\fangzhongwei\Desktop\WPF Net Core\image\image-20200710193823822.png)

2.2、实现ICommand接口

定义BaseCommand类实现ICommand接口

using System; using System.Collections.Generic; using System.Text; using System.Windows.Input; namespace Common.Command { /// <summary> /// 实现ICommand接口 /// </summary> public class BaseCommand : ICommand { public Predicate<object> CanExecuteDelegate { get; set; } public Action<object> ExecuteDelegate { get; set; } public BaseCommand(Action<object> execute) { ExecuteDelegate = execute; } public BaseCommand(Action<object> execute, Predicate<object> canExecute) { CanExecuteDelegate = canExecute; ExecuteDelegate = execute; } public BaseCommand() { } /// <summary> /// Defines the method that determines whether the command can execute in its current state. /// </summary> public bool CanExecute(object parameter) { if (CanExecuteDelegate != null) return CanExecuteDelegate(parameter); return true; } /// <summary> /// Occurs when changes occur that affect whether the command should execute. /// </summary> public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } /// <summary> /// 执行 /// </summary> public void Execute(object parameter) { try { if (ExecuteDelegate != null) ExecuteDelegate(parameter); } catch (Exception ex) { string moudle = ExecuteDelegate.Method.DeclaringType.Name + ":" + ExecuteDelegate.Method.Name; } } /// <summary> /// Raises the CanExecuteChanged event. /// </summary> public void InvalidateCanExecute() { CommandManager.InvalidateRequerySuggested(); } } } 2.3、实现INotifyPropertyChanged接口 using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; namespace Common.Command { /// <summary> /// 实现INotifyPropertyChanged接口 /// </summary> public class NotifyPropertyBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = this.PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } public object Clone() { return this.MemberwiseClone(); } } } 2、主程序HelloCore 2.1、新建WPF APP项目

项目名称为HelloCore

新建View、ViewModel、Model三个文件夹

![image-20200711130227939](C:\Users\fangzhongwei\Desktop\WPF Net Core\image\image-20200711130227939.png)

删除MainWindow.xaml

![image-20200711130756699](C:\Users\fangzhongwei\Desktop\WPF Net Core\image\image-20200711130756699.png)

2.2、添加窗体MainView.xaml

![image-20200711131905603](C:\Users\fangzhongwei\Desktop\WPF Net Core\image\image-20200711131905603.png)

修改MainView.xaml

<Window x:Class="HelloCore.View.MainView" 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:HelloCore.View" mc:Ignorable="d" Title="HelloCore"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <TextBox Text="{Binding Result}" Margin="5"/> <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right"> <Button Content="HelloCore" Command="{Binding HelloCommand}" Margin="10"/> <Button Content="Clear" Command="{Binding ClearCommand}" Margin="10"/> </StackPanel> </Grid> </Window> 2.3、添加MainViewModel.cs文件

![image-20200711133707892](C:\Users\fangzhongwei\Desktop\WPF Net Core\image\image-20200711133707892.png)

using Common.Command; using System; using System.Collections.Generic; using System.Text; using System.Windows.Input; namespace HelloCore.ViewModel { public class MainViewModel : NotifyPropertyBase { private string _result; /// <summary> /// 绑定到界面上TextBox的Text属性上 /// </summary> public string Result { get { return _result; } set { _result = value; OnPropertyChanged("Result"); } } private ICommand _helloCommand; private ICommand _clearCommand; public ICommand HelloCommand { get { return this._helloCommand ?? (this._helloCommand = new BaseCommand() { CanExecuteDelegate = x => true, ExecuteDelegate = x => { Result = "Hello Net Core"; } }); } } public ICommand ClearCommand { get { return this._clearCommand ?? (this._clearCommand = new BaseCommand() { CanExecuteDelegate = x => true, ExecuteDelegate = x => { Result = ""; } }); } } } } 2.4、通过DataContext绑定

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpwjwj.html