Wpf 带有 MvvmCross 的多个嵌套用户控件 [英] Wpf Multiple nested Usercontrols with MvvmCross
问题描述
我是 MvvmCross
的新手,但我使用 mvvm
有一段时间了.我知道如何用嵌套的用户控件组合用户控件.现在使用 mvvmcross
我不得不在另一个用户控件中显示两个或多个用户控件.除了 MvvmCross
,我不使用任何其他框架.我的 Rootview 看起来像这样:
`<DockPanel><TextBlock Text="Root";DockPanel.Dock=顶部"/><ContentControl x:Name=MainMenuVM"内容={Binding MainMenuVM}"DockPanel.Dock=顶部"/></DockPanel></views:MvxWpfView>`
对应的ViewModel如下所示:
使用 MvvmCross.Commands;使用 MvvmCross.Logging;使用 MvvmCross.Navigation;使用 MvvmCross.ViewModels;命名空间 MvvmCrossTest.Core.ViewModels{公共类 RootViewModel:MvxNavigationViewModel{私有只读 IMvxViewModelLoader _mvxViewModelLoader;公共 RootViewModel(IMvxLogProvider logProvider, IMvxNavigationService navigationService, IMvxViewModelLoader mvxViewModelLoader) : base(logProvider, navigationService){_mvxViewModelLoader = mvxViewModelLoader;ShowMainMenu();}私有 MainMenuViewModel _mainMenuVM;public MainMenuViewModel MainMenuVM{得到 { 返回 _mainMenuVM;}放{SetProperty(ref _mainMenuVM, value);RaisePropertyChanged(() => MainMenuVM);}}公共 MvxCommand ShowMainMenuCommand { 获取;放;}公共无效 ShowMainMenu(){MainMenuVM = (MainMenuViewModel)_mvxViewModelLoader.LoadViewModel(MvxViewModelRequest.GetDefaultRequest(typeof(MainMenuViewModel)), null, null);}}}
我想在内容控件中显示的简化视图如下所示:
<网格></网格></views:MvxWpfView>
还有相应的 ViewModel.
使用 MvvmCross.Logging;使用 MvvmCross.Navigation;使用 MvvmCross.ViewModels;命名空间 MvvmCrossTest.Core.ViewModels{公共类 MainMenuViewModel : MvxNavigationViewModel{public MainMenuViewModel(IMvxLogProvider logProvider, IMvxNavigationService navigationService) : base(logProvider, navigationService){}}}
我看到的是MvvmCrossTest.Core.ViewModels.MainMenuViewModel"的文字,而不是 View/ViewModel.
我不想使用 Xamarin!
我创建了一个项目,其中我向您展示一些基础知识.
简单
您可以在此处找到示例.>
RootView
;
NestedView
;
<网格><Label Content="Hello from NestedView";/></网格></views:MvxWpfView>
扩展(自定义演示者)
使用此自定义 wpf 演示器,您可以在容器中指定它们时轻松导航到视图.这样,您就可以在使用 mvvmcross 导航服务时从视图模型关闭和打开视图.
我从另一个项目复制了这个.您可以在我的示例项目此处中找到整个实现.
RootView
;
<网格><标签内容=你好"/><ItemsControl region:MvxContainer.Id=RootViewRegion"/></网格></views:MvxWpfView>
后面的RootView
代码;
[MvxContentPagePresentation(WrapInNavigationPage = true, NoHistory = false)]公共部分类 RootView:MvxWpfView{公共根视图(){初始化组件();}}
RootViewModel
;
公共类RootViewModel:MvxNavigationViewModel{私有只读 IMvxNavigationService _navigationService;公共 IMvxAsyncCommand ShowNestedViewModelCommand { 获取;保护集;}公共 RootViewModel(IMvxLogProvider logProvider, IMvxNavigationService navigationService) : base(logProvider, navigationService){this._navigationService = 导航服务;//导航到嵌套视图this.ShowNestedViewModelCommand = new MvxAsyncCommand(() => this._navigationService.Navigate());ShowNestedViewModelCommand.Execute();}}
后面的NestedView
代码;
[MvxWpfPresenter(RootViewRegion", mvxViewPosition.NewOrExsist)]公共部分类 NestedView : MvxWpfView{公共嵌套视图(){初始化组件();}}
很高兴知道
当您遇到导航和视图模型连接问题时,请使用后面的代码.它加载特定视图的视图模型;
[MvxContentPagePresentation(WrapInNavigationPage = true, NoHistory = false)]公共部分类 SomeView : MvxWpfView{公共 SomeView(){初始化组件();if (!(ViewModel 是 ViewModels.SomeViewModel)){if (Mvx.IoCProvider.TryResolve(out var someViewModel)){ViewModel = someViewModel;返回;}var _viewModelLoader = Mvx.IoCProvider.Resolve();var request = new MvxViewModelInstanceRequest(typeof(ViewModels.SomeViewModel));request.ViewModelInstance = _viewModelLoader.LoadViewModel(request, null);ViewModel = request.ViewModelInstance as ViewModels.SomeViewModel;Mvx.IoCProvider.RegisterSingleton(ViewModel);}}}
I'm pretty new in MvvmCross
but I'm working with mvvm
for awhile. I know how to compose usercontrols with nested usercontrols. Now with mvvmcross
I got stucked to show two or more usercontrols in another usercontrol. I don't use any other framework than MvvmCross
.
My Rootview looks like this:
`<views:MvxWpfView
x:Class="MvvmCrossTest.Wpf.Views.RootView"
xmlns:views="clr-namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<DockPanel>
<TextBlock Text="Root" DockPanel.Dock="Top"/>
<ContentControl x:Name="MainMenuVM" Content="{Binding MainMenuVM}" DockPanel.Dock="Top" />
</DockPanel>
</views:MvxWpfView>`
The corresponding ViewModel looks like this:
using MvvmCross.Commands;
using MvvmCross.Logging;
using MvvmCross.Navigation;
using MvvmCross.ViewModels;
namespace MvvmCrossTest.Core.ViewModels
{
public class RootViewModel: MvxNavigationViewModel
{
private readonly IMvxViewModelLoader _mvxViewModelLoader;
public RootViewModel(IMvxLogProvider logProvider, IMvxNavigationService navigationService, IMvxViewModelLoader mvxViewModelLoader) : base(logProvider, navigationService)
{
_mvxViewModelLoader = mvxViewModelLoader;
ShowMainMenu();
}
private MainMenuViewModel _mainMenuVM;
public MainMenuViewModel MainMenuVM
{
get { return _mainMenuVM; }
set
{
SetProperty(ref _mainMenuVM, value);
RaisePropertyChanged(() => MainMenuVM);
}
}
public MvxCommand ShowMainMenuCommand { get; set; }
public void ShowMainMenu()
{
MainMenuVM = (MainMenuViewModel)_mvxViewModelLoader.LoadViewModel(MvxViewModelRequest.GetDefaultRequest(typeof(MainMenuViewModel)), null, null);
}
}
}
The simplified View I want to show in the contentcontrol looks like this:
<views:MvxWpfView
x:Class="MvvmCrossTest.Wpf.Views.MainMenuView"
xmlns:views="clr-namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Background="Aqua"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
</Grid>
</views:MvxWpfView>
And also the corresponding ViewModel.
using MvvmCross.Logging;
using MvvmCross.Navigation;
using MvvmCross.ViewModels;
namespace MvvmCrossTest.Core.ViewModels
{
public class MainMenuViewModel : MvxNavigationViewModel
{
public MainMenuViewModel(IMvxLogProvider logProvider, IMvxNavigationService navigationService) : base(logProvider, navigationService)
{
}
}
}
Instead of the View/ViewModel I see a text that says "MvvmCrossTest.Core.ViewModels.MainMenuViewModel".
I don't want to use Xamarin!
I created a project where I show you some basics.
simple
You can find the samples here.
the RootView
;
<views:MvxWpfView xmlns:local="clr-namespace:SomeProject.Views" ...>
<Grid>
<Label Content="Hello from RootView" />
<local:NestedView />
</Grid>
</views:MvxWpfView>
the NestedView
;
<views:MvxWpfView ...>
<Grid>
<Label Content="Hello from NestedView" />
</Grid>
</views:MvxWpfView>
extended (custom presenter)
With this custom wpf presenter you can easily navigate to views while specified themn in a container. With that, you can close and open views from the view model while using the mvvmcross navigation service.
I copied this from another project. You can find the whole implementation in my sample project here.
the RootView
;
<views:MvxWpfView ...>
<Grid>
<Label Content="Hello" />
<ItemsControl region:MvxContainer.Id="RootViewRegion"/>
</Grid>
</views:MvxWpfView>
the RootView
code behind;
[MvxContentPagePresentation(WrapInNavigationPage = true, NoHistory = false)]
public partial class RootView: MvxWpfView<RootViewModel>
{
public RootView()
{
InitializeComponent();
}
}
the RootViewModel
;
public class RootViewModel: MvxNavigationViewModel
{
private readonly IMvxNavigationService _navigationService;
public IMvxAsyncCommand ShowNestedViewModelCommand { get; protected set; }
public RootViewModel(IMvxLogProvider logProvider, IMvxNavigationService navigationService) : base(logProvider, navigationService)
{
this._navigationService = navigationService;
// navigate to nested view
this.ShowNestedViewModelCommand = new MvxAsyncCommand(() => this._navigationService.Navigate<NestedViewModel>());
ShowNestedViewModelCommand.Execute();
}
}
the NestedView
code behind;
[MvxWpfPresenter("RootViewRegion", mvxViewPosition.NewOrExsist)]
public partial class NestedView : MvxWpfView<NestedViewModel>
{
public NestedView()
{
InitializeComponent();
}
}
good to know
when you have problems with navigation and view model connection use this code behind. It loads the viewmodel for the specific view;
[MvxContentPagePresentation(WrapInNavigationPage = true, NoHistory = false)]
public partial class SomeView : MvxWpfView<ViewModels.SomeViewModel>
{
public SomeView()
{
InitializeComponent();
if (!(ViewModel is ViewModels.SomeViewModel))
{
if (Mvx.IoCProvider.TryResolve<ViewModels.SomeViewModel>(out var someViewModel))
{
ViewModel = someViewModel;
return;
}
var _viewModelLoader = Mvx.IoCProvider.Resolve<IMvxViewModelLoader>();
var request = new MvxViewModelInstanceRequest(typeof(ViewModels.SomeViewModel));
request.ViewModelInstance = _viewModelLoader.LoadViewModel(request, null);
ViewModel = request.ViewModelInstance as ViewModels.SomeViewModel;
Mvx.IoCProvider.RegisterSingleton<ViewModels.SomeViewModel>(ViewModel);
}
}
}
这篇关于Wpf 带有 MvvmCross 的多个嵌套用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!