Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Android SetPage after async fails

$
0
0

I'm trying to navigate from a splash screen (ContentPage) to the main app screen (MasterDetailPage).
In my ViewModel, I send a message to the native Android activity passing in the new MasterDetailPage.
On the message handler, I just SetPage(message.NewPage) and everything works fine.

BUT...

If I send the message in an async method after a Task.Delay(2500), the message reaches the Activity code and the SetPage is executed, but the Android emulator only shows a blank screen =( No exceptions are thrown (at least the debugger didn't catch any).

Which is the best way to override the current Page (I don't need navigation here), after an async call?

Resume:
Works: Method GoTo(new ContentPage()) => Sends a Message with ContentPage => The AndroidActivity listens to this message => SetPage(ContentPage) => Success \O/

Does not works: Method async Task.Delay(2500); GoTo(new ContentPage()) => everything is exactly the same after that => SetPage(ContentPage) => Blank screen =(

BTW... In Windows Phone, both sync/async methods works fine. Didn't had the time to test on iOS.

In Windows Phone, the code is:

    protected virtual void OnTopNavigationMessage(TopNavigationMessage message)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            var newContent = (FrameworkElement) message.View.XFormsPage.ConvertPageToUIElement(this);

            if (newContent.Parent != null)
            {
                ((Panel) newContent.Parent).Children.Remove(newContent);
            }

            Content = newContent;
        });
    }

In Android:

    protected virtual void OnTopNavigationMessage(TopNavigationMessage message)
    {
        Device.BeginInvokeOnMainThread(() => SetPage(message.View.XFormsPage));
    }

And in iOS:

    protected virtual void OnTopNavigationMessage(TopNavigationMessage message)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            window.RootViewController = message.View.XFormsPage.CreateViewController();
        });
    }

This works:

    protected override  void OnInitialize()
    {
        GoTo<HomeView>();
    }

    public void GoTo<TView>()
        where TView : BaseView
    {
        var view = BaseApp.BaseInstance.DependencyContainer.Resolve<TView>();

        MessengerInstance.Send(new TopNavigationMessage(view));
    }

But this doesn't:

    protected override async void OnInitialize()
    {
        await Task.Delay(2500);

        GoTo<HomeView>();
    }

    public void GoTo<TView>() (SAME CODE ABOVE)

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>