NotifyAppBase
对于基础方法使用基本没有啥区别了
直接上代码
基础1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace NotifyAppBase
{
public class ViewModelBase : NotifyPropertyBase
{
}
public class NotifyPropertyBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual bool Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (object.Equals(storage, value)) return false;
storage = value;
this.OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged(string propertyName)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
基础2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace NotifyAppBase
{
public class RelayCommand : ICommand
{
private Action ExecuteMethod;
private Func<bool> CanExecuteMethod;
public RelayCommand(Action execute)
{
this.ExecuteMethod = execute;
}
public RelayCommand(Action execute, Func<bool> canExecute)
{
this.ExecuteMethod = execute;
this.CanExecuteMethod = canExecute;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
//根据自己需要更改
return true;
}
public void Execute(object parameter)
{
if (this.CanExecuteMethod == null)
{
this.ExecuteMethod?.Invoke();
}
else
{
var canexecute = this.CanExecuteMethod.Invoke();
if (canexecute == true)
{
this.ExecuteMethod?.Invoke();
}
}
}
}
public class RelayCommand<T> : ICommand
{
private Action<T> ExecuteMethod;
private Func<T, bool> CanExecuteMethod;
public RelayCommand(Action<T> execute)
{
this.ExecuteMethod = execute;
}
public RelayCommand(Action<T> execute, Func<T, bool> canExecute)
{
this.ExecuteMethod = execute;
this.CanExecuteMethod = canExecute;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
//根据自己需要更改
return true;
}
public void Execute(object parameter)
{
if (this.CanExecuteMethod == null)
{
this.ExecuteMethod?.Invoke((T)parameter);
}
else
{
var canexecute = this.CanExecuteMethod?.Invoke((T)parameter);
if (canexecute == true)
{
this.ExecuteMethod?.Invoke((T)parameter);
}
}
}
}
}
使用 1 viewmodel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NotifyAppBase.ViewModel
{
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
ClickCommand = new RelayCommand(ClickMethod);
DoubleClickCommand = new RelayCommand(DoubleClickMethod);
ClickWidthParamCommand = new RelayCommand<string>(ClickWidthParamMethod);
}
private string _NotifyString;
public string NotifyString { get => _NotifyString; set => Set(ref _NotifyString, value); }
private string _TextBoxInputString;
public string TextBoxInputString { get => _TextBoxInputString; set => Set(ref _TextBoxInputString, value); }
public RelayCommand ClickCommand { get; set; }
public RelayCommand DoubleClickCommand { get; set; }
public RelayCommand<string> ClickWidthParamCommand { get; set; }
private void ClickWidthParamMethod(string obj)
{
//do something
NotifyString = $"you click button & send param [{obj}]";
}
private void DoubleClickMethod()
{
//do something
NotifyString = "you double click button";
}
private void ClickMethod()
{
//do something
NotifyString = "you click button";
}
}
}
使用2 绑定 界面
<Window x:Class="NotifyAppBase.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:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:local="clr-namespace:NotifyAppBase"
xmlns:vm="clr-namespace:NotifyAppBase.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<vm:MainViewModel x:Name="MainViewModel"/>
</Window.DataContext>
<Grid>
<Button Content="Button1" Command="{Binding ClickCommand}" HorizontalAlignment="Left" Margin="185,251,0,0" VerticalAlignment="Top" Width="74"/>
<Button Content="Button2" Command="{Binding ClickWidthParamCommand}" CommandParameter="{Binding Text,ElementName=param}" HorizontalAlignment="Left" Margin="311,251,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox x:Name="param" HorizontalAlignment="Left" Height="23" Margin="311,201,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<TextBlock HorizontalAlignment="Left" Margin="164,87,0,0" TextWrapping="Wrap" Text="{Binding NotifyString}" VerticalAlignment="Top"/>
<Button Content="Button3" Command="{Binding ClickWidthParamCommand}" CommandParameter="{Binding TextBoxInputString}" HorizontalAlignment="Left" Margin="497,251,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="497,201,0,0" TextWrapping="Wrap" Text="{Binding TextBoxInputString,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
<Button Content="DoubleClick" HorizontalAlignment="Left" Margin="185,293,0,0" VerticalAlignment="Top" Width="74">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding DoubleClickCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</Window>