0

我现在正在学习 MVVM。由于我看到很多教程或项目只使用 View 和 ViewModel,我有点困惑。这是我的代码。

模型 :

public class StudentModel : PropertyChangedBase
{
    private String _firstName;
    public String FirstName
    {
        get { return _firstName; }
        set
        {
            _firstName = value;
            NotifyOfPropertyChange(() => FirstName);
        }
    }

    private Double _gradePoint;
    public Double GradePoint
    {
        get { return _gradePoint; }
        set
        {
            _gradePoint = value;
            NotifyOfPropertyChange(() => GradePoint);
        }
    }
}

看法 :

<UserControl x:Class="MVVMLearningWithCaliburnMicro.Views.StudentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:cal="http://www.caliburnproject.org">
    <Grid Width="525" Height="300" Background="Lavender">
        <DockPanel>
            <TextBlock HorizontalAlignment="Center" Text="Student Data"
                       DockPanel.Dock="Top" FontSize="20" />
            <StackPanel Orientation="Vertical" HorizontalAlignment="Center"
                        VerticalAlignment="Stretch"
                        Margin="0,8" DockPanel.Dock="Top">
                <StackPanel Orientation="Horizontal" Margin="0,5">
                    <TextBlock Text="Name" FontSize="15" Margin="5,0" />
                    <TextBox Name="txtName" Text="{Binding Path=Student.FirstName}" Width="250" />
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="0,5">
                    <TextBlock Text="Grade" FontSize="15" Margin="5,0" />
                    <TextBox Name="txtGrade" Text="{Binding Path=Student.GradePoint}" Width="250" />
                </StackPanel>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="0,5" HorizontalAlignment="Center"
                        VerticalAlignment="Bottom"
                        DockPanel.Dock="Bottom">
                <Button Name="btnSave" Width="100" Height="40"
                        cal:Message.Attach="SaveStudent">
                    <TextBlock Text="Save" FontSize="15" />
                </Button>
            </StackPanel>
        </DockPanel>
    </Grid>
</UserControl>

视图模型:

public class StudentViewModel
{
    public StudentModel Student { get; set; }

    public void SaveStudent()
    {
        MessageBox.Show(String.Format("Saved: {0} - ({1})", Student.FirstName, Student.GradePoint));
    }

    public StudentViewModel()
    {
        Student = new StudentModel { FirstName = "Tom Johnson", GradePoint = 3.7 };
    }

    private Boolean CanSaveStudent()
    {
        return Student.GradePoint >= 0.0 || Student.GradePoint <= 4.0;;
    }
}

Q :
1. 我怎么把我的守卫财产NotifyOfPropertyChange()放在模型里?
2.(愚蠢的问题)我的 MVVM 模式是否指向了正确的方法?

4

4 回答 4

2

一种解决方案是从 PropertyChangedBase 继承视图模型并订阅 StudentModel 的属性更改。然后将guard方法转换为属性,如下所示:

public class StudentViewModel: PropertyChangedBase
{
    public StudentModel Student { get; set; }

    public void SaveStudent()
    {
        MessageBox.Show(String.Format("Saved: {0} - ({1})", Student.FirstName, Student.GradePoint));
    }

    public StudentViewModel()
    {
        Student = new StudentModel { FirstName = "Tom Johnson", GradePoint = 3.7 };
        Student.PropertyChanged += delegate { NotifyOfPropertyChanged( () => CanSaveStudent)};
    }

    public Boolean CanSaveStudent
    {
        get 
        {
            return Student.GradePoint >= 0.0 || Student.GradePoint <= 4.0;
        }
    }
}

希望它有效。

于 2012-06-09T21:17:05.687 回答
1

通知事件应该出现在 中view model,因为它会从 中传达更改model并将它们推送到 UI,反之亦然。这是根据 MVVM 设计指南。

在您的特定情况下,您可以或保持原样,但删除不必要view model的,或将通知器移入view model

