Xamarin.Forms Set video as splash screen

喜欢而已 提交于 2020-07-06 20:20:00

问题


I am working on xamarin.forms shared project. I am facing problem in setting video as splash screen. I got reference from here.

The problem I face is video player is initialized and doing its process and in that time, AppDelegate code returns first. so video is not displayed but its sound is coming. Is there anything I am missing ?

Here I merged VideoController and VideoViewController of the sample. I only use VideoViewController and refer my video from Resources folder in SetMoviePlayer() function

The code I tried :

AppDelegate.cs

[Register("AppDelegate")]
public partial class AppDelegate : Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    public override UIWindow Window { get; set; }
    VideoViewController control = new VideoViewController();

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            try
            {
                Window = new UIWindow(UIScreen.MainScreen.Bounds);

                Window.RootViewController = control;

                //global::Xamarin.Forms.Forms.Init();
                //LoadApplication(new App());

                //control.VideoCompletionEvent += Control_VideoCompletionEvent;   // Tried to invoke this on video completion but doesn't help. AppDelegate returns value first then this event is invoked.
                Task.Delay(7000).Wait();   // video is 7 seconds long                
            }
            catch (Exception ex)
            {
                Console.WriteLine("======== "+ex.Message);
            }
            Window.RootViewController = null;
            global::Xamarin.Forms.Forms.Init();
            LoadApplication(new App());
            return true;
        }
        //private bool Control_VideoCompletionEvent()
        //{
        //    //Window.RootViewController = null;
        //    //global::Xamarin.Forms.Forms.Init();
        //    //LoadApplication(new App());
        //    //return true;
        //}
}

VideoViewController and VideoCutter Files are same as in the link above.

Thank you


回答1:


You can launch the UIViewControl in AppDelegate in following way, and use a MessagingCenter to notify launching the Page in Xamarin.forms:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    global::Xamarin.Forms.Forms.Init();

    Window = new UIWindow(UIScreen.MainScreen.Bounds);

    UIViewController1 control = new UIViewController1();

    Window.RootViewController = control;
    Window.MakeKeyAndVisible();

    MessagingCenter.Subscribe<object, object>(this, "ShowMainScreen", (sender, args) =>
    {
        LoadApplication(new App());
        base.FinishedLaunching(app, options);
    });

    return true;
}

And in your VideoViewController, send the MessagingCenter when you video is finished:

  public override void ViewDidLoad()
        {
            View = new UniversalView();

            base.ViewDidLoad();

            // Perform any additional setup after loading the view


            NSTimer.CreateScheduledTimer(7, true, (obj) =>
            {
                MessagingCenter.Send<object, object>(this, "ShowMainScreen", null);
            });
        }

You can put the send action in the videoCompleteEvent.

Here I uploaded a sample and you can check it: LaunchViewController-xamarin.forms




回答2:


In your case, this line is the problem:

LoadApplication(new App());

As it causes Xamarin to replace your VideoViewController with its own ViewController. So you need to put it somewhere else after the video is completed (I see that you have some commented out code for that event).




回答3:


Try to add the MakeKeyAndVisible line after your RootViewController line as shown:

Window.RootViewController = control;
Window.MakeKeyAndVisible();

It seems like the video is being played in the background.

Here's a simple example to show you how Xamarin Native iOS works as that sample is completely build programmatically (without Xibs/Storyboard changes) so it can help you better understand what you are doing



来源:https://stackoverflow.com/questions/56649595/xamarin-forms-set-video-as-splash-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!