C# MVVM编程

C# MVVM编程,c#,wpf,mvvm,C#,Wpf,Mvvm,您好,我刚刚开始使用MVVM模式,在窗口中显示文本时遇到问题。你能帮助我吗。 这就是模型: using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace trying_the_best.Model {

您好,我刚刚开始使用MVVM模式,在窗口中显示文本时遇到问题。你能帮助我吗。 这就是模型:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace trying_the_best.Model
{
    public class PersoanaModel : DependencyObject
    {

        public string Nume
        {
            get { return (string)GetValue(NumeProperty); }
            set { SetValue(NumeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Nume.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NumeProperty =
            DependencyProperty.Register("Nume", typeof(string), typeof(PersoanaModel), new PropertyMetadata(0));



         public string Prenume
        {
            get { return (string)GetValue(PrenumeProperty); }
            set { SetValue(PrenumeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Prenume.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PrenumeProperty =
            DependencyProperty.Register("Prenume", typeof(string), typeof(PersoanaModel), new PropertyMetadata(0));


    }
}
这是ViewModel

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using trying_the_best.Model;

namespace trying_the_best.ViewModel
{
   public class PersoanaViewModel
    {
        public ObservableCollection<PersoanaModel> persoana
        {
            get;
            set;
        }
        public void incarcarepersoane()
        {
            ObservableCollection<PersoanaModel> Persoana = new ObservableCollection<PersoanaModel>();
            Persoana.Add(new PersoanaModel { Nume = "Bogdan", Prenume = "Marius" });

            persoana = Persoana;
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用最佳模式;
名称空间正在尝试\u最佳。ViewModel
{
公共类PersoanaViewModel
{
公共可观测收集人员
{
得到;
设置
}
公共事业单位
{
ObservableCollection Persoana=新的ObservableCollection();
添加(新的PersoanaModel{Nume=“Bogdan”,Prenume=“Marius”});
persoana=persoana;
}
}
}
这是主视图

<Window x:Class="trying_the_best.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:trying_the_best.ViewModel"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Margin="89,40,0,0" TextWrapping="Wrap" Text="{Binding XPath=Nume}" VerticalAlignment="Top" Width="219" Height="50"/>
    </StackPanel>
</Window>

使用cs文件

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using trying_the_best.ViewModel;
namespace trying_the_best
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
尝试使用最佳视图模型;
命名空间正在尽最大努力
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
}
}

我想把那个名字写出来。你能给我一个建议吗?我在互联网上搜索了一些答案,但我可以找到解决问题的方法。也许我只是个傻瓜,但我需要帮助。

您的代码中有一些问题

首先,您的视图模型包含一个人员列表。据我所知,从你的观点来看,你想显示一个人,对吗

此外视图的
DataContext
似乎未设置。要访问视图模型的字段,必须设置
DataContext

<Window x:Class="trying_the_best.MainWindow"
xmlns:local="clr-namespace:Your.Namespace.Here; assembly=Your.Assembly"
        [...]>
    <Window.DataContext>
        <local:PersoanaViewModel />
    </Window.DataContext>
    [...]
</Window>
基本上它现在应该可以工作了,但是如果在构造函数之后更改视图模型中的模型,您会注意到视图没有更新。要实现视图的更新,必须在视图模型中实现
INotifyPropertyChanged

class PersoanaViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    [...]

    public Persoana Persoana 
    {
        get
        {
            return _persoana;
        }
        set
        {
            if(value == _persoana) return; // do not update if the objects are equal
            _persoana = value;
            OnPropertyChanged(nameof(Nume));
        }
    }
}

这基本上是可行的,但它只是一个例子,向你展示了如何做到这一点。在现实世界中有更多的事情要考虑。

在这个代码中,视图的DATACONTRON没有设置,并且没有VIEWMDED的实例。您需要开始小,然后工作大。你已经开始大干一场了,现在你看不到大海捞针了。模型中的依赖属性似乎有点奇怪。你确定你真的需要它们吗?通常模型上只有“正常”属性。oke进行了必要的更改,但仍然没有显示任何内容。嗯,您是否可以为PersoanaViewModel创建一个空构造函数,启动调试器并检查是否调用了此构造函数?只需在构造函数中设置一个断点即可。此外,您确定
{Binding XPath=Nume}
应该是
{Binding Path=Nume}
还是更简单的
{Binding Nume}
不起作用,需要应用正确的命名空间定义。
class PersoanaViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    [...]

    public Persoana Persoana 
    {
        get
        {
            return _persoana;
        }
        set
        {
            if(value == _persoana) return; // do not update if the objects are equal
            _persoana = value;
            OnPropertyChanged(nameof(Nume));
        }
    }
}