Sonali
Sonali

Reputation: 2273

Navigation Page Exception in C# using Xamarin forms

I have implemented a hybrid web view in my Xamarin PCL app.

I am calling a C# function from an Html page using JavaScript in the aforementioned, hybrid web view.

The problem is that although my function is being called, an exception is thrown when I try to redirect from it.

Android.Util.AndroidRuntimeException:

Only the original thread that created a view hierarchy can touch its views.

at System.Runtime.ExceptionServices.ExceptionDispatchInfo.

My code is as follows:

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));

}

Upvotes: 0

Views: 448

Answers (1)

TommyHaugland
TommyHaugland

Reputation: 171

Sounds like you're trying to update UI from a background thread. Try doing the navigation from the main thread:

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

Upvotes: 3

Related Questions