简体   繁体   English

在用户打开应用程序后,如何让我的Xamarin.Forms for iOS和Android应用程序尽快显示启动画面?

[英]How can I make my Xamarin.Forms for iOS and Android application show a splash screen as soon as possible after a user has opened the application?

I'm looking for hints, an example or anything that could guide me with how to make a start up splash screen for Xamarin application Xamarin.Forms application. 我正在寻找提示,示例或任何可以指导我如何为Xamarin应用程序Xamarin.Forms应用程序启动启动屏幕的内容。

I assume I need to make different changes to the iOS and the Android projects and have researched what I can find already. 我假设我需要对iOS和Android项目进行不同的更改,并研究了我已经找到的内容。

However much of the information seems dated or no longer works. 然而,许多信息似乎过时或不再有效。

Would appreciate any suggestions and advice on how to do this for iOS and Android. 非常感谢有关如何为iOS和Android执行此操作的任何建议和建议。

Xamarin.Forms is a solution that, in itself, takes a while to load. Xamarin.Forms是一种解决方案,本身需要一段时间才能加载。 Since all cross-platform things have to wait for Xamarin, there is no cross-platform good implementation of a splash screen. 由于所有跨平台的东西都必须等待Xamarin,因此没有跨平台良好的启动画面实现。

It looks like you'll have to implement it separately for iOS and Android. 看起来你必须分别为iOS和Android实现它。 This will enable the splash screen to show up much faster and before the standard blue Xamarin screen. 这将使启动画面在标准蓝色Xamarin屏幕之前显示得更快。

EDIT: Alan asked for a complete solution, so here it is: 编辑:艾伦要求一个完整的解决方案,所以这里是:

