简体   繁体   English

在哪里初始化 Xamarin.iOS 应用程序中实例化的 PCL?

[英]Where to initialize PCL instanced in a Xamarin.iOS app?

I'm working on a Xamarin application, which I will at first have working on iOS, but plan to later expand to Android and other mobile platforms.我正在开发一个 Xamarin 应用程序,我首先将在 iOS 上工作,但计划稍后扩展到 Android 和其他移动平台。

As such, I'm trying to keep as much common code in PCLs as possible.因此,我试图在 PCL 中保留尽可能多的通用代码。

My question: what is the best practise - in Xamarin.iOS for now - to initialize any dependent PCL code?我的问题:什么是最佳实践 - 现在在 Xamarin.iOS 中 - 初始化任何依赖的 PCL 代码?

For now I have it in the RootViewController inside ViewDidLoad()现在我在ViewDidLoad()RootViewController有它

public override void ViewDidLoad()
{
    base.ViewDidLoad();
    _engine = new MyEngine();
    View = new MainView(_engine);
}

Is this the right spot?这是正确的地方吗? I'd considered putting it in the ctor for the RootViewController , but there's a fair bit going on in the initialization code, which thus ran against "don't put heavy duty init code into a constructor".我曾考虑将它放在RootViewController的构造函数中,但是在初始化代码中发生了相当多的事情,因此与“不要将重型初始化代码放入构造函数中”相悖。

Things that happen are:发生的事情是:

  • Load app settings加载应用设置
  • If app is run for first time ever, load basic defaults如果应用程序是第一次运行,加载基本默认值
  • Initialise other PCL libraries, such as a TextToSpeech module, a state engine (hence the name of the class above), etc初始化其他 PCL 库,例如 TextToSpeech 模块、状态引擎(因此是上面类的名称)等
  • Prepare a data grid based on XML or JSON input根据 XML 或 JSON 输入准备数据网格

Alternately, I though it should possibly go into the AppDelegate section, but that didn't sound right.或者,我认为它应该进入AppDelegate部分,但这听起来不对。

I'm still fairly new to mobile app dev in general and Xamarin in specific, though I've done C# native code for Windows for years.尽管我已经为 Windows 编写了 C# 本机代码多年,但我对移动应用程序开发和 Xamarin 总体上还是相当陌生。 I just want to make sure I follow best practises, but there doesn't seem to be a "thou shalt" in this case.我只是想确保我遵循最佳实践,但在这种情况下似乎没有“你应该”。

Edit: I've extracted the solution based on @wishmaster's suggestions.编辑:我已经根据@wishmaster 的建议提取了解决方案。

For iOS the Appdelegate method is the best place for initialization code.对于 iOS,Appdelegate 方法是初始化代码的最佳位置。 The appdelegate also provides multiple delegate methods to give you feedback on application lifecyle events such as the method " DidFinishLauchingWithOptions " . appdelegate 还提供了多个委托方法来为您提供有关应用程序生命周期事件的反馈,例如方法“ DidFinishLauchingWithOptions ”。 if you have a lot of data to download or long running tasks that your app depends on I would suggest you take a look backgrounding for iOS .如果您有大量数据要下载或您的应用程序依赖的长时间运行任务,我建议您查看iOS 的背景 A technique I have also used is for my first viewcontroller on IOS (or activity on Android) to display a splash screen and a loading indicator while i run some code to refresh the cache.我还使用过的一项技术是让我在 IOS(或 Android 上的活动)上的第一个视图控制器在我运行一些代码以刷新缓存时显示启动画面和加载指示器。

Using @wishmaster's pointers, this solution works like a charm:使用@wishmaster 的指针,这个解决方案就像一个魅力:

In AppDelegate.csAppDelegate.cs

// in the global section put any data you may make available elsewhere
private var _engine;
public Engine => _engine;

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    /*
     * Do whatever init needs to happen here, if you need to make this
     * available elsewhere, ensure you have properties or accessors,
     * as above.
     */
    _engine = new MyEngine();
    return true;
}

Then in RootViewController.cs using a similar approach to these examples in Obc-C or Swift you can access the information through a property pointing at the AppDelegate.然后在RootViewController.cs使用与Obc-CSwift 中的这些示例类似的方法,您可以通过指向 AppDelegate 的属性访问信息。

var myappdelegate = UIApplication.SharedApplication.Delegate as AppDelegate;
var engine = myappdelegate.Engine;
View = new MainView(engine);

The result resulted in a snappier start up of the application, because the initialisation now happens during the splash screen and no longer between splash screen and appearance of the UI.结果导致应用程序更快速地启动,因为初始化现在发生在启动画面期间,而不再发生在启动画面和 UI 外观之间。

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

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