学习网址

实现了数据Binding、Command指令、NotifyPropertyChanged(控件属性变更会传递给对象)

  1. public class ShowNumModel:INotifyPropertyChanged
  2. {
  3. public event PropertyChangedEventHandler? PropertyChanged;
  4. private string content;
  5. public string Content
  6. {
  7. get { return content; }
  8. set
  9. {
  10. content = value;
  11. this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("content"));
  12. }
  13. }
  14. public ShowNumModel(string content)
  15. {
  16. this.Content = content;
  17. }
  18. }
public class CommandHelper : ICommand
    {
        public event EventHandler? CanExecuteChanged;

        public Action<object> DoExecute { get; set; }

        public bool CanExecute(object? parameter)
        {
            return true;
        }

        public void Execute(object? parameter)
        {
            DoExecute?.Invoke(parameter);
        }

        public CommandHelper(Action<object> doExecute)
        {
            DoExecute = doExecute;
        }
    }
 public class MainWindowsModel
    {
        public event PropertyChangedEventHandler? PropertyChanged;

        public string Title { get; set; } = "AlbertZhao";

        public ButtonModel BtnModel { get; set; } = new ButtonModel();

        public CommandHelper ButtonClickCommand
        {
            get => new CommandHelper(DoButtonClick); 
        }     

        private void DoButtonClick(object obj)
        {
            ButtonModel button = obj as ButtonModel;
            var test = button.Content;
            // 实现界面时间的实时刷新
            Task.Run(() =>{

                while (true)
                {
                    // 这一句等同于async ()=> await Task.Delay(100);
                    // Task.Delay(100).GetAwaiter().GetResult();
                    this.BtnModel.Content = DateTime.Now.ToString();
                }
            });

        }

    }