iOS: Make changes to the LaunchScreen.storyboard file in the Resources section of the .iOS project (that's the splash screen, by the way). iOS:对.iOS项目的参考资料部分中的LaunchScreen.storyboard文件进行更改(顺便说一下,这是启动画面)。 I'd suggest opening it in Xcode instead because the storyboard editor of Xcode is frankly just better than Xamarin. 我建议在Xcode中打开它,因为Xcode的故事板编辑器坦率地说比Xamarin好。

Android: You're going to have to have an image here. Android:你将不得不在这里拥有一张图片。 From official Xamarin support - 从官方Xamarin支持 -

The quickest way to render and display the splash screen is to create a custom theme and apply it to an Activity that exhibits the splash screen. 渲染和显示启动画面的最快方法是创建自定义主题并将其应用于展示启动画面的活动。 When the Activity is rendered, it loads the theme and applies the drawable resource (referenced by the theme) to the background of the activity. 呈现活动时,它会加载主题并将可绘制资源(由主题引用)应用于活动的背景。 This approach avoids the need for creating a layout file. 这种方法避免了创建布局文件的需要。 The splash screen is implemented as an Activity that displays the branded drawable, performs any initializations, and starts up any tasks. 启动屏幕实现为活动,显示品牌drawable,执行任何初始化,并启动任何任务。 Once the app has bootstrapped, the splash screen Activity starts the main Activity and removes itself from the application back stack. 应用程序启动后,启动屏幕Activity将启动主Activity并将其自身从应用程序堆栈中移除。

Here's the specific code involved for that. 这是涉及的具体代码。 Below is for a drawable. 下面是一个drawable。 Place it in the Resources/Drawable folder of the Android project. 将它放在Android项目的Resources / Drawable文件夹中。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <color android:color="@color/splash_background"/>
  </item>
  <item>
    <bitmap
        android:src="@drawable/splash"
        android:tileMode="disabled"
        android:gravity="center"/>
  </item>
</layer-list>

This is for a theme (add it to values/styles.xml, or add values/styles.xml if the document doesn't exist.). 这是一个主题(将其添加到values / styles.xml,如果文档不存在,则添加values / styles.xml。)。

<resources>
  <style name="MyTheme.Base" parent="Theme.AppCompat.Light">
  </style>

  <style name="MyTheme" parent="MyTheme.Base">
  </style>

  <style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_screen</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
  </style>
</resources>

Add this code to your Android project as MyTheme.cs: 将此代码作为MyTheme.cs添加到您的Android项目中:

[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : AppCompatActivity
{
    static readonly string TAG = "X:" + typeof(SplashActivity).Name;

    public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
    {
        base.OnCreate(savedInstanceState, persistentState);
        Log.Debug(TAG, "SplashActivity.OnCreate");
    }

    // Launches the startup task
    protected override void OnResume()
    {
        base.OnResume();
        Task startupWork = new Task(() => { SimulateStartup(); });
        startupWork.Start();
    }

    // Simulates background work that happens behind the splash screen
    async void SimulateStartup ()
    {
        Log.Debug(TAG, "Performing some startup work that takes a bit of time.");
        await Task.Delay (8000); // Simulate a bit of startup work.
        Log.Debug(TAG, "Startup work is finished - starting MainActivity.");
        StartActivity(new Intent(Application.Context, typeof (MainActivity)));
    }
}

Then, after you've done all that for Android, remove the MainLauncher attribute from MainActivity, since your launcher is now MyTheme. 然后,在完成Android的所有操作后,从MainActivity中删除MainLauncher属性,因为您的启动器现在是MyTheme。

This is by no means easy, but it's the only way there is right now. 这绝非易事,但这是现在唯一的方式。

Make sure you do not have SimulateStartup sample code. 确保您没有SimulateStartup示例代码。 It used to be boiler plate code a while ago: 它曾经是锅炉板代码前一段时间:

class SplashActivity: SplashActivity类:

It forces an await when the app starts up incase you want to make any service calls on the splash screen 当应用程序启动时,它会强制等待您想要在启动屏幕上进行任何服务调用

MainActivity.cs MainActivity.cs

// Launches the startup task
protected override void OnResume()
{
    base.OnResume();
    Task startupWork = new Task(() => { SimulateStartup(); });
    startupWork.Start();
}

// Simulates background work that happens behind the splash screen
async void SimulateStartup ()
{
    Log.Debug(TAG, "Performing some startup work that takes a bit of time.");
    await Task.Delay (8000); // Simulate a bit of startup work.
    Log.Debug(TAG, "Startup work is finished - starting MainActivity.");
    StartActivity(new Intent(Application.Context, typeof (MainActivity)));
}

Other alternative to improve splash speed: 提高飞溅速度的其他替代方法:

  • use small or native components to create your splash screen 使用小型或本机组件来创建启动画面
  • make sure nothing is on OnResume and not calling any services 确保OnResume上没有任何内容 ,也没有调用任何服务

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

相关问题 如何在Xamarin.Forms应用程序中报告iOS和Android内存以及进程参数? - How can I report on iOS and Android memory and process parameters inside my Xamarin.Forms application? 如何在Xamarin.Forms中删除iOS和Android的启动画面? - How to remove Splash screen for both iOS and Android in Xamarin.Forms? 如何使图像显示在我的 Xamarin.Forms 应用程序的 UWP 版本中 - How do I make images show up in UWP version of my Xamarin.Forms application 运行 Xamarin.Forms 应用程序时如何找出 Android 和 iOS 版本? - How can I find out Android and iOS versions when running a Xamarin.Forms application? 如何使用 Xamarin.forms 打开我的 Android 本机应用程序? - How to open my Android native application with Xamarin.forms? 如何为我的Xamarin.Forms iOS应用添加锁定屏幕? - How can I add a lock screen to my Xamarin.Forms iOS app? 如何用向左滑动的方式编写Xamarin.Forms启动屏幕的代码? - How can I code a Xamarin.Forms splash screen with a left right swipe? 如何为我的Xamarin.Forms应用程序添加链接,以便打开网页? - How can I add a link to my Xamarin.Forms application so it opens a web page? Xamarin.Forms iOS 应用程序在 SplashScreen 后发布时崩溃 - Xamarin.Forms iOS application crash on release after SplashScreen 无法在设备上运行Xamarin.iOS应用程序,在启动画面后退出 - Can't run Xamarin.iOS application on a device, quits after splash screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM