zoukankan      html  css  js  c++  java
  • WPF 中的 经典的ModelView 通知页面更新 UI

    view model

    ------------------------------------------------------------------------------

    using HPControls.Helper;

    using System;

    using System.ComponentModel;

    using System.Threading;

    using Xiaowei.Models;

    using Xiaowei.Services;

    using Xiaowei.Settings;

    namespace Xiaowei.ViewModels

    {

        public class MuteViewModel : INotifyPropertyChanged

        {

            public event PropertyChangedEventHandler PropertyChanged;

            public bool IsMuted

            {

                get { return LocalSettings.IsMuted; }

                set

                {

                    if(LocalSettings.IsMuted != value)

                    {

                        LocalSettings.IsMuted = value;

                        Property.Raise(this, PropertyChanged, "IsMuted");

                    }

                }

            }

            public void Toggle()

            {

                _ = HPMetrics.Metrics.Track(LocalSettings.CustomerId, (int)HPMetrics.XwEventType.ClickButton,"MuteButton");

       

                   IsMuted = !IsMuted;

                _ = ApplicationModel.Current.ToggleMute(IsMuted);

                if(IsMuted)

                {

                    StateManager.SetAttachState(StateManager.AttachStateEnum.micOff);

                }

                else

                {

                    StateManager.RemoveAttachState(StateManager.AttachStateEnum.micOff);

                }

                

            }

            public static MuteViewModel Get() => LazyInstance.Value;

            private MuteViewModel()

            {

                Property.Raise(this, PropertyChanged, "IsMuted");

                if (IsMuted)

                {

                    StateManager.SetAttachState(StateManager.AttachStateEnum.micOff);

                }

                else

                {

                    StateManager.RemoveAttachState(StateManager.AttachStateEnum.micOff);

                }

            }

            

            private static readonly Lazy<MuteViewModel> LazyInstance = new Lazy<MuteViewModel>(()=>

            {

                return new MuteViewModel();

            }, LazyThreadSafetyMode.ExecutionAndPublication);

        }

    }

    Property 类

    ------------------------------------------

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using Windows.ApplicationModel.Core;

    namespace HPControls.Helper

    {

        public static class Property

        {

            public static void Raise(object sender, PropertyChangedEventHandler handler, string property)

            {

                Run.Invoke(CoreApplication.MainView.CoreWindow.Dispatcher, () =>

                {

                    handler?.Invoke(sender, new PropertyChangedEventArgs(property));

                });

            }

        }

    }

  • 相关阅读:
    MVC架构模式
    Promise对象
    Trick:如何去掉html标签点击时的蓝色边框
    动态REM
    Ajax 详解及CORS
    数据预测之BP神经网络具体应用以及matlab代码
    Pearson相关系数
    Java linux lame .wav音频转mp3 并且压缩
    JavaWeb向浏览器返回一个音频流
    Tomcat 运行 idea 编译好的 .class JavaWeb 项目
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14301004.html
Copyright © 2011-2022 走看看