如何在 iOS 中使用没有故事板的 MvvmCross?

How to use MvvmCross without storyboards in iOS?

当我使用 MvvmCross 5 时,我对所有视图进行了编码,并通过在我的 AppDelegate:

中编写此代码来避免为我的 iOS 应用程序使用故事板
[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();
    }
}

但是在 MvvmCross 6.4.2 中,MvxIosSetup 没有接受 2 个参数的基本构造函数。相反,我有:

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

public class Setup : MvxIosSetup<App>
{
}

如何配置它以便在没有故事板的情况下编写我的视图?

编辑

我使用 MvvmCross 7 创建了一个带有 1 个视图 model/controller 的非常小的示例应用程序。ViewDidLoad 方法从未在我的 MainViewController 中被调用。有人可以告诉我为什么吗?我把我的代码放在这里:

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

无论您是否展示故事板,Setup.cs 和 AppDelegate.cs 文件都没有任何关系。通常取决于您要将哪些演示属性应用于 ViewController。

但是,Info.plist 中设置了一些内容,改变了 MvvmCross 期望设置的方式。

现在,在您放置在 GitHub 上的示例应用程序中,您可以执行以下操作以使其不使用故事板:

  1. 删除Main.storyboard
  2. 在 Info.plist 中将主界面设置为 (none)
  3. 在 Info.plist 中将启动图像设置为 LaunchScreen.storyboard
  4. 删除 Info.plist
  5. 中的场景委托内容
  6. 删除MainViewController
  7. 中的构造函数

这里的主要问题是你实际上是在告诉 iOS ViewController 通过提供这个构造函数有一个故事板:

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

还告诉 iOS 通过 Info.plist 使用故事板和所有场景委托内容。

我创建了一个 PR,显示需要更改哪些内容才能使其 运行 没有 storboard,并显示您在 ViewController 上设置的蓝色背景颜色。

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