使用MVVMCROSS的WPF应用程序 [英] WPF application using MVVMCROSS

查看:853
本文介绍了使用MVVMCROSS的WPF应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了MVVMCROSS,我认为这是解决跨平台问题的一个好主意.因此,我很想移植正在开发的应用程序和整个库,以使其更具可移植性.现在我正在尝试将我的视图模型绑定到视图,并且面临几个问题和疑问...

I recently came across MVVMCROSS which I think is a very good idea to solve cross platform issues. So I was tempted to port over an application I was working on and my entire libraries to make them more portable. Now I am trying ti bind my viewmodels to the views and I am facing several issues and questions...

我已经看到了导航如何工作以及如何开始正常工作的第一个视图的示例.但是,对于我的应用程序,我不能使用它.我的主视图包含一个位于窗口顶部的菜单栏,从中可以调用视图.主窗口包含以下内容:

I have seen the examples of how navigation works and how to start the first view which works fine. However, for my application I cannot use this.. My main view consists of a menu bar at the top of the window from where views can be called. The main window contains this :

  <Grid Height="Auto" Width="Auto">
<Grid.RowDefinitions>
  <RowDefinition Height="30" />
  <RowDefinition Height="*" />
</Grid.RowDefinitions>
<Menu Name="MainMenu"
      Height="25"
      Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type Grid}}}"
      Grid.Row="0">
  <MenuItem Header="Tools" Width="Auto">
    <MenuItem Header="Solar estimator" HorizontalAlignment="Left" Width="Auto" Click="Menu_Click"/>
  </MenuItem>
</Menu>
<DockPanel Grid.Row="1" Name="DockPanel" Height="Auto" Width="Auto">

</DockPanel>

所以我想做的就是,每当我从菜单中调用新视图时,我都会将其附加到扩展面板上.

So what I wish to do is that whenever I call a new view from the menu, I'll attach it to the dockpanel.

Menu_Click函数具有以下代码:

The function Menu_Click has this code :

      EnergyNeedsEstimatorView estimatorView = new EnergyNeedsEstimatorView();
  double estimatorViewHeight = estimatorView.Height;
  double estimatorViewWidth = estimatorView.Width;
  estimatorView.SetValue(UserControl.WidthProperty, double.NaN);
  estimatorView.SetValue(UserControl.HeightProperty, double.NaN);
  DockPanel.Children.Add(estimatorView);
  DockPanel.Height = estimatorView.Height;
  DockPanel.Width = estimatorView.Width;
  this.Height = estimatorViewHeight + MainMenu.Height + SystemParameters.WindowCaptionHeight + SystemParameters.ResizeFrameHorizontalBorderHeight;
  this.Width = estimatorViewWidth + SystemParameters.ResizeFrameVerticalBorderWidth + 10;

但是,由于我从核心库中明确删除了IMvxAppStart部分,并从我的桌面库中的DoSetup中删除了IMvxAppStart Resolve,因此视图显示得很好,因此不再建立viewmodel和视图之间的链接.我想我可以像以前一样做,并指定我的观点是DataContext是viewmodel,并且可以像使用MVVMCross之前一样工作,但是我觉得这不是最好的方法.

So view is displayed fine however, since I explicitly removed the IMvxAppStart part from my core library and removed the IMvxAppStart Resolve from DoSetup in my Desktop library, the link between the viewmodel and the view is no longer made. I suppose I could do like before and specify to my view that it's DataContext is the viewmodel and it would work like I did before using MVVMCross but I feel this is not the best approach.

所以您可能想知道,为什么不只是将菜单栏添加到每个视图,而不是尝试将我的视图附加到扩展面板上?好吧,当我需要进行更改时,我不想在任何地方更改菜单栏.所以我试图有一个通用的主窗口,从那里可以显示所有视图.现在,我被困住了,试图弄清楚如何使用MVVMCross来做到这一点.

So you probably are wondering, why not just add the menu bar to every view instead of trying to attach my views to a dockpanel? Well, I do not want to change my menu bars everywhere when I need to make a change. So I am trying to have a generic main window from where to display all views. Right now I am stuck and trying to figure out how I can do this with MVVMCross..

有什么想法吗?

推荐答案

如果您要求容器为您创建一个View,则它将自动尝试为您加载ViewModel-请参见

If you ask the container to create you a View, then it automatically tries to Load a ViewModel for you - see the code in https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Wpf/Views/MvxWpfViewsContainer.cs

因此您可以将new替换为

 car view = Mvx.Resolve<IMvxWpfViewsContainer>().CreateView(MvxViewModelRequest<MyViewModel>.GetDefaultRequest()):


但是,通常在mvvmcross中,ViewModel和Applications不直接与Container对话-而是与Presenter对话,最好修改代码以使用Presenter.在此流程中,您的菜单将 -发送演示者ViewModelRequest对象 -主持人然后要求容器提供视图 -容器加载View并为其提供有关ViewModel的信息(操作方式取决于平台和View). -演示者然后可以以某种方式在显示屏(通常是窗口)上实际显示视图".


However, normally in mvvmcross ViewModels and Applications don't talk directly to the Container - instead, they talk to a Presenter and it may be better to modify your code to use a presenter instead. Within this flow your menu would - send the Presenter ViewModelRequest objects - the Presenter then asks the Container for a View - the Container loads the View and provides it with info on it's ViewModel (how this is done depends on the platform and on the View). - the Presenter can then actually show the View on the display (normally a window) somehow.

在其他框架中,您可能会听到使用术语navigation service而不是Mvx称为Presenter的情况

In other frameworks, you may hear the term navigation service used instead of what Mvx calls Presenter

这篇关于使用MVVMCROSS的WPF应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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