How to use MvvmCross without storyboards in iOS?

浪尽此生 提交于 2021-01-28 19:39:02

问题


When I was using MvvmCross 5, I coded all my views and avoided using storyboards for my iOS app by writing this in my AppDelegate:

[Register("AppDelegate")]
public class AppDelegate : MvxApplicationDelegate
{
    private MvxIosViewPresenter viewPresenter;

    public override UIWindow Window
    {
        get;
        set;
    }

    /// <summary>
    /// MvvmCross Mods:
    /// - Creates a new presenter, which determines how Views are shown
    /// - This example uses a standard presenter.
    /// </summary>
    /// <param name="application"></param>
    /// <param name="launchOptions"></param>
    /// <returns></returns>
    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        // create a new window instance based on the screen size
        Window = new UIWindow(UIScreen.MainScreen.Bounds);

        // MvvmCross Mod Start---------------------------------------------

        // This class will determine how Views are shown
        this.viewPresenter = new MvxIosViewPresenter(this, Window);//new ViewPresenter(Window);

        // Init the Setup object, which initializes App.cs
        var setup = new Setup(this, this.viewPresenter);
        setup.Initialize();

        //this.viewPresenter.PresentModalViewController(new ListenViewController(), true);

        // Use IoC to find and start the IMvxAppStart object
        var startup = Mvx.Resolve<IMvxAppStart>();
        startup.Start();

        // MvvmCross Mod End--------------------------------------------------

        // make the window visible
        Window.MakeKeyAndVisible();

        return true;
    }

public class Setup : MvxIosSetup
{
    public Setup(MvxApplicationDelegate appDelegate, IMvxIosViewPresenter presenter)
        : base(appDelegate, presenter)
    {
    }

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

    protected override IMvxTrace CreateDebugTrace()
    {
        return new DebugTrace();
    }
}

But in MvvmCross 6.4.2, MvxIosSetup does not have the base constructor that takes 2 arguments. Instead I have:

[Register(nameof(AppDelegate))]
public partial class AppDelegate : MvxApplicationDelegate<Setup, App>
{
}

public class Setup : MvxIosSetup<App>
{
}

How can I configure it so that I can code my views without storyboards?

EDIT

I created a very small sample app with 1 view model/controller, using MvvmCross 7. The ViewDidLoad method never gets called in my MainViewController. Can someone please tell me why? I put my code here:

https://github.com/sinight95/TestApp/tree/main/TestApp


回答1:


Neither the Setup.cs nor AppDelegate.cs files have anything to do whether you are presenting a storyboard or not. It is usually all up to which Presentation Attributes you are applying to a ViewController.

However, there are some stuff set in the Info.plist that changes how MvvmCross expects things to be set up.

Now in your example App you've put up on GitHub, you can do the following to make it not use the storyboard:

  1. Remove Main.storyboard
  2. In Info.plist set Main interface to (none)
  3. In Info.plist set launch images to LaunchScreen.storyboard
  4. Remove the scene delegate stuff in Info.plist
  5. Remove the constructor in MainViewController

What is the main issue here is that you are essentially telling iOS that the ViewController has a storyboard by supplying this constuctor:

public MainViewController() : base(nameof(MainViewController), null)
{
}

Also are telling iOS to use storyboards through the Info.plist with all the scene delegation stuff.

I've created a PR showing what needs to be changed to make it run without the storboard and show the blue background color you've set on your ViewController.

https://github.com/sinight95/TestApp/pull/1



来源:https://stackoverflow.com/questions/65488504/how-to-use-mvvmcross-without-storyboards-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!