学习网址
- 微软官方Github-WPF网站
-
WPF界面与逻辑处理
界面交互对象、界面渲染、业务逻辑控制、事件驱动与数据驱动。WPF在接受Windows消息会直接命中的窗口句柄,然后通过路由进入到内部对象(命中)。在业务逻辑层面WPF不直接操作控件,隔山大牛(通过数据建模MVVM来获取数据)。
代码实现:https://github.com/AlbertZhaoz/NET_GetStartedWithWPF AlbertWPF
- AlbertWPF_Calculate
实现了数据Binding、Command指令、NotifyPropertyChanged(控件属性变更会传递给对象)
public class ShowNumModel:INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
private string content;
public string Content
{
get { return content; }
set
{
content = value;
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("content"));
}
}
public ShowNumModel(string content)
{
this.Content = content;
}
}
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();
}
});
}
}