简体   繁体   中英

MvvmCross Xamarin IOS “Could not find view for ViewModel”

I use Storyboard with MvvmCross and Xamarin iOS. A I'm getting exception. Here is stack trace:

{System.Collections.Generic.KeyNotFoundException: Could not find view for Kosht.Core.ViewModels.TutorialViewModel at MvvmCross.Views.MvxViewsContainer.GetViewType (System.Type viewModelType) [0x00081] in :0 at MvvmCross.Presenters.MvxAttributeViewPresenter.GetPresentationAttribute (MvvmCross.ViewModels.MvxViewModelRequest request) [0x0000c] in :0 at MvvmCross.Presenters.MvxAttributeViewPresenter.Show (MvvmCross.ViewModels.MvxViewModelRequest request) [0x00000] in :0 at MvvmCross.Platforms.Ios.Views.MvxIosViewDispatcher+<>c__DisplayClass2_0.b__0 () [0x0001d] in :0 at MvvmCross.Base.MvxMainThreadAsyncDispatcher+<>c__DisplayClass0_0.b__0 () [0x00000] in :0 at MvvmCross.Base.MvxMainThreadAsyncDispatcher+<>c__DisplayClass1_0+<b__0>d.MoveNext () [0x00011] in :0 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__6_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11 .14.0.14/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018 at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.14.0.14/src/Xamarin.iOS/Foundation/NSAction.cs:178 --- End of stack trace from previous location where exception was thrown --- at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr) at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.14.0.14/src/Xamarin.iOS/UIKit/UIApplication.cs:79 at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0002c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.14.0.14/src/Xamarin.iOS/UIKit/UIApplication.cs:63 at Kosht.iOS.Application.Main (System.String[] args) [0x00001] in /Users/v adimkhadyka/Projects/Kosht/src/Mobile/Kosht.iOS/Main.cs:13 }

My TutorialView:

[MvxViewFor(typeof(TutorialViewModel))]
    [MvxFromStoryboard("Tutorial")]
    [MvxRootPresentation]
    public partial class TutorialView : BaseViewController<TutorialViewModel>
    {
        public TutorialView() : base("TutorialView", null)
        {
        }

        public override void ViewDidLoad()
        {
            base.NavigationController.NavigationBarHidden = true;
            base.ViewDidLoad();
        }
    }

My BaseViewController:

    public class BaseViewController<TViewModel> : MvxViewController,  IBaseView<TViewModel> where TViewModel : BaseViewModel
        {
            protected BaseViewController(string nibName, NSBundle bundle) : base(nibName, bundle)
            {
            }

            public BaseViewController(IntPtr handle) : base(handle)
            {
            }

            public BaseViewController()
            {
            }

            public override void ViewDidLoad()
            {
                base.ViewDidLoad();
                // Perform any additional setup after loading the view, typically from a nib.
            }

            public new TViewModel ViewModel => (TViewModel)base.ViewModel;


            public override void DidReceiveMemoryWarning()
            {
                base.DidReceiveMemoryWarning();
                // Release any cached data, images, etc that aren't in use.
            }
}

If I remove BaseViewController and inherited my TutorialView from MvxViewController , everything works great, but with my base class I'm getting the exception.

Change this line

public class BaseViewController<TViewModel> : MvxViewController,  IBaseView<TViewModel> where TViewModel : BaseViewModel

To

public class BaseViewController<TViewModel> : MvxViewController<TViewModel> where TViewModel : class, IMvxViewModel

And remove this

public new TViewModel ViewModel => (TViewModel)base.ViewModel;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM