在Windows 8应用程序中,ViewModel中的依赖属性和INotifyPropertyChanged有何区别?

3

我已创建一个空的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. 使用依赖属性
  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 for web和WP7没有coerce功能...WinRT也是如此...但我仍在寻找并希望),并且对我来说看起来更自然。但不幸的是,它在第一种方法中作为OneTime工作。
有人能解释一下为什么MS放弃使用Dependency Property来实现视图模型吗?

这可能会有所帮助:https://dev59.com/U3A65IYBdhLWcg3w8zft#3552550 - Lukasz Madon
@lukas 我知道优缺点。但是使用依赖属性的方式不正常(只能作为“一次性”)。 - RredCat
1个回答

5

在您的ViewModel中不应使用DependencyProperty - 只应在控件中使用它们。 您永远不会想要将一个ViewModel绑定到另一个ViewModel,此外,ViewModels不需要持久化其值,也不需要提供默认值或属性元数据。

您应该仅在ViewModel中使用INotifyPropertyChanged。


1
就个人而言,我不同意。我总是更喜欢在我的视图模型中使用 DP,因为:a - 它执行速度更快(不使用反射);b - 它的语义表明“这些内容属于表示层”。我认为 WinRT 中外部 DP 的当前行为(例如,“OneTime”)是一个 bug。希望能从 Microsoft 团队得到一些关于此问题的评论。 - Illidan
1
我不同意你的观点,请查看http://msdn.microsoft.com/en-us/library/ms752914.aspx。 "依赖属性的目的是提供一种基于其他输入值计算属性值的方式。这些其他输入可能包括系统属性(如主题和用户偏好)、即时属性确定机制(如数据绑定和动画/故事板)、多次使用的模板(如资源和样式)或通过与元素树中其他元素的父子关系已知的值"。 - Slugart

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接