首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实现BtnSquareCommand/RelayCommand

实现BtnSquareCommand/RelayCommand
EN

Stack Overflow用户
提问于 2016-08-30 11:56:44
回答 1查看 27关注 0票数 0

我正在尝试实现一种方法,它可以将一个正方形图转换成一个圆。当我现在跑的时候,有一个正方形可以移动。无论如何,应该如何实现平方按钮的命令?

代码语言:javascript
复制
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));
        }

    }
}

编辑:

按钮已经实现了,但没有它们的功能,所以它们什么也不做

代码语言:javascript
复制
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));

        }

    }
}

之后我该怎么办?

……

代码语言:javascript
复制
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);
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-30 12:03:25

代码语言:javascript
复制
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
}

查查我关于同一件事的另一个答案。关于如何正确地做这件事有更多的解释:Create a SearchCommand in wpf using Mvvm that loads new data to my list view

编辑:你也必须改变你的内容部分,它需要实现INotifyPropertyChanged;

代码语言:javascript
复制
 private object _content;
 public object Content
 {
   get{return _content;}
   set
     { 
       _content = value; 
        OnPropertyChanged("Content"");
      }
 }

编辑编辑:

我会帮你把全班都发出去。

只需复制粘贴到您的项目。

代码语言:javascript
复制
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);
        }
    }
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39226858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档