MVVM模式实现

本文关键字:实现 模式 MVVM | 更新日期: 2023-09-27 18:27:34

我想在WP应用程序中使用MVVM pattern。我对这种模式有一些想法。但是有些事情我不明白。我不知道这样做是否是一种好的做法。

所以,我有Model。模型是一种数据结构。字段和属性的集合。

型号

 public class Person : INotifyPropertyChanged
{
    private string name;
    private GeoCoordinate coordinate;
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            if (this.name != value)
            {
                this.name = value;
                this.RaisePropertyChanged("Name");
            }
        }
    }
    public GeoCoordinate Coordinate
    {
        get
        {
            return this.coordinate;
        }
        set
        {
            if (this.coordinate != value)
            {
                this.coordinate = value;
                this.RaisePropertyChanged("Coordinate");
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

ViewModel初始化模型的字段。

ViewModel

 public class PersonViewModel : INotifyPropertyChanged
    {
        public Person User
        {
            get;
            private set;
        }        
        public PersonViewModel()
        {
           this.User = new Person();
        }  
        public LoadData()
        {
           Service.GetUser((result) => 
            {
                 this.User.Name = result.Name;
                 this.User.Coordinate = result.Coordinate;
            });
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

查看

PersonViewModel _viewModel;
this.DataContext = _viewModel;
_viewModel.LoadData();

以下是我想澄清的时刻:

  1. ViewModel如何通知View加载数据时出错,结束加载
  2. 我可以将部分日期传递给View吗(没有数据绑定,从技术上讲是可能的,我的意思是,这在模式下是允许的)

例如,在ViewModel中:

   public LoadData(Action<Person, Exception> act)
            {
               Service.GetUser((result, error) => 
                {
                    if (error != null)
                    {
                        act.Invoke(null, error);
                    }
                    else
                    {
                        this.User.Name = result.Name;
                        this.User.Coordinate = result.Coordinate;
                        act.Invoke(result, null);
                    }
                });                   
            } 

视图中:

_viewModel.LoadData((result, error) =>
    { 
       if (error != null)
       {
             //error data loading
       }
       else
       {
            //successfully loading
       }
    });

这太可怕了,可能这种方法破坏了整个概念。但是,例如,我使用Jeff Wilcox静态映射。

           <jwMaps:StaticMap                                
                            Provider="Bing"                                                                   
                            Visibility="Visible">
                            <jwMaps:StaticMap.MapCenter>
                                <geo:GeoCoordinate 
                                    Latitude ="50"
                                    Longitude="50" />
                            </jwMaps:StaticMap.MapCenter>
                        </jwMaps:StaticMap>

我无法将坐标绑定到此控件。我试过了,没用。如果使用

 StaticMap.MapCenter =
       new GeoCoordinate() { Latitude = user.Latitude, Longitude = user.Longitude };

然后工作。

如果是代表,我可以在一个成功的分支中完成。。。

请提供帮助建议。

MVVM模式实现

您可以将消息从viewmodel发送到视图,以便使用mvvm light的Messenger类

我不会破坏MVVM模式因为MVPM模式是你必须在ViewModel中完成逻辑部分,但这并不意味着你不能在代码后面使用你的Page.xaml.cs代码。你可以在代码后面决定可见性或任何与UI相关的代码。此外,它并不是一个硬编码模式,实际上它是为了以更好、更简单的方式组织您的项目,但如果您必须在视图viewModel之间进行通信,因为某些原因,您无法从viewModel解决问题,而无法使用codeehind。我不是专家,但这就是我的想法。。希望能更新一下这个话题。。