使用Prism和SignalR导航到Xamarin Forms中的另一个页面时出现异常

时间:2017-07-23 21:25:26

标签: xamarin.forms prism signalr.client

我有一个使用Prism的Xamarin Forms PCL应用程序。此外,该应用程序是SignalR客户端。在MainPage(脚手架创建)中,我设置了SignalR:

 public MainPageViewModel(INavigationService navigationService)
    {
        this.navigationService = navigationService;

        SetupSignalr();
    }

 private async void SetupSignalr()
    {
        hubConnection = new HubConnection("http://mypc:15816/");
        hubProxy = hubConnection.CreateHubProxy("SignatureCaptureHub");
        hubProxy.On<string>("ShowSignatureControl",  receiptText =>  NavigateToSignaturePad(receiptText));
        await hubConnection.Start();

        await hubProxy.Invoke("JoinGroup", "Group1234");
    }

这一切都按预期工作。 SignalR服务器获取加入组事件。我的问题是收到的事件,即处理程序&#34; NavigateToSignaturePad&#34;。该处理程序如下所示:

private async void NavigateToSignaturePad(string receiptText)
    {
        System.Diagnostics.Debug.WriteLine("Got a ShowSignatureControl event!");

        Title = "Got a ShowSignatureControl event!";

        try
        {
            //Device.BeginInvokeOnMainThread(async () => await navigationService.NavigateAsync("SignatureCapturePage"));
            await navigationService.NavigateAsync("SignatureCapturePage");
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }
    }

如上所述,当收到signalR消息时,我得到以下异常:

  

at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;中的[0x0000c]:0 at   Java.Interop.JniEnvironment + InstanceMethods.CallVoidMethod   (Java.Interop.JniObjectReference实例,Java.Interop.JniMethodInfo   方法)[0x00068] in:0 at   Android.Runtime.JNIEnv.CallVoidMethod(System.IntPtr jobject,   System.IntPtr jmethod)[0x0000e] in   &lt; 9ab9faae1b4b4f0da28e7c4ac61e2c78&gt;:0 at   Android.Views.IMenuInvoker.Clear()[0x00033] in   &lt; 9ab9faae1b4b4f0da28e7c4ac61e2c78&gt;:0 at   Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.UpdateMenu   ()[0x0005a] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \程序兼容性\ NavigationPageRenderer.cs:682   在   Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.ToolbarTrackerOnCollectionChanged   (System.Object sender,System.EventArgs eventArgs)[0x00000] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \程序兼容性\ NavigationPageRenderer.cs:668   在Xamarin.Forms.Internals.ToolbarTracker.EmitCollectionChanged()   [0x00008] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \内幕\ ToolbarTracker.cs:60   在Xamarin.Forms.Internals.ToolbarTracker.OnPropertyChanged   (System.Object sender,System.ComponentModel.PropertyChangedEventArgs   propertyChangedEventArgs)[0x00052] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \内幕\ ToolbarTracker.cs:130   at(包装器委托调用)   :invoke_void_object_PropertyChangedEventArgs   (对象,System.ComponentModel.PropertyChangedEventArgs)at   Xamarin.Forms.BindableObject.OnPropertyChanged(System.String   propertyName)[0x0000a] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ BindableObject.cs:137   在Xamarin.Forms.Element.OnPropertyChanged(System.String   propertyName)[0x00000] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ Element.cs:379   在Xamarin.Forms.BindableObject.SetValueActual   (Xamarin.Forms.BindableProperty属性,   Xamarin.Forms.BindableObject + BindablePropertyContext上下文,   System.Object值,System.Boolean currentApplying,   Xamarin.Forms.BindableObject + SetValueFlags属性,System.Boolean   沉默)[0x000f4] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ BindableObject.cs:581   在Xamarin.Forms.BindableObject.SetValueCore   (Xamarin.Forms.BindableProperty属性,System.Object值,   Xamarin.Forms.BindableObject + SetValueFlags属性,   Xamarin.Forms.BindableObject + SetValuePrivateFlags privateAttributes)   [0x0014b] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ BindableObject.cs:378   在Xamarin.Forms.BindableObject.SetValue   (Xamarin.Forms.BindableProperty属性,System.Object值,   System.Boolean fromStyle,System.Boolean checkAccess)[0x0005f] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ BindableObject.cs:531   在Xamarin.Forms.BindableObject.SetValue   (Xamarin.Forms.BindablePropertyKey propertyKey,System.Object value)   [0x0000e] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ BindableObject.cs:91   在Xamarin.Forms.NavigationPage.set_CurrentPage(Xamarin.Forms.Page   值)[0x00000] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ NavigationPage.cs:85   在Xamarin.Forms.NavigationPage.PushPage(Xamarin.Forms.Page页面)   [0x00011] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ NavigationPage.cs:371   在Xamarin.Forms.NavigationPage + d__90.MoveNext()   [0x0002f] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ NavigationPage.cs:350

     

---抛出异常的前一个位置的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;中的[0x0000c]:0 at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess   (System.Threading.Tasks.Task任务)[0x0003e] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification   (System.Threading.Tasks.Task任务)[0x00028] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd   (System.Threading.Tasks.Task任务)[0x00008] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.GetResult()[0x00000] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   Xamarin.Forms.NavigationPage + d__48.MoveNext()[0x0017f] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ NavigationPage.cs:182

     

