Search code examples
xamarinxamarin.iosmvvmcrossdeep-linking

Sending new parameters in MvxViewModelRequest from a IMvxNavigationFacade when deeplinking


I am using deeplinking in my app and Im looking to preset some parameters when navigating to the viewmodel using a IMvxNavigationFacade. The deep link url is like this:

myappname://deeplink/toviewwithdata/?navigatetoview=viewtype1&id=78910

So the deep linking is working and im getting to the navigation facade using the assembly attribute

[assembly: MvxNavigation(typeof(RoutingFacade), @"myappname://deeplink/toviewwithdata/\?navigatetoview=(?<viewtype>viewtype1)&id=(?<id>\d{5})")]

I tried to add other parameters to the MvxViewModelRequest using a MvxBundle but dont think im doing it right. here is my navigation facade:

public class RoutingFacade : IMvxNavigationFacade
{
    public Task<MvxViewModelRequest> BuildViewModelRequest(string url, IDictionary<string, string> currentParameters)
    {
       var viewModelType = typeof(FirstViewModel);
       var parameters = new MvxBundle();
       try
       {
           // TODO: Update this to handle different view types and add error handling
           if (currentParameters != null)
           {
               Debug.WriteLine($"RoutingFacade - {currentParameters["viewtype"]}, {currentParameters["id"]}");

               switch (currentParameters["viewtype"])
               {
                   case "viewtype1":
                       viewModelType = typeof(FirstViewModel);
                       parameters.Data.Add("test", "somevalue");
                       break;
                   default:
                   case "viewtype2":
                       viewModelType = typeof(FirstViewModel);
                       break;
               }
           }
       }
       catch (Exception ex)
       {
           Debug.WriteLine($"RoutingFacade - Exception:  {ex.Message}");
           //TODO  viewModelType = typeof(ErrorViewModel);
       }

    return Task.FromResult(new MvxViewModelRequest(viewModelType, parameters, null));
}

then my viewmodel Init method

    public void Init(string id, string viewtype, string test)
    {
        // Do stuff with parameters
    }

but the test parameter is null? How do you pass parameters into a MvxViewModelRequest?

Update:

Don’t know if its possible from looking at the source here https://github.com/MvvmCross/MvvmCross/blob/f4b2a7241054ac288a391c4c7b7a7342852e1e19/MvvmCross/Core/Core/Navigation/MvxNavigationService.cs#L122 as the request parameters get set from the regex of the deeplink url and the return from BuildViewModelRequest, facadeRequest.parameterValues get ignored.


Solution

  • Added this functionality in this pull request