Xamarin.Forms 设置视频为启动画面

4

我正在处理一个 xamarin.forms 的共享项目。我在设置视频作为启动画面时遇到了问题。我从这里得到了参考。

我遇到的问题是视频播放器被初始化并执行其过程,在此期间,AppDelegate代码首先返回。所以视频没有显示出来,但声音是有的。我是否漏掉了什么?

在这里,我合并了示例中的VideoControllerVideoViewController。我只使用了VideoViewController,并在SetMoviePlayer()函数中引用我的资源文件夹中的视频。

我尝试过的代码:

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;
        //}
}

VideoViewControllerVideoCutter文件与上面链接中的相同。

谢谢

3个回答

5

您可以通过以下方式在AppDelegate中启动UIViewControl,并使用MessagingCenter通知在Xamarin.forms中启动页面:

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

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

    var control = new VideoViewController();

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

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

    return true;
}

在你的VideoViewController中,当你的视频播放结束时发送MessagingCenter
public override void ViewDidLoad()
{
    View = new UniversalView();
    base.ViewDidLoad();

    // Perform any additional setup after loading the view
            
    NSTimer.CreateScheduledTimer(7, false, (obj) =>
    {
        MessagingCenter.Send<object, object>(this, "ShowMainScreen", null);
    });
}

你可以将发送操作放在 videoCompleteEvent 中。
这里我上传了一个示例,你可以查看: LaunchViewController-xamarin.forms

谢谢@Jack Hua - MSFT,它起作用了!!我没想到消息中心 :) - Gayatri Gokhale
你实际上需要使用NSTimer.CreateScheduledTimer(7, false, (obj)来代替NSTimer.CreateScheduledTimer(7, true, (obj),否则它会在循环中每7秒加载一次主屏幕。 - Luis Lema

1
尝试在RootViewController行后添加MakeKeyAndVisible代码,如下所示:
Window.RootViewController = control;
Window.MakeKeyAndVisible();

似乎视频正在后台播放。 这里有一个简单的例子,展示了Xamarin Native iOS是如何工作的,该示例完全通过编程方式构建(没有Xibs / Storyboard更改),因此可以帮助您更好地理解您正在做什么。

1
在你的情况下,这一行是问题所在:

LoadApplication(new App());

由于这会导致Xamarin将您的VideoViewController替换为其自己的ViewController。因此,您需要在视频完成后将其放在其他地方(我看到您对该事件有一些注释掉的代码)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接