实施 BtnSquareCommand/RelayCommand
Implementing BtnSquareCommand/RelayCommand
我正在尝试实现一种可以将方形变为圆形的方法。当我 运行 现在有一个可以移动的正方形。无论如何,应该如何实现方形按钮的命令?
namespace Square
{
public class MainViewModel : INotifyPropertyChanged
{
private double _x;
public object Content { get; set; }
public double X
{
get { return _x; }
set
{
_x = value;
}
}
public ICommand BtnSquareCommand = new RelayCommand(); //I'm stuck here
public double Y { get; set; }
public MainViewModel()
{
Content = new SquareViewModel();
}
private void SetSquare()
{
Content = new SquareViewModel();
}
private void SetCircle()
{
Content = new SquareViewModel();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnProperrtyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
编辑:
按钮已经实现,但还没有实现它们的功能,所以它们什么都不做
namespace Square
{
public class MainViewModel : INotifyPropertyChanged
{
private double _x;
public object Content { get; set; }
public double X
{
get { return _x; }
set
{
_x = value;
}
}
public ICommand BtnSquareCommand {get ; set;}
public ICommand BtnCircleCommand {get ; set;}
public double Y { get; set; }
public MainViewModel()
{
Content = new SquareViewModel();
}
private void SetSquare()
{
Content = new SquareViewModel();
}
private void SetCircle()
{
Content = new CircleViewModel();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnProperrtyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
之后我该做什么?
.....
namespace Sqaure
{
public class MainViewModel : INotifyPropertyChanged
{
private double _x;
public object Content { get; set; }
public double X
{
get { return _x; }
set
{
_x = value;
OnPropertyChanged("Content");
}
}
public ICommand BtnSquareCommand { get; set; }
void BtnSquareCommand_Click(object obj)
{
SetSquare();
}
public ICommand BtnCircleCommand { get; set; }
void BtnCircleCommand_Click(object obj)
{
SetCircle();
}
public double Y { get; set; }
public MainViewModel()
{
Content = new SquareViewModel();
BtnSquareCommand = new RelayCommand(BtnSquareCommand_Click);
}
private void SetSquare()
{
Content = new SquareViewModel();
}
private void SetCircle()
{
Content = new CircleViewModel();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
Console.WriteLine("HEEJ");
}
public class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Func<bool> _canExecute;
public RelayCommand(Action<object> execute, Func<bool> canExecute = null)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute.Invoke();
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
}
}
public MainViewModel()
{
Content = new SquareViewModel();
BtnSquareCommand= new RelayCommand(BtnSquareCommand_Click, ()=> CanExecute); //I'm stuck here
}
public ICommand BtnSquareCommand {get ; set;}
void BtnSquareCommand_Click(object obj)
{
//DO YOUR WORK HERE
}
查看我关于同一问题的其他答案。关于如何正确执行此操作的更多说明:
编辑:
您还必须更改内容部分,它需要实施 INotifyPropertyChanged;
private object _content;
public object Content
{
get{return _content;}
set
{
_content = value;
OnPropertyChanged("Content"");
}
}
编辑编辑:
我会 post 整个 class 给你。
只需将其复制粘贴到您的项目中即可。
namespace Sqaure
{
public class MainViewModel : INotifyPropertyChanged
{
private double _x;
private object _content;
public object Content
{
get{return _content;}
set
{
_content = value;
OnPropertyChanged("Contenct");
}
}
public double X
{
get { return _x; }
set
{
_x = value;
OnPropertyChanged("X");
}
}
public ICommand BtnSquareCommand { get; set; }
void BtnSquareCommand_Click(object obj)
{
SetSquare();
}
public ICommand BtnCircleCommand { get; set; }
void BtnCircleCommand_Click(object obj)
{
SetCircle();
}
public double Y { get; set; }
public MainViewModel()
{
Content = new SquareViewModel();
BtnSquareCommand = new RelayCommand(BtnSquareCommand_Click);
BtnCircleCommand = new RelayCommand(BtnCircleCommand_Click);
}
private void SetSquare()
{
Content = new SquareViewModel();
}
private void SetCircle()
{
Content = new CircleViewModel();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
Console.WriteLine("HEEJ");
}
}
public class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Func<bool> _canExecute;
public RelayCommand(Action<object> execute, Func<bool> canExecute = null)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute.Invoke();
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
}
我正在尝试实现一种可以将方形变为圆形的方法。当我 运行 现在有一个可以移动的正方形。无论如何,应该如何实现方形按钮的命令?
namespace Square
{
public class MainViewModel : INotifyPropertyChanged
{
private double _x;
public object Content { get; set; }
public double X
{
get { return _x; }
set
{
_x = value;
}
}
public ICommand BtnSquareCommand = new RelayCommand(); //I'm stuck here
public double Y { get; set; }
public MainViewModel()
{
Content = new SquareViewModel();
}
private void SetSquare()
{
Content = new SquareViewModel();
}
private void SetCircle()
{
Content = new SquareViewModel();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnProperrtyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
编辑:
按钮已经实现,但还没有实现它们的功能,所以它们什么都不做
namespace Square
{
public class MainViewModel : INotifyPropertyChanged
{
private double _x;
public object Content { get; set; }
public double X
{
get { return _x; }
set
{
_x = value;
}
}
public ICommand BtnSquareCommand {get ; set;}
public ICommand BtnCircleCommand {get ; set;}
public double Y { get; set; }
public MainViewModel()
{
Content = new SquareViewModel();
}
private void SetSquare()
{
Content = new SquareViewModel();
}
private void SetCircle()
{
Content = new CircleViewModel();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnProperrtyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
之后我该做什么?
.....
namespace Sqaure
{
public class MainViewModel : INotifyPropertyChanged
{
private double _x;
public object Content { get; set; }
public double X
{
get { return _x; }
set
{
_x = value;
OnPropertyChanged("Content");
}
}
public ICommand BtnSquareCommand { get; set; }
void BtnSquareCommand_Click(object obj)
{
SetSquare();
}
public ICommand BtnCircleCommand { get; set; }
void BtnCircleCommand_Click(object obj)
{
SetCircle();
}
public double Y { get; set; }
public MainViewModel()
{
Content = new SquareViewModel();
BtnSquareCommand = new RelayCommand(BtnSquareCommand_Click);
}
private void SetSquare()
{
Content = new SquareViewModel();
}
private void SetCircle()
{
Content = new CircleViewModel();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
Console.WriteLine("HEEJ");
}
public class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Func<bool> _canExecute;
public RelayCommand(Action<object> execute, Func<bool> canExecute = null)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute.Invoke();
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
}
}
public MainViewModel()
{
Content = new SquareViewModel();
BtnSquareCommand= new RelayCommand(BtnSquareCommand_Click, ()=> CanExecute); //I'm stuck here
}
public ICommand BtnSquareCommand {get ; set;}
void BtnSquareCommand_Click(object obj)
{
//DO YOUR WORK HERE
}
查看我关于同一问题的其他答案。关于如何正确执行此操作的更多说明:
编辑: 您还必须更改内容部分,它需要实施 INotifyPropertyChanged;
private object _content;
public object Content
{
get{return _content;}
set
{
_content = value;
OnPropertyChanged("Content"");
}
}
编辑编辑:
我会 post 整个 class 给你。
只需将其复制粘贴到您的项目中即可。
namespace Sqaure
{
public class MainViewModel : INotifyPropertyChanged
{
private double _x;
private object _content;
public object Content
{
get{return _content;}
set
{
_content = value;
OnPropertyChanged("Contenct");
}
}
public double X
{
get { return _x; }
set
{
_x = value;
OnPropertyChanged("X");
}
}
public ICommand BtnSquareCommand { get; set; }
void BtnSquareCommand_Click(object obj)
{
SetSquare();
}
public ICommand BtnCircleCommand { get; set; }
void BtnCircleCommand_Click(object obj)
{
SetCircle();
}
public double Y { get; set; }
public MainViewModel()
{
Content = new SquareViewModel();
BtnSquareCommand = new RelayCommand(BtnSquareCommand_Click);
BtnCircleCommand = new RelayCommand(BtnCircleCommand_Click);
}
private void SetSquare()
{
Content = new SquareViewModel();
}
private void SetCircle()
{
Content = new CircleViewModel();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
Console.WriteLine("HEEJ");
}
}
public class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Func<bool> _canExecute;
public RelayCommand(Action<object> execute, Func<bool> canExecute = null)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute.Invoke();
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
}