于 2012-06-09T21:03:21.407 回答
0

好的,我的代码中有一个结论这
是我的灵感

@tsells——感谢第三个链接

@john polvora - 感谢您指出该属性
重要提示:它应该是公共财产

使用 Caliburn.Micro 的保护条款未触发保护条款错误


这是我的代码,我认为它们更干净

模型 :

public class StudentModel
{
    public String FirstName { get; set; }
    public Double GradePoint { get; set; }
}

看法 :

<UserControl x:Class="MVVMWithCMTwo.Views.StudentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:cal="http://www.caliburnproject.org">
    <Grid Width="525" Height="300" Background="Lavender">
        <DockPanel>
            <TextBlock HorizontalAlignment="Center" Text="Student Data"
                       DockPanel.Dock="Top" FontSize="20" />
            <StackPanel Orientation="Vertical" HorizontalAlignment="Center"
                        VerticalAlignment="Stretch"
                        Margin="0,8" DockPanel.Dock="Top">
                <StackPanel Orientation="Horizontal" Margin="0,5">
                    <TextBlock Text="Name" FontSize="15" Margin="5,0" />
                    <TextBox Name="txtName" Text="{Binding Path=StudentName}" Width="250" />
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="0,5">
                    <TextBlock Text="Grade" FontSize="15" Margin="5,0" />
                    <TextBox Name="txtGrade" Text="{Binding Path=StudentGrade}" Width="250" />
                </StackPanel>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="0,5" HorizontalAlignment="Center"
                        VerticalAlignment="Bottom"
                        DockPanel.Dock="Bottom">
                <Button Name="btnSave" Width="100" Height="40"
                        cal:Message.Attach="SaveStudent">
                    <TextBlock Text="Save" FontSize="15" />
                </Button>
            </StackPanel>
        </DockPanel>
    </Grid>
</UserControl>

视图模型:

public class StudentViewModel : PropertyChangedBase
{
    public StudentModel Student { get; set; }

    public String StudentName
    {
        get { return Student.FirstName; }
        set
        {
            Student.FirstName = value;
            NotifyOfPropertyChange(() => Student.FirstName);
        }
    }

    public Double StudentGrade
    {
        get { return Student.GradePoint; }
        set
        {
            Student.GradePoint = value;
            NotifyOfPropertyChange(() => Student.GradePoint);
            NotifyOfPropertyChange(() => CanSaveStudent);
        }
    }

    public void SaveStudent()
    {
        MessageBox.Show(String.Format("Saved: {0} - ({1})", Student.FirstName, Student.GradePoint));
    }

    public StudentViewModel()
    {
        Student = new StudentModel { FirstName = "Tom Johnson", GradePoint = 3.7 };
    }

    public Boolean CanSaveStudent
    {
        get { return Student.GradePoint >= 0.0 && Student.GradePoint <= 4.0; }
    }
}
于 2012-06-10T05:29:07.767 回答
0

您的通知更改应该在模型中,因为这也是您的约束。基本上,您的视图模型应该加载对象列表并为您的 UI 提供一个绑定集合来处理大多数事情。您上面的实现似乎是一个带有单个对象的详细面板(例如编辑模式)。

把它们加起来。

ViewModel - 应该实现 INotifyPropertyChangeed 接口 Model - 应该实现 INotifyPropertyChangeed 接口

要启用/禁用命令/按钮,您应该使用命令绑定。我有一个 WPF 和 Silverlight 教程,您可以参考。

http://tsells.wordpress.com/2010/06/23/command-binding-with-wpf-and-silverlight-net-4-0-with-mv-vm/

在过去的几年里,我还围绕这件事发布了一些教程。喜欢的话就去看看吧......

http://tsells.wordpress.com/2011/02/08/using-reflection-with-wpf-and-the-inotifypropertychanged-interface/

http://tsells.wordpress.com/2010/06/02/wpf-model-view-viewmodel-mv-vm-example/

于 2012-06-09T23:31:51.233 回答