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

Passing already instantiated view to INavigationService

$
0
0

Hi!

This might be a bad practice but it's already written code that we're bound to manage.

Pre XLabs 2

  public async static Task ViewTerms(ViewModelNavigation navigation, Terms terms)
    {
        var view = Resolver.Resolve<TermsAndConditionsView>();
        var model = view.BindingContext as TermsAndConditionsViewModel;
        model.SelectTerms(Terms.Upload);

        await navigation.PushAsync(view);
    }

In XLabs 2, ViewModelNavigation is replaced with INavigationService. None of the overloads lets you pass in an already created View/Page.

So the question is, how do you write code like the one above? Do I have to rewrite it to pass in parameters instead like

 navigation.NavigateTo<TermsAndConditionsView>(Terms.Upload);

I personally don't like untyped parameters being passed, but that might just be me! :)

[EDIT]

NavigationService.NavigateTo(Type pageType, object parameter = null, bool animated = true)

The "parameter"-parameter doesn't seem to be used at all in the code. for NavigateTo(...)?

            public void NavigateTo(Type pageType, object parameter = null, bool animated = true)
    {
        if (_navigation == null)
        {
            throw new InvalidOperationException("Xamarin Forms Navigation Service not found");
        }

        object page;
        var pInfo = pageType.GetTypeInfo();
        var xfPage = typeof(Xamarin.Forms.Page).GetTypeInfo();
        var xlvm = typeof(IViewModel).GetTypeInfo();

        if (pInfo.IsAssignableFrom(xlvm) || pInfo.IsSubclassOf(typeof(ViewModel)))
        {
            page = ViewFactory.CreatePage(pageType);
        }
        else if (pInfo.IsAssignableFrom(xfPage) || pInfo.IsSubclassOf(typeof(Xamarin.Forms.Page)))
        {
            page = Activator.CreateInstance(pageType);
        }
        else
        {
            throw new ArgumentException("Page Type must be based on Xamarin.Forms.Page");
        }

                    // SHOULDN'T PARAMETERS BE PASSED ABOUT HERE?

        _navigation.PushAsync(page as Xamarin.Forms.Page);
    }

Viewing all articles
Browse latest Browse all 58056

Trending Articles



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