2017-08-15 47 views
1

在Xamarin Forms for iOS中,我有一个用于显示视频控件的ContentPage的自定义渲染器。在我的Xamarin Forms应用程序中,此自定义ContentPage显示在NavigationPage内。 我希望在通过MQTT发送特定消息时打开视频屏幕。从后台线程推送内容页面冻结应用程序

当我通过单击主屏幕上的链接打开视频页面时,它按预期打开。我知道我正在通过MQTT接收消息,并且由于控制台语句和断点而致电Navigation.PushModalAsync()。但是,自定义渲染页面不会显示,并且我的应用程序的UI每次调用PushModalAsync后都会冻结。

我还需要做些什么才能触发Navigation.PushModalAsync(),这是基于在我的应用程序的后台接收到MQTT通知?

ViewRoomsPage.axml.cs:

[XamlCompilation(XamlCompilationOptions.Compile)] 
public partial class ViewRoomsPage : ContentPage 
{ 
    public ViewRoomsPage() 
    { 
     InitializeComponent(); 
    } 

    public string StreamUri { get; set; } 
} 

ViewRoomsPage.axml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     x:Class="MyForms.Pages.ViewRoomsPage"> 
<ContentPage.Content> 
</ContentPage.Content> 

VideoViewerRenderer.cs(除去视频代码;这应该显示空白红屏它还。当从主屏幕上的按钮启动时工作)

[assembly: ExportRenderer(typeof(ViewRoomsPage), typeof(ViewRoomsRenderer))] 
namespace MyForms.IOS.NativeImplementations 
{ 
    public class ViewRoomsRenderer : PageRenderer 
    { 
     private IJKFFMoviePlayerController _playerController; 

     protected override void OnElementChanged(VisualElementChangedEventArgs e) 
     { 

      base.OnElementChanged(e); 

      if (e.OldElement != null || Element == null) 
      { 
       return; 
      } 

      e.NewElement.BackgroundColor = Color.Red; 
     } 
    } 
} 

方法从接收MQTT消息

public void PushViewRooms() 
    { 
     Device.BeginInvokeOnMainThread(async() => 
     { 
      await Application.Current.MainPage.Navigation.PushModalAsync(new ViewRoomsPage()); 
     }); 
    } 

在App.xaml.cs触发:

public partial class App : Application 
{ 
    public App() 
    { 
     SetupDependencies(); // using StructureMap 
     Manager = DependencyContainer.Resolve<IMqttManager>(); 
     Manager.Connect();    

     InitializeComponent(); 

     var mainPage = new MainPage(); 
     MainPage = new NavigationPage(mainPage); 
    } 
} 
+0

导航需要在主线程上发生。 –

+0

对,因此在'Device.BeginInvokeOnMainThread()'中包装Navigation.Push调用' – Cass

+0

'PushViewRooms()'从哪里调用? –

回答

0

的问题是引起的Task.WaitAll()死锁在运行的另一段代码被触发背景。

谢谢所有帮助理智检查它不是渲染器设置方式的人。

相关问题