ViewModel for Windows 8应用程序中的依赖属性vs INotifyPropertyChanged

时间:2012-06-12 21:13:15

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

我创建了空白的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
  3. 对于第一种方法,它是:

    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来实现视图模型吗?

1 个答案:

答案 0 :(得分:4)

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

您应该只在ViewModels中使用INotifyPropertyChanged。

相关问题