xamarin.forms - 创建 View-ViewModel 查找表时出现问题 - 您为 ViewModel 注册了多个 View

标签 xamarin.forms mvvmcross

我使用 MvvmCross 启动了一个 Xamarin.Froms 项目。我按照MvvmCross官方网站上的文档使用Xamarin.Forms启动Android项目。这是我的 Core 项目中的代码:

public class App : MvxApplication
{
    public App()
    {

    }

    public override void Initialize()
    {
        base.Initialize();

        Mvx.IoCProvider.RegisterSingleton(new NavigationStack());
        Mvx.IoCProvider.RegisterSingleton<IMvxAppStart>(new MvxAppStart<MainViewModel>(this, Mvx.IoCProvider.Resolve<IMvxNavigationService>()));
    }
}

public class MainViewModel : BaseViewModel
{
    public MainViewModel(NavigationStack navigationStack) : base(navigationStack)
    {
    }
}

我的 Forms 项目中的代码: MainView.xaml:

<views:MvxContentPage x:TypeArguments="viewModels:MainViewModel"
xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
xmlns:views="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
xmlns:mvx="clr-namespace:MvvmCross.Forms.Bindings;assembly=MvvmCross.Forms"
xmlns:viewModels="clr-namespace:MyApp.Core.ViewModels;assembly=MyApp.Core"
x:Class="MyApp.Forms.Views.MainView">

    <ContentPage.Content>
        <StackLayout Margin="10">
            <Label Text="Subtotal" />
        </StackLayout>
    </ContentPage.Content>

</views:MvxContentPage>

MainView.xaml.cs:

public partial class MainView : MvxContentPage<MainViewModel>
{
    public MainView()
    {
        InitializeComponent();
    }
}

在我的 Android 项目中:

[Activity(
    Label = "MyApp.Droid",
    Theme = "@style/MyTheme",
    MainLauncher = true,
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
    LaunchMode = LaunchMode.SingleTask)]
public class MainActivity : MvxFormsAppCompatActivity<MvxFormsAndroidSetup<Core.App, Forms.App>, Core.App, Forms.App>
{
    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;
        base.OnCreate(bundle);
    }
}

它可以编译,但是当我启动应用程序时,出现异常:

MvvmCross.Exceptions.MvxException: Problem seen creating View-ViewModel lookup table - you have more than one View registered for the ViewModels: 2*MainViewModel (MainActivity,MainView)

最佳答案

如果您的 ViewModel 名为 MainViewModel 并且您的 Forms 页面也是如此,则可能会出现名称冲突,因为 MvvmCross 将有 2 个 View 到 View 模型查找。您可以通过以不同的方式命名您的事件来防止这种情况,例如“FormsActivity.cs”。

你也可以将你的MainViewModel重命名为MvxMainViewModel(无论你喜欢什么),那么这个异常就会消失。

关于xamarin.forms - 创建 View-ViewModel 查找表时出现问题 - 您为 ViewModel 注册了多个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52783130/

相关文章:

xamarin - 在 Xamarin 中处理 MVVMCross 项目时,是否有真正的 axml 编辑器或工具?

xamarin - MvvmCross 中繁重的初始化操作放在哪里

serialization - 使用 MvvmCross ShowViewModel 传递复杂的导航参数

android - 正确的配置更改处理

api - 在后台线程上运行数据下载

c# - 将 'Xamarin.Forms' 添加到...未将对象引用设置为对象的实例。添加 nuget 包时出现错误

xamarin - 指定的子项已有父项。您必须先对 child 的 parent 调用 removeView()。”面对这个问题

c# - "ResolveLibraryProjectImports"任务意外失败

c# - Xamarin.Forms Shell 页面中的自定义 TitleView(顶部栏)

listview - 如何清除/避免 FFImageLoading 在列表项图像中的缓存