2016-11-25 76 views
0

我在Xamarin PCL應用程序中實現了混合Web視圖。導航頁面在C#中使用Xamarin形式的異常

我在前面提到的混合Web視圖中使用JavaScript從Html頁面調用C#函數。

問題是,雖然我的函數被調用,但當我嘗試從它重定向時拋出異常。

Android.Util.AndroidRuntimeException:

只有創建視圖層次可以觸摸其觀點原來的線程。

at System.Runtime.ExceptionServices.ExceptionDispatchInfo。

我的代碼如下:

var isValid = AreCredentialsCorrect(user); 

if (isValid) 
{ 
    try 
    { 
     await Navigation.PushAsync(new UserDashboard("local.html?auth_admin=true")); 

    } 
    catch { } 

} 

public UserDashboard(string uriname) 
{ 
    InitializeComponent(); 

    hybridWebView.Uri = uriname; 

    hybridWebView.RegisterAction(data => userLogin(data)); 

} 

回答

1

聽起來像你想從後臺線程更新UI。嘗試從主線程導航:

Device.BeginInvokeOnMainThread(async() => await Navigation.PushAsync(new UserDashboard("local.html?auth_admin=true")));