同一视图中的多个 ViewModel [英] Multiple ViewModels in same View

查看:25
本文介绍了同一视图中的多个 ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在同一个视图 (MainPage.xaml) 中显示多个不同的 ViewModel.

I have several different ViewModels that I would like to display in the same view (MainPage.xaml).

我是新手,不知道该怎么做.我试图创建一个 MainViewModel:

I'm new to this and don't know how to do it. I have tried to create a MainViewModel:

    public class MainViewModel : ViewModelBase, INotifyPropertyChanged
    {
        WeatherViewModel weatherView = new WeatherViewModel();
        ForecastViewModel forecastViewModel = new ForecastViewModel();
        DeparturesViewModel departuresViewModel = new DeparturesViewModel();
        CalenderViewModel calenderViewModel = new CalenderViewModel();
    }

    public void GetAllViews()
    {
        weatherView.GetCurrentTemp();
        forecastViewModel.GetForecastTemp();
        departuresViewModel.GetDepartures();
        calenderViewModel.GetCalender();
    }

在我的 MainPage.xaml.cs 中有这个:

And in my MainPage.xaml.cs I have this:

     public MainPage()
     {
        this.InitializeComponent();
        this.DataContext = new MainViewModel();
     }

     private void Window_Loaded(object sender, RoutedEventArgs e)
     {
        var vm = this.DataContext as MainViewModel;
        vm.GetAllViews();
     }

我设法像这样单独显示每个 ViewModel:

I manage to display each ViewModel individually like this instead:

  this.DataContext = new WeatherViewModel();

但我想在同一个视图中显示所有内容.

but I would like to display everything in the same View.

推荐答案

我认为您走在正确的轨道上,但遗漏了一些小而重要的部分.

I think you're on the right track but missed some small but important pieces.

在您的示例代码中,MainViewModel 类当前设置有您真正需要公共属性的私有字段.此外,如果 ViewModelBase 尚未实现 INotifyPropertyChanged,我会确保它;这样,从 ViewModelBase 派生的任何类都不需要担心那部分.

In your example code the MainViewModel class is currently setup with private fields where you really need public properties. Additionally, I would make sure ViewModelBase implements INotifyPropertyChanged if it's not already; that way none of the classes deriving from ViewModelBase need to worry about that part.

public abstract class ViewModelBase : INotifyPropertyChanged
{
    /* INotifyPropertyChanged implementation +
       whatever other common behavior makes sense
       belongs in this class
    */
}

public class MainViewModel : ViewModelBase
{
    public WeatherViewModel Weather { get; } = new WeatherViewModel();
    public ForecastViewModel Forecast { get; } = new ForecastViewModel();
    public DeparturesViewModel Departures { get; } = new DeparturesViewModel();
    public CalendarViewModel Calendar { get; } = new CalendarViewModel();
}

在您的视图代码隐藏文件中,您将数据上下文设置为 MainViewModel 的 2 个不同实例 - 一次在构造函数中,一次在 Loaded 事件处理程序中.我会坚持使用构造函数版本,或者您可以像这样在 XAML 中设置数据上下文:

In your view code behind file you're setting the data context to 2 different instances of MainViewModel - once in the constructor and once in the Loaded event handler. I'd stick with the constructor version or instead you could set the data context in XAML like this:

<MainPage.DataContext>
    <MainViewModel>
</MainPage.DataContext>

一旦设置了主页的数据上下文并且视图模型是公共属性,那么您就可以使用绑定来访问视图模型的状态(属性),可能是这样的:

Once the data context for the main page is setup and the view models are public properties then you can use bindings to access the state (properties) of the view models perhaps something like this:

<TextBlock Text='{Binding Path=Weather.CurrentTempCelsius, StringFormat='Current Temp: {0}°C'}' />

这篇关于同一视图中的多个 ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