问题描述
我正在使用MvvmCross框架编写一个Xamarin.iOS
,Xamarin.Android
和UWP
跨平台应用程序.
I am writing a Xamarin.iOS
, Xamarin.Android
and UWP
cross-platform application with the MvvmCross framework.
我正在制作一个具有LoginViewModel的LoginPage.在Xamarin.iOS
,Xamarin.Android
项目中,ViewModel和下面的View的绑定工作正常
I am making a LoginPage which has a LoginViewModel. In the Xamarin.iOS
, Xamarin.Android
projects, the binding of the ViewModel and the View with below works just fine
public class LoginActivity : MvxAppCompatActivity<LoginViewModel>
public partial class LoginViewController : MvxViewController<LoginViewModel>
尝试在UWP
项目上执行与上述相同的操作,但出现一些错误.
Trying to do the same as above on UWP
project, I get some error.
在XAML中:
<views:MvxWindowsPage
x:TypeArguments="viewModels:LoginViewModel" x:Class="MyApp.UWP.Views.LoginView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="using:MvvmCross.WindowsUWP.Views"
xmlns:viewModels="using:MyApp.PresentationCore.ViewModels"
mc:Ignorable="d">
我的C#代码是
public sealed partial class LoginView : MvxWindowsPage<LoginViewModel>
但是我得到编译错误.我该如何解决?
But I get compilation errors. How can I resolve them?
我认为错误有点模棱两可,因为在第一个错误中没有模板版本,但是在第三个错误中则涉及到违反模板约束的情况.
I think the errors are a little ambiguous because at the first error there is no templated version, but the third error is about a template constraint violation.
我知道有一个将ViewModel和View与命名约定或属性绑定的选项,但是我想使用这种强类型的解决方案.
I know there is an option binding the ViewModel and the View with naming convention or attributes, but I would like to use this strongly typed solution.
推荐答案
不幸的是,我相信UWP不支持基本页面的TypeArguments
以及随后的泛型类型参数.您可以在 Prism GitHub线程中进行讨论.因此,您将不得不使用其他注册选项之一.
Unfortunately I believe UWP does not support TypeArguments
and subsequently generic types parameters for a base pages. You can check out the Prism GitHub thread where they discuss it. So you will have to go with one of the other registration options.
Mvvmcross提供了多种将View
注册到ViewModel
的替代方法.在Mvvmcross初始化期间,它将尝试使用ViewModel注册到您的View
. cs#L31"rel =" nofollow> MvxViewModelViewTypeFinder
的顺序如下:
Mvvmcross offers various alternative approaches for registering your View
to a ViewModel
. During initialisation of Mvvmcross it will attempt to register your ViewModel
to your View
using MvxViewModelViewTypeFinder
in the following order:
基于属性的注册:
Attribute based registration:
您可以在页面类中添加MvxViewFor
属性.
You can add MvxViewFor
attribute to your page class.
[MvxViewFor(typeof(FirstViewModel))]
public sealed partial class FirstView : MvxWindowsPage
{
public FirstView()
{
this.InitializeComponent();
}
}
基于具体类型的注册:
Concrete type based registration:
您可以通过指定ViewModel的具体类型将View
注册到ViewModel
.
You can register your View
to your ViewModel
by specifying the concrete type of the ViewModel.
public sealed partial class FirstView : MvxWindowsPage
{
public new FirstViewModel ViewModel => base.ViewModel as FirstViewModel;
public FirstView()
{
this.InitializeComponent();
}
}
或者在可以使用通用基类的Android和iOS上:
Or in the case of Android and iOS where generic base class can be used:
// Android
public class FirstActivity : MvxAppCompatActivity<FirstViewModel>
// iOS
public class FirstViewController : MvxViewController<FirstViewModel>
基于公约的注册:
Convention based registration:
您可以让视图和ViewModel遵循相同的命名约定,Mvvmcross会为您映射它们(xxxView和xxxViewModel)
You can have your view and ViewModel follow the same naming convention and Mvvmcross will map them for you (xxxView and xxxViewModel)
查看
public sealed partial class FirstView : MvxWindowsPage
{
public FirstView()
{
this.InitializeComponent();
}
}
ViewModel
public class FirstViewModel : MvxViewModel
这篇关于MvvmCross MvxWindowsPage< TViewModel>编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!