【问题标题】:Dependency Property vs INotifyPropertyChanged in ViewModel for Windows 8 applicationWindows 8 应用程序的 ViewModel 中的依赖属性与 INotifyPropertyChanged
【发布时间】:2012-06-15 19:47:38
【问题描述】:

我创建了空白 C#/XAML Windows 8 应用程序。添加简单的 XAML 代码:

<Page
    x:Class="Blank.MainPage"
    IsTabStop="false"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel
            Margin="0,150"
            HorizontalAlignment="Center">
            <TextBlock
                x:Name="xTitle"
                Text="{Binding Title, Mode=TwoWay}"/>
            <Button Content="Click me!" Click="OnClick" />
        </StackPanel>
    </Grid>
</Page>

以及C#部分的简单代码:

public sealed partial class MainPage
    {
        private readonly ViewModel m_viewModel;

        public MainPage()
        {
            InitializeComponent();
            m_viewModel = new ViewModel
            {
                Title = "Test1"
            };
            DataContext = m_viewModel;
        }

        private void OnClick(object sender, RoutedEventArgs e)
        {
            m_viewModel.Title = "Test2";
        }
    }

现在我想实现ViewModel。我有两种方法:

  1. 使用Dependency Property
  2. 实施INotifyPropertyChanged

第一种方法是:

public class ViewModel : DependencyObject
    {
        public string Title
        {
            get
            {
                return (string)GetValue(TitleProperty);
            }
            set
            {
                SetValue(TitleProperty, value);
            }
        }

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string)
            , typeof(ViewModel)
            , new PropertyMetadata(string.Empty));
    }

第二个是:

public class ViewModel : INotifyPropertyChanged
    {
        private string m_title;

        public string Title
        {
            get
            {
                return m_title;
            }
            set
            {
                m_title = value;
                OnPropertyChanged("Title");
            }
        }

        protected void OnPropertyChanged(string name)
        {
            if (null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

我更喜欢第一种方式,因为它允许使用coerce(Silverlight 用于 web 和 WP7 没有coerce 功能.. WinRT 也是.. 但我仍在寻找和希望)并且看起来更自然为了我。但不幸的是,对于第一种方法,它的作用是OneTime

谁能向我解释为什么 MS 放弃使用 Dependency Property 来实现视图模型?

【问题讨论】:

标签: c# windows-8 microsoft-metro inotifypropertychanged dependencyobject


【解决方案1】:

您不应该在 ViewModel 中使用 DependencyProperty - 您应该只在控件中使用它们。您永远不会想将一个 ViewModel 绑定到另一个 ViewModel,而且 ViewModel 不需要保留它们的值,也不需要提供默认值,也不需要提供属性元数据。

您应该只在您的 ViewModel 中使用 INotifyPropertyChanged。

【讨论】:

  • 我个人不同意。我总是更喜欢在我的视图模型中使用 DP,因为:a - 它执行得更快(不使用反射)和 b - 它的语义说“这个人员属于表示层”。 WinRT 中外部 DP 的当前行为(例如“OneTime”)我认为是一个错误。很高兴从 Microsoft 团队获得对此的一些评论。
  • 我不同意你的观点,请查看msdn.microsoft.com/en-us/library/ms752914.aspx。 “依赖属性的目的是提供一种基于其他输入值计算属性值的方法。这些其他输入可能包括系统属性,例如主题和用户偏好,即时属性确定机制,例如数据绑定和动画/故事板、资源和样式等多用途模板,或通过与元素树中其他元素的父子关系已知的值"
猜你喜欢
  • 2013-08-12
  • 2011-04-02
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-27
  • 1970-01-01
相关资源
最近更新 更多