Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support NavigationService by type #2147

Closed
2 of 4 tasks
mcvasquez opened this issue Aug 29, 2017 · 3 comments
Closed
2 of 4 tasks

Support NavigationService by type #2147

mcvasquez opened this issue Aug 29, 2017 · 3 comments
Milestone

Comments

@mcvasquez
Copy link

mcvasquez commented Aug 29, 2017

Steps to reproduce

I'm trying to update this line from MvvmCross 4.4.0 to 5.1.1:

ShowViewModel(item.ViewModelType);

Where item.ViewModelType (System.Type) contains the typeof a MvxViewModel. Ex:

typeof(MyFirstViewModel);

I'm doing this because I have a list of items with different ViewModel to navigate in a single MvxCommand.

Expected behavior

Navigate through a ViewModel by type.

Actual behavior

NavigationService does not support navigation by System.Type.

Configuration

Version: 5.1.1

Platform:

  • iOS
  • Android
  • WPF
  • UWP
@b099l3
Copy link
Contributor

b099l3 commented Aug 30, 2017

Hey in one of the projects I have been working on I build a menu generator that has similar functionality, e.g., passing by type. I created some extensions on the navigation service. I'll create a PR with this to see if it fixes this issue.

Edit Just realised you have done this already

@martijn00
Copy link
Contributor

Hehe sorry @b099l3 it would be nice seeing some other contributions then! I don't know what sort of menu generator you made, but i would like to have some bindings for the Android NavigationLayout sometime.

@b099l3
Copy link
Contributor

b099l3 commented Aug 30, 2017

I was basically using reflection to create generic Navigate<TViewModel, TParameter> methods like this:

public static Task NavigateToViewModel(this IMvxNavigationService navigationService, Type viewModel, object parameter = null)
{
    try
    {
        if (parameter == null)
        {
            // Generic Method creation with reflection
            //NavigationService.Navigate<ViewModel>();
            var NavigateGenericList = typeof(MvxNavigationService).GetMethods();
            var NavigateGeneric = NavigateGenericList.Where(x =>
                                                            x.Name == "Navigate" &&
                                                            x.IsGenericMethod &&
                                                            x.GetParameters().Length == 1).FirstOrDefault();
            var NavigateGenericMethod = NavigateGeneric.MakeGenericMethod(new[] { viewModel });
            return (Task)NavigateGenericMethod.Invoke(navigationService, new object[] { null });
        }
        else
        {
            // Using reflection to generate the generic method at runtime.
            //NavigationService.Navigate<NextViewModel, MyObject>(new MyObject());
            var NavigateGenericList = typeof(MvxNavigationService).GetMethods();
            var NavigateGeneric = NavigateGenericList.Where(x =>
                                                            x.Name == "Navigate" &&
                                                            x.IsGenericMethod &&
                                                            x.GetParameters().Length == 2).FirstOrDefault();
            var NavigateGenericMethod = NavigateGeneric.MakeGenericMethod(new[] { viewModel, parameter.GetType() });
            return (Task)NavigateGenericMethod.Invoke(navigationService, new object[] { parameter, null });
        }
    }
    catch (Exception ex)
    {
        throw new MvxNavigationServiceExtensionException(viewModel, parameter, ex);
    }
}

Its quite brittle but needed to finish the project quick. I see the Navigate method signature has changed so I won't need my extension in Mvx 5.2 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants