C# 使用布局Xamarin创建SplashScreen

C# 使用布局Xamarin创建SplashScreen,c#,android,xamarin,C#,Android,Xamarin,我想在Xamarin工作室制作一个splashscreen 我做了以下工作: 使用splashimage创建了我的布局 创建了一个主题(styles.xml),因此标题栏被隐藏 创建了一个活动,该活动设置contentview,然后让线程睡眠 出于某种原因,它不起作用,我希望您能在这里帮助我: SplashScreen.cs(SplashScreen活动) 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用系统线程; 使

我想在Xamarin工作室制作一个splashscreen

我做了以下工作:

  • 使用splashimage创建了我的布局
  • 创建了一个主题(styles.xml),因此标题栏被隐藏
  • 创建了一个活动,该活动设置contentview,然后让线程睡眠
出于某种原因,它不起作用,我希望您能在这里帮助我:

SplashScreen.cs(SplashScreen活动)

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统线程;
使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
名称空间EvoApp
{
[活动(MainLauncher=true,NoHistory=true,Theme=“@style/Theme.Splash”)]
公共课:活动
{
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
this.SetContentView(Resource.Layout.Splash);
ImageView image=findviewbyd(Resource.Id.evolticLogo);
image.SetImageResource(Resource.Drawable.Splash);
《睡眠》(2000年);
启动性(类型(主要活动));
}
}
}
styles.xml

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

真的
因此,这是一个空白的喷溅活动


提前谢谢

屏幕是空白的,因为通过调用
Thread.Sleep
然后在
OnCreateView
中调用
StartActivity
,您首先暂停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));
    });
}
protectedoverride void OnCreate(捆绑包)
{
base.OnCreate(bundle);
this.SetContentView(Resource.Layout.Splash);
ImageView image=findviewbyd(Resource.Id.evolticLogo);
image.SetImageResource(Resource.Drawable.Splash);
System.Threading.Tasks.Task.Run(()=>{
《睡眠》(2000年);
启动性(类型(主要活动));
});
}

屏幕是空白的,因为通过调用
Thread.Sleep
然后在
OnCreateView
中调用
StartActivity
,您首先暂停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));
    });
}
protectedoverride void OnCreate(捆绑包)
{
base.OnCreate(bundle);
this.SetContentView(Resource.Layout.Splash);
ImageView image=findviewbyd(Resource.Id.evolticLogo);
image.SetImageResource(Resource.Drawable.Splash);
System.Threading.Tasks.Task.Run(()=>{
《睡眠》(2000年);
启动性(类型(主要活动));
});
}

这确实有效!但是,在渲染初始屏幕之前,会出现1秒的空白屏幕。你知道如何避免出现这种空白屏幕吗?我针对这一1秒空白屏幕的解决方案是为活动添加一种样式(在“setContentView”之前应用),其背景颜色与我的启动视图相同。这使得splash布局的入口更好这实际上很有效!但是,在渲染初始屏幕之前,会出现1秒的空白屏幕。你知道如何避免出现这种空白屏幕吗?我针对这一1秒空白屏幕的解决方案是为活动添加一种样式(在“setContentView”之前应用),其背景颜色与我的启动视图相同。这使得splash布局的入口更好