MvxSidebarPresentationAttribute未配置于Presenter字典的异常求助
Got it, let's tackle this System.Collections.Generic.KeyNotFoundException you're hitting with MvxSidebarPresentationAttribute in your MvvmCross 5.6 iOS project. This error pops up because the MvvmCross view presenter hasn't been configured to recognize and handle that sidebar-specific attribute. Here's how to resolve it step by step:
1. Ensure You Have the Correct NuGet Package Installed
First, double-check that your iOS project has the MvvmCross.iOS.Support.Sidebar package installed, and that its version matches your MvvmCore (5.6.x). This package adds all the sidebar presentation logic MvvmCross needs:
- Open the NuGet Package Manager in Visual Studio 2015
- Search for
MvvmCross.iOS.Support.Sidebar - Install the version that aligns with your MvvmCross 5.6 installation (avoid mixing major versions)
2. Configure the Sidebar Presenter in Your Setup Class
Next, you need to tell MvvmCross to use the sidebar presenter instead of the default one, and register the MvxSidebarPresentationAttribute with it. Update your iOS project's Setup.cs file:
using MvvmCross.iOS.Support.Sidebar; using MvvmCross.iOS.Views.Presenters.Attributes; using MvvmCross.Core.ViewModels; namespace YourProjectNamespace.iOS { public class Setup : MvxIosSetup<Core.App> { protected override IMvxIosViewPresenter CreatePresenter() { // Initialize the sidebar presenter with your app delegate and window var sidebarPresenter = new MvxSidebarPresenter(ApplicationDelegate, Window); // Register the attribute mapping so the presenter knows how to handle it sidebarPresenter.RegisterAttributeMapping( typeof(MvxSidebarPresentationAttribute), (attribute, viewType) => new MvxSidebarPresentationAttributeAdapter((MvxSidebarPresentationAttribute)attribute)); return sidebarPresenter; } // Keep any other existing Setup overrides you have here } }
3. Verify Your Attribute Usage
Make sure you're applying the MvxSidebarPresentationAttribute correctly to your view controllers. For example:
Menu View Controller (Sidebar Panel)
using MvvmCross.iOS.Support.Sidebar; using MvvmCross.iOS.Views; [MvxSidebarPresentation( MvxPanelEnum.Left, MvxPanelHintType.PushPanel, false)] // false = don't hide the panel when selecting items public class MenuViewController : MvxViewController<MenuViewModel> { // Your view controller code here }
Center View Controllers
using MvvmCross.iOS.Support.Sidebar; using MvvmCross.iOS.Views; [MvxSidebarPresentation( MvxPanelEnum.Center, MvxPanelHintType.ResetRoot, true)] // true = allow the sidebar to be swiped open from this screen public class FirstViewController : MvxViewController<FirstViewModel> { // Your view controller code here } [MvxSidebarPresentation( MvxPanelEnum.Center, MvxPanelHintType.Push, true)] public class SecondViewController : MvxViewController<SecondViewModel> { // Your view controller code here }
4. Clean and Rebuild Your Project
Sometimes NuGet package references or cached build files can cause issues. Do a full clean and rebuild:
- Go to Build > Clean Solution in Visual Studio
- Then go to Build > Rebuild Solution
- Run the project again on your iPhone 7 (iOS 10.3) simulator
5. Double-Check AppDelegate Setup
Ensure your iOS AppDelegate.cs correctly initializes MvvmCross and uses the setup class:
using MvvmCross.iOS.Views; namespace YourProjectNamespace.iOS { [Register("AppDelegate")] public class AppDelegate : MvxApplicationDelegate { public override UIWindow Window { get; set; } public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) { Window = new UIWindow(UIScreen.MainScreen.Bounds); var setup = new Setup(this, Window); setup.Initialize(); var appStart = Mvx.Resolve<IMvxAppStart>(); appStart.Start(); Window.MakeKeyAndVisible(); return true; } } }
These steps should resolve the KeyNotFoundException by ensuring MvvmCross recognizes and handles the MvxSidebarPresentationAttribute properly.
内容的提问来源于stack exchange,提问作者Mayur Kerasiya

