如何基于MvvmCross ViewModel创建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

