8. 监听属性改变(ImplementPropertyChanged),外界绑定处理动作

1.

2. 内部封装示例代码

namespace FlightBase.Model
{
    // 一个第三方框架,帮我们坚挺属性值(例如:SearchingStatus)的改变,改变以后会调用OnSearchingStatusChanged方法
    [ImplementPropertyChanged]
    public class SearchingSatelliteModel
    {
        public SearchingStatus SearchingStatus { get; set; }

        // SearchingStatusChangedAction,提供给外界SearchingStatus属性改变时候的处理事件,这个事件在OnSearchingStatusChanged方法中调用(?.Invoke , 如果不空就调用)
        public Action SearchingStatusChangedAction { get; set; }

        public void OnSearchingStatusChanged()
        {
            SearchingStatusChangedAction?.Invoke(this.SearchingStatus);
        }
    }

    public enum SearchingStatus
    {
        None = 0,
        Searching,
        Succeed
    }

3. 外界绑定Action代码

  • 在实例化SearchingSatelliteModel对象的时候,直接关联Action
  • searchingStatus 是SearchingStatusChangedAction 传递过来的参数
8. 监听属性改变(ImplementPropertyChanged),外界绑定处理动作_第1张图片
Paste_Image.png
Model = new SearchingSatelliteModel()
            {
                SearchingStatusChangedAction = searchingStatus =>
                {
                    switch (searchingStatus)
                    {
                        case SearchingStatus.Searching:
                            UiModel.SearchingTip = UiModel.DoSearchingTip;
                            break;
                        case SearchingStatus.Succeed:
                            UiModel.SearchingTip = UiModel.SearchSucceedTip;
                            UiModel.SearchingImage = "bg_satellite_search__succeed.png";
                            break;
                    }
                }
            };

你可能感兴趣的:(8. 监听属性改变(ImplementPropertyChanged),外界绑定处理动作)