使用布局 Xamarin 创建初始屏幕

本文关键字:屏幕 创建 Xamarin 布局 | 更新日期: 2023-09-27 18:36:05

我试图在Xamarin Studio中创建启动画面。

我做了以下工作:

  • 使用启动图像创建了我的布局。
  • 创建了一个主题(样式.xml),以便隐藏标题栏。
  • 创建了一个活动,用于设置内容视图,然后让线程休眠。

由于某种原因它不起作用,我希望你能在这里帮助我:

初始屏幕.cs(初始屏幕活动)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace EvoApp
{
    [Activity (MainLauncher = true, NoHistory = true, Theme = "@style/Theme.Splash")]           
    public class SplashScreen : Activity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            this.SetContentView (Resource.Layout.Splash);
            ImageView image = FindViewById<ImageView> (Resource.Id.evolticLogo);
            image.SetImageResource (Resource.Drawable.Splash);
            Thread.Sleep (2000);
            StartActivity (typeof(MainActivity));
        }
    }
}

样式.xml

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
  <style name="Theme.Splash" parent="android:Theme">
    <item name="android:windowNoTitle">true</item>
  </style>
</resources>

所以这样做的结果是一个空白的飞溅活动。

提前感谢!

使用布局 Xamarin 创建初始屏幕

屏幕是空白的,因为通过调用Thread.Sleep然后在OnCreateViewStartActivity,您首先暂停UI线程(这将不会导致任何内容显示),然后立即使用StartActivity退出活动。

要解决此问题,请将Thread.Sleep()StartActivity()转移到后台线程中:

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);
    this.SetContentView (Resource.Layout.Splash);
    ImageView image = FindViewById<ImageView> (Resource.Id.evolticLogo);
    image.SetImageResource (Resource.Drawable.Splash);
    System.Threading.Tasks.Task.Run( () => {
        Thread.Sleep (2000);
        StartActivity (typeof(MainActivity));
    });
}