c# - MVVM 我是否正确地实现了这个

标签 c# wpf mvvm

在过去一周或更长时间里,我一直在尝试围绕 mvvm 进行思考,但仍然有些挣扎。我观看了 Jason Dolingers MVVM 视频并学习了 Reed Copsey 类(class),但仍然发现自己想知道我是否做得对……我创建了一个非常简单的时钟应用程序,我将在下面发布。该程序的输出与预期的一样,但是我更感兴趣的是我是否真正正确地使用了该模式。任何想法评论等将不胜感激。

我的模型

using System;
using System.Threading;

namespace Clock
{
    public class ClockModel
    {
        private const int TIMER_INTERVAL = 50;

        private DateTime _time;

        public event Action<DateTime> TimeArrived;

        public ClockModel()
        {
            Thread thread = new Thread(new ThreadStart(GenerateTimes));
            thread.IsBackground = true;
            thread.Priority = ThreadPriority.Normal;
            thread.Start();
        }

        public DateTime DateTime
        {
            get
            {
                return _time;
            }
            set
            {
                this._time = value;
                if (TimeArrived != null)
                {
                    TimeArrived(DateTime);
                }
            }
        }

        private void GenerateTimes()
        {
            while (true)
            {
                DateTime = DateTime.Now;
                Thread.Sleep(TIMER_INTERVAL);
            }
        }
    }
}

我的观点
<Window x:Class="Clock.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ViewModels="clr-namespace:Clock"
        Title="MainWindow" Height="75" Width="375">
    <Window.DataContext>
        <ViewModels:ClockViewModel />
    </Window.DataContext>
    <StackPanel Background="Black">
            <TextBlock Text="{Binding Path=DateTime}" Foreground="White" Background="Black" FontSize="30" TextAlignment="Center" />
    </StackPanel>
</Window>

我的 View 模型
using System;
using System.ComponentModel;

namespace Clock
{
    public class ClockViewModel : INotifyPropertyChanged
    {
        private DateTime _time;
        private ClockModel clock;

        public ClockViewModel()
        {
            clock = new ClockModel();
            clock.TimeArrived += new Action<DateTime>(clock_TimeArrived);
        }

        private void clock_TimeArrived(DateTime time)
        {
            DateTime = time;
            this.RaisePropertyChanged("DateTime");
        }

        public DateTime DateTime 
        {
            get
            {
                return _time;
            }

            set
            {
                _time = value;
            }
        }


        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises the property changed event.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }
}

最佳答案

你这样做的方式很好。我只需要改变一件事:将调用移至 RaisePropertyChange到属性的设置者。这通常是如何完成的,它可以防止您在设置属性时忘记引发通知。

关于c# - MVVM 我是否正确地实现了这个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5615070/

相关文章:

mvvm - 找不到安装了 .NET Framework 4 的 System.ComponentModel.Composition

c# - 为 Windows 手机 silverlight 页面实现 Dispose 和 Finalize

c# - 在 bing map xaml 中绑定(bind)位置

c# - 使用 System.ComponentModel

c# - 在c#中使用xelement创建动态xml

c# - WPF MVVM 可观察集合不更新 GUI

java - Android MVVM 谁应该读取和存储包

c# - 如何清除浏览器后退按钮点击 MVC4 中的浏览器缓存?

wpf - 如何在 MVVM 中的一个 StackPanel 中动态设置 Extenders 内的 3 个 DataGrids 的高度?

wpf - 使用 WPF 在 F# 中调用窗口加载事件的方法