如何在没有故事板的情况下在 iOS 中使用 MvvmCross? [英] How to use MvvmCross without storyboards in iOS?
问题描述
当我使用 MvvmCross 5 时,我编写了我所有的视图,并通过在我的 AppDelegate
中编写它来避免在我的 iOS 应用程序中使用故事板:
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();
}
}
但是在 MvvmCross 6.4.2 中,MvxIosSetup
没有接受 2 个参数的基本构造函数.相反,我有:
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?
编辑
我使用 MvvmCross 7 创建了一个非常小的示例应用程序,其中包含 1 个视图模型/控制器.ViewDidLoad
方法从未在我的 MainViewController
中被调用.有人可以告诉我为什么吗?我把我的代码放在这里:
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
推荐答案
Setup.cs 和 AppDelegate.cs 文件都与您是否呈现故事板无关.通常取决于您将哪些 Presentation Attributes 应用于 ViewController.
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.
但是,Info.plist 中设置的一些内容会改变 MvvmCross 期望的设置方式.
However, there are some stuff set in the Info.plist that changes how MvvmCross expects things to be set up.
现在在 GitHub 上的示例应用程序中,您可以执行以下操作以使其不使用故事板:
Now in your example App you've put up on GitHub, you can do the following to make it not use the storyboard:
- 删除
Main.storyboard
- 在 Info.plist 中将 Main interface 设置为 (none)
- 在 Info.plist 中将启动图像设置为 LaunchScreen.storyboard
- 移除 Info.plist 中的场景委托内容
- 移除
MainViewController
中的构造函数
- Remove
Main.storyboard
- In Info.plist set Main interface to (none)
- In Info.plist set launch images to LaunchScreen.storyboard
- Remove the scene delegate stuff in Info.plist
- Remove the constructor in
MainViewController
这里的主要问题是,您实际上是在通过提供此构造函数来告诉 iOS ViewController 有一个故事板:
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)
{
}
还通过 Info.plist 告诉 iOS 使用故事板和所有场景委托内容.
Also are telling iOS to use storyboards through the Info.plist with all the scene delegation stuff.
我创建了一个 PR,显示需要更改哪些内容以使其在没有 storboard 的情况下运行,并显示您在 ViewController 上设置的蓝色背景颜色.
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
这篇关于如何在没有故事板的情况下在 iOS 中使用 MvvmCross?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!