Xamarin iOS无法在没有故事板和使用MVVMCross 7.0.0的情况下启动第一个视图控制器

nuypyhwy  于 2023-09-28  发布在  iOS
关注(0)|答案(1)|浏览(123)

在我将xamarin ios应用程序中的mvvmcross更新到v7.0.0后,我无法让它运行。它只是冻结在启动画面中,并没有启动第一个视图控制器。我检查了AppDelegate和安装文件,一切正常。视图控制器构造函数被命中,视图模型也被命中,但视图控制器中的ViewDidLoad方法没有被命中。

[Register("AppDelegate")]
    [assembly: Preserve(typeof(MvxIosSetup<App>))]
    public partial class AppDelegate : MvxApplicationDelegate<MvxIosSetup<App>, App>
    {
        /// <summary>
        /// UIApplicationDelegate.Window doesn't really exist / work. It was added by Xamarin.iOS templates 
        /// </summary>
        public new virtual UIWindow Window { get; set; }

        public AppDelegate() : base()
        {
            RegisterSetup();
        }

        public override void WillEnterForeground(UIApplication application)
        {
            FireLifetimeChanged(MvxLifetimeEvent.ActivatedFromMemory);
        }

        public override void DidEnterBackground(UIApplication application)
        {
            FireLifetimeChanged(MvxLifetimeEvent.Deactivated);
        }

        public override void WillTerminate(UIApplication application)
        {
            FireLifetimeChanged(MvxLifetimeEvent.Closing);
        }

        public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            Window = new UIWindow(UIScreen.MainScreen.Bounds);

            var setup = new Setup();
            setup.InitializePrimary();
            setup.InitializeSecondary();
            setup.PlatformInitialize(this, Window);

            var myFirstViewController = new LoginView();
            Window.RootViewController = myFirstViewController;

            var startup = Mvx.IoCProvider.Resolve<IMvxAppStart>();
            startup.Start();

            Window.MakeKeyAndVisible();

                   

            SetupUiStyles();

            return true;
        }

        private void FireLifetimeChanged(MvxLifetimeEvent which)
        {
            var handler = LifetimeChanged;
            handler?.Invoke(this, new MvxLifetimeEventArgs(which));
        }

        public event EventHandler<MvxLifetimeEventArgs> LifetimeChanged;

        private void SetupUiStyles()
        {
            UINavigationBar.Appearance.TintColor = UIColor.Black;
            UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.Default;

            if (UIDevice.CurrentDevice.CheckSystemVersion(15, 0))
            {
                var appearance = new UINavigationBarAppearance()
                {
                    BackgroundColor = UIColor.FromRGB(99, 183, 105),
                    TitleTextAttributes = new UIStringAttributes() { ForegroundColor = UIColor.White }
                };


                UINavigationBar.Appearance.StandardAppearance = appearance;
                UINavigationBar.Appearance.CompactAppearance = appearance;
                UINavigationBar.Appearance.ScrollEdgeAppearance = appearance;
            }

            // Disable check for delegate in event registration: https://forums.xamarin.com/discussion/39470/installed-5-9-build-431-event-registration-is-overwriting-existing-delegate-error
            UIApplication.CheckForEventAndDelegateMismatches = false;
        }
       
    }
public class Setup : MvxIosSetup<App>
    {
        public Setup()
        {

        }

        protected override IMvxApplication CreateApp()
        {
            return new App();
        }

        protected override IMvxIosViewPresenter CreateViewPresenter()
        {
            return new MvxIosViewPresenter(ApplicationDelegate, Window);
        }

        protected override void InitializeFirstChance()
        {           
            base.InitializeFirstChance();
        }

        public override void InitializeSecondary()
        {
            Mvx.IoCProvider.RegisterType<LoginViewModel, LoginViewModel>();
            InitializeViewModelTypeFinder();
            base.InitializeSecondary();
        }

        protected override IMvxNameMapping InitializeViewModelTypeFinder()
        {
            var viewModelByNameRegistry = CreateViewModelByNameRegistry();

            var viewModelAssemblies = GetViewModelAssemblies();

            foreach(var assemly in viewModelAssemblies)
            {
                viewModelByNameRegistry.AddAll(assemly);
            }

            var nameMapping = CreateViewToViewModelNaming();
            Mvx.IoCProvider.RegisterSingleton(nameMapping);

            return nameMapping;
        }

    }
public class BaseView<T> : MvxViewController where T : MvxViewModel
    {
        protected BaseView(string name, NSBundle bundle = null)
        {

        }

        public new T ViewModel
        {
            get
            {
                return base.ViewModel as T;
            }
            set
            {
                base.ViewModel = value;
            }

        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
        }
}
[MvxRootPresentation]
    public partial class LoginView : BaseView<LoginViewModel>, IBaseView
    {
        public LoginView()
            : base("LoginView", null)
        {
           // being hit
        }

       public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            //not being hit
        }
    }

我试了上面的代码和其他东西。有时ViewModel没有绑定到View(DataContext null)其他时间集。Apply()得到错误。但当它工作时,LoginView不显示,应用程序冻结在启动屏幕上

9fkzdhlc

9fkzdhlc1#

谢谢Cheesebaron。我在阅读你的评论之前就知道错了,但你是对的。
这是我最后一个FinishedLaunching方法,没有Window。
var startup = Mvx.IoCProvider.Resolve();
int findDuplicate();

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            Window = new UIWindow(UIScreen.MainScreen.Bounds);

            Window.RootViewController = new UINavigationController();

            var result = base.FinishedLaunching(application, launchOptions);

            SetupUiStyles();

            return result;
        }
public class Setup : MvxIosSetup<App>
    {
        protected override void InitializeFirstChance()
        {
            base.InitializeFirstChance();

            Mvx.IoCProvider.RegisterType<IMvxJsonConverter, MvxJsonConverter>();
        }

        protected override void InitializeLastChance()
        {
            base.InitializeLastChance();
        }

        protected override IMvxIocOptions CreateIocOptions()
        {
            return new MvxIocOptions
            {
                PropertyInjectorOptions = MvxPropertyInjectorOptions.MvxInject
            };
        }
    }

MVVMCross 6.4.x及更高版本(6.4.2、7.0.0 -我测试过)的设置文件不需要CreateApp方法
而我的基本ViewController和ViewControllers,则不需要有构造函数,因为我没有使用故事板

public class BaseView<T> : MvxViewController<T> where T : MvxViewModel
    {
    //no constructor 
    }
[MvxRootPresentation]
    public partial class LoginView : BaseView<LoginViewModel>, IBaseView
    {
       public LoginView()
       {
       //no base reference  like before
       }
    }

最后,我从LoginView中删除了一些NavigatorController,我们曾经在那里设置了一些属性

相关问题