You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于MvvmCross ViewModel创建Xamarin.Forms页面并自动匹配?

How to Auto-Map MvvmCross ViewModels to Pages in Xamarin.Forms

Great question! Let's walk through how to get MvvmCross to automatically map your MasterMenuViewModel to MasterMenuPage in Xamarin.Forms, just like you'd do in Xamarin Native—no manual page type required.

1. Stick to MvvmCross Naming Conventions

MvvmCross relies on default naming rules to auto-discover views for viewmodels, and you're already off to a perfect start:

  • Your viewmodel: MasterMenuViewModel
  • Your page: public partial class MasterMenuPage : MvxContentPage<MasterMenuViewModel>

As long as the prefix matches (both start with MasterMenu) and the suffixes are ViewModel/Page, MvvmCross will recognize this pair automatically. This is the core of the "auto-detect" behavior you're looking for.

2. Ensure MvvmCross Scans Your View Assembly

MvvmCross needs to know where your pages live. Update your platform-specific Setup classes to include the assembly containing your MasterMenuPage:

Android Setup Example

public class Setup : MvxFormsAndroidSetup<Core.App, App>
{
    protected override IEnumerable<Assembly> GetViewAssemblies()
    {
        var assemblies = base.GetViewAssemblies().ToList();
        // Add the assembly where your MasterMenuPage is located
        assemblies.Add(typeof(MasterMenuPage).Assembly);
        return assemblies;
    }
}

iOS Setup Example

public class Setup : MvxFormsIosSetup<Core.App, App>
{
    protected override IEnumerable<Assembly> GetViewAssemblies()
    {
        var assemblies = base.GetViewAssemblies().ToList();
        assemblies.Add(typeof(MasterMenuPage).Assembly);
        return assemblies;
    }
}

If your pages are in the same assembly as the Setup class, this step might not be necessary, but adding it explicitly avoids any scanning gaps.

3. Navigate to the ViewModel (Not the Page)

Instead of manually instantiating MasterMenuPage, use MvvmCross's IMvxNavigationService to navigate directly to the viewmodel. This is where the magic happens—MvvmCross will auto-load the corresponding page for you:

public class MainViewModel : MvxViewModel
{
    private readonly IMvxNavigationService _navigationService;

    public MainViewModel(IMvxNavigationService navigationService)
    {
        _navigationService = navigationService;
        OpenMasterMenuCommand = new MvxAsyncCommand(OpenMasterMenu);
    }

    public IMvxAsyncCommand OpenMasterMenuCommand { get; }

    private async Task OpenMasterMenu()
    {
        // MvvmCross automatically finds and loads MasterMenuPage here
        await _navigationService.Navigate<MasterMenuViewModel>();
    }
}

4. Verify Your Forms App Integration

Make sure your Xamarin.Forms App class inherits from MvxFormsApplication to hook into MvvmCross's lifecycle:

public partial class App : MvxFormsApplication
{
    public App()
    {
        InitializeComponent();
    }

    protected override void OnCreate()
    {
        base.OnCreate();
        // Set your initial viewmodel to kick off the app
        Mvx.IoCProvider.RegisterSingleton<IMvxAppStart>(new MvxFormsAppStart<MainViewModel>());
    }
}

Behind the Scenes

MvvmCross uses the MvxDefaultViewLocator by default, which looks for views that follow the pattern: [ViewModelNameWithoutSuffix]Page. As long as your naming is consistent and your views are in a scanned assembly, this locator handles the mapping automatically—no manual registration needed.

内容的提问来源于stack exchange,提问作者Agat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:16:51