繁体   English   中英

如何在 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 个 arguments 的基本构造函数。 相反,我有:

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

public class Setup : MvxIosSetup<App>
{
}

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

编辑

我使用 MvvmCross 7 创建了一个带有 1 个视图模型/控制器的非常小的示例应用程序。 ViewDidLoad方法永远不会在我的MainViewController中被调用。 有人可以告诉我为什么吗? 我把我的代码放在这里:

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

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

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

现在,在您安装在 GitHub 上的示例应用程序中,您可以执行以下操作使其不使用 storyboard:

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

这里的主要问题是您实际上是通过提供此构造函数来告诉 iOS ViewController 具有 storyboard :

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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