---抛出异常的前一个位置的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;中的[0x0000c]:0 at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess   (System.Threading.Tasks.Task任务)[0x0003e] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification   (System.Threading.Tasks.Task任务)[0x00028] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd   (System.Threading.Tasks.Task任务)[0x00008] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.GetResult()[0x00000] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   Prism.Navigation.PageNavigationService + d__31.MoveNext()   [0x00139]在&lt; 7a32c28f35024e719a93e31b4d1e3d2a&gt;中:0   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;中的[0x0000c]:0 at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess   (System.Threading.Tasks.Task任务)[0x0003e] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification   (System.Threading.Tasks.Task任务)[0x00028] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd   (System.Threading.Tasks.Task任务)[0x00008] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.GetResult()[0x00000] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   &lt; 7a32c28f35024e719a93e31b4d1e3d2a&gt;中的Prism.Navigation.PageNavigationService +&lt;&gt; c__DisplayClass17_0 +&lt; b__0&gt; d.MoveNext()[0x00091]:0   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;中的[0x0000c]:0 at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess   (System.Threading.Tasks.Task任务)[0x0003e] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification   (System.Threading.Tasks.Task任务)[0x00028] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd   (System.Threading.Tasks.Task任务)[0x00008] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.GetResult()[0x00000] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   Prism.Navigation.PageNavigationService + d__24.MoveNext   &lt; 7a32c28f35024e719a93e31b4d1e3d2a&gt;中的()[0x00125]:0   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;中的[0x0000c]:0 at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess   (System.Threading.Tasks.Task任务)[0x0003e] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification   (System.Threading.Tasks.Task任务)[0x00028] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd   (System.Threading.Tasks.Task任务)[0x00008] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.GetResult()[0x00000] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   Prism.Navigation.PageNavigationService + d__17.MoveNext   &lt; 7a32c28f35024e719a93e31b4d1e3d2a&gt;中的()[0x00198]:0   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;中的[0x0000c]:0 at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess   (System.Threading.Tasks.Task任务)[0x0003e] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification   (System.Threading.Tasks.Task任务)[0x00028] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd   (System.Threading.Tasks.Task任务)[0x00008] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.GetResult()[0x00000] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   Prism.Navigation.PageNavigationService + d__14.MoveNext   &lt; 7a32c28f35024e719a93e31b4d1e3d2a&gt;中的()[0x0016b]:0   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;中的[0x0000c]:0 at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess   (System.Threading.Tasks.Task任务)[0x0003e] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification   (System.Threading.Tasks.Task任务)[0x00028] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd   (System.Threading.Tasks.Task任务)[0x00008] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   System.Runtime.CompilerServices.TaskAwaiter.GetResult()[0x00000] in   &lt; 3fd174ff54b146228c505f23cf75ce71&gt;:0 at   ReceiptManager.SignatureCapture.ViewModels.MainPageViewModel + d__12.MoveNext   ()[0x0005a] in   C:\回购\ ContentManagment.ReceiptManager.SignatureCapture \来源\ ReceiptManager.SignatureCapture \的ViewModels \ MainPageViewModel.cs:65   ---托管Android.Util.AndroidRuntimeException堆栈跟踪结束--- android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。在   android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6556)at   android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:907)at   android.view.View.requestLayout(View.java:18722)at   android.view.View.requestLayout(View.java:18722)at   android.view.View.requestLayout(View.java:18722)at   android.view.View.requestLayout(View.java:18722)at   android.view.View.requestLayout(View.java:18722)at   android.view.View.requestLayout(View.java:18722)at   android.widget.RelativeLayout.requestLayout(RelativeLayout.java:360)     在android.view.View.requestLayout(View.java:18722)at   android.view.View.requestLayout(View.java:18722)at   android.view.View.requestLayout(View.java:18722)at   android.view.View.requestLayout(View.java:18722)at   android.support.v7.widget.ActionMenuPresenter.updateMenuView(ActionMenuPresenter.java:231)     在   android.support.v7.view.menu.MenuBuilder.dispatchPresenterUpdate(MenuBuilder.java:284)     在   android.support.v7.view.menu.MenuBuilder.onItemsChanged(MenuBuilder.java:1030)     在   android.support.v7.view.menu.MenuBuilder.clear(MenuBuilder.java:589)

为什么Title属性(绑定到MainPage上的标签)更新得很好,但是,导航到另一个页面会引发UI线程问题?

我也尝试使用Device.BeginInvokeOnMainThread方法(注释掉以供参考),并且应用程序崩溃时出现未处理的异常。

我在使用Visual Studio 2017部署到Android模拟器的Xamarin.Forms版本2.3.3.192。感谢。

1 个答案:

答案 0 :(得分:0)

我认为问题不在于Signalr对处理程序的调用,而在于我导航到的视图/页面。我在该视图模型中绑定了一个TextCell,它似乎是问题的主要原因。我只是用了一个标签而问题就消失了。这里参考的是我最终得到的代码:

 public MainPageViewModel(INavigationService navigationService)
    {
        this.navigationService = navigationService;

        SetupSignalr();
    }

    private async void SetupSignalr()
    {
        hubConnection = new HubConnection("http://mypc:15816/");
        hubProxy = hubConnection.CreateHubProxy("SignatureCaptureHub");
        hubProxy.On<string>("ShowSignatureControl",  receiptText =>  NavigateToSignaturePad(receiptText));
        await hubConnection.Start();

        await hubProxy.Invoke("JoinGroup", "Group1234");
    }

    private void NavigateToSignaturePad(string receiptText)
    {
        System.Diagnostics.Debug.WriteLine("Got a ShowSignatureControl event!");

        var navigationParameters = new NavigationParameters
        {
            { "receiptText", receiptText }
        };
        Device.BeginInvokeOnMainThread(() => navigationService.NavigateAsync(nameof(SignatureCapturePage), navigationParameters));
    }

但我确实必须使用BeginInvokeOnMainThread。没有它,没有抛出异常,但导航也没有发生。