我正在从事xamarin.forms
共享项目。我在将视频设置为初始屏幕时遇到问题。我从here获得了参考。
我面临的问题是视频播放器已初始化并进行了处理,在此期间,AppDelegate代码首先返回。因此不会显示视频,但会发出声音。我有什么想念的吗?
在这里,我合并了示例的VideoController
和VideoViewController
。我只使用VideoViewController
并在SetMoviePlayer()
函数中从Resources文件夹中引用我的视频
我尝试的代码:
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
和VideoCutter
文件与上面的链接相同。
谢谢
答案 0 :(得分:3)
您可以通过以下方式在AppDelegate中启动UIViewControl,并使用MessagingCenter
通知在Xamarin.forms
中启动Page:
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;
}
视频播放完毕后,在VideoViewController
中发送MessagingCenter
:
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);
});
}
您可以将发送操作放入videoCompleteEvent
。
我在这里上传了一个示例,您可以检查它:LaunchViewController-xamarin.forms
答案 1 :(得分:1)
在您的情况下,这行是问题所在:
LoadApplication(new App());
因为它使Xamarin用自己的VideoViewController
替换您的ViewController
。因此,您需要在视频播放完成后将其放在其他位置(我看到您对该事件有一些注释代码)。
答案 2 :(得分:1)
尝试在您的RootViewController行之后添加MakeKeyAndVisible行,如下所示:
Window.RootViewController = control;
Window.MakeKeyAndVisible();
视频似乎在后台播放。
Here's a simple example向您展示Xamarin Native iOS的工作原理,因为该示例是完全以编程方式构建的(无需Xibs / Storyboard更改),因此可以帮助您更好地了解自己的工作