简体   繁体   English

同一视图中的多个ViewModel

[英]Multiple ViewModels in same View

I have several different ViewModels that I would like to display in the same view (MainPage.xaml). 我想在同一视图(MainPage.xaml)中显示几个不同的ViewModel。

I'm new to this and don't know how to do it. 我对此并不陌生,不知道该怎么做。 I have tried to create a MainViewModel: 我试图创建一个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();
    }

And in my MainPage.xaml.cs I have this: 在我的MainPage.xaml.cs中,我有以下内容:

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

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

I manage to display each ViewModel individually like this instead: 我设法像这样单独显示每个ViewModel:

  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. 我认为您的路线正确,但是错过了一些小而重要的片段。

In your example code the MainViewModel class is currently setup with private fields where you really need public properties. 在您的示例代码中, MainViewModel类当前使用私有字段设置,您确实需要公共属性。 Additionally, I would make sure ViewModelBase implements INotifyPropertyChanged if it's not already; 另外,如果尚未实现,我将确保ViewModelBase实现INotifyPropertyChanged that way none of the classes deriving from ViewModelBase need to worry about that part. 这样,从ViewModelBase派生的类都无需担心该部分。

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();
}

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. 在文件后面的视图代码中,您将数据上下文设置为MainViewModel的2个不同实例-一次在构造函数中,一次在Loaded事件处理程序中。 I'd stick with the constructor version or instead you could set the data context in XAML like this: 我会坚持使用构造函数版本,或者改为可以在XAML中设置数据上下文,如下所示:

<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'}' />

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

You have many ways to approach. 您有很多方法可以解决。 Fist way using x:bind . 使用x:bind拳头方式。 You could initialize each view model in the page resource and give them x:Name , then using x:bind to access specific property like following. 您可以在页面资源中初始化每个视图模型,并给它们x:Name ,然后使用x:bind来访问特定的属性,如下所示。

<Page.Resources>
    <local:CalenderViewModel x:Name="CalenderViewModel"/>
    <local:DeparturesViewModel x:Name="DeparturesViewModel"/>
    <local:ForecastViewModel x:Name="ForecastViewModel"/>
    <local:WeatherViewModel x:Name="WeatherViewModel"/>
</Page.Resources>
<Grid>
    <TextBlock Text="{x:Bind WeatherViewModel.temperature}"/>
</Grid>

Other Way is that integrate all the ViewModels into MainViewModel . 另一种方法是将所有ViewModels集成到MainViewModel And coding.monkey provide the correct solution that you could refer directly. 而且coding.monkey提供了可以直接引用的正确解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM