Implementing deep linking in MvvmCross involves using the `NavigationService` to navigate between ViewModels based on URIs. Here's a detailed guide on how to achieve this:
1. Setup NavigationService
First, ensure that the `NavigationService` is properly set up in your MvvmCross application. This service is responsible for navigating between ViewModels and supports URI-based navigation.
2. Define Navigation Attributes
To enable deep linking, you need to define navigation attributes for your ViewModels. These attributes specify the URI patterns that should trigger navigation to specific ViewModels. You can use regular expressions to capture parameters from the URI.
csharp
[assembly: MvxNavigation(typeof(ViewModelA), @"mvx://test/\?id=(?[A-Z0-9]{32})$")]
In this example, navigating to `mvx://test/?id=ANY_32_CHARACTERS` will open `ViewModelA` and pass the captured `id` as a parameter.
3. Implement NavigationFacades
NavigationFacades are used to return the correct ViewModel along with parameters based on the URI. They are constructed using `Mvx.IocConstruct` to leverage dependency injection.
csharp
public class MyNavigationFacade : IMvxNavigationFacade
{
public MvxViewModelRequest GetViewModelRequest(string uri, IDictionary parameters)
{
// Logic to determine which ViewModel to return based on the URI
if (uri.StartsWith("mvx://test/"))
{
return new MvxViewModelRequest(typeof(ViewModelA), parameters);
}
// Handle other URIs
}
}
4. Register NavigationService
Ensure that the `NavigationService` is registered as a singleton in your app's setup. This service uses `IMvxViewDispatcher` to display the ViewModels.
5. Handle Deeplinks
To handle deeplinks from outside the app, you need to register a custom scheme in your app's manifest (for Android) or info.plist (for iOS). For example, if you want to handle `mvx://test/`, you would register `mvx` as the scheme.
On Android, you would add an intent filter to your Activity:
xml
On iOS, you would add a URL scheme to your info.plist:
xml
CFBundleURLTypes
CFBundleTypeRole
Editor
CFBundleURLSchemes
mvx
Then, override `OnNewIntent` in your Android Activity or implement `OpenUrl` in your iOS AppDelegate to pass the URI to the `NavigationService`.
6. Navigate with Parameters
When navigating, you can pass parameters to the target ViewModel. MvvmCross supports both simple and complex parameters. For simple parameters, ensure the class has a parameterless constructor and only uses certain types (e.g., `int`, `string`, `Guid`).
csharp
public class DetailParameters
{
public int Index { get; set; }
}
// In MasterViewModel
ShowViewModel(new DetailParameters { Index = 2 });
// In DetailViewModel
public void Init(DetailParameters parameters)
{
// Use the parameters
}
For complex parameters, you might need to implement custom serialization or use the MvvmCross JSON plugin.
Conclusion
Deep linking in MvvmCross is powerful and flexible, allowing you to navigate between ViewModels using URIs and parameters. By setting up navigation attributes, implementing NavigationFacades, and registering custom schemes, you can enable seamless navigation both within and from outside your app.
Citations:[1] https://www.mvvmcross.com/documentation/fundamentals/navigation
[2] https://www.mvvmcross.com/documentation/tutorials/tipcalc/the-tip-calc-navigation
[3] https://community.glideapps.com/t/deep-link-to-another-screen-within-the-app/39285
[4] https://github.com/MvvmCross/MvvmCross/issues/1315
[5] https://www.youtube.com/watch?v=SP_CwEmePkE
[6] https://www.mvvmcross.com/documentation/fundamentals/data-binding
[7] https://www.mvvmcross.com/documentation/fundamentals/linking
[8] https://stackoverflow.com/questions/71568130/deep-linking-for-ios-in-xamarin-from/71580991
[9] https://www.mvvmcross.com/documentation/advanced/customizing-using-App-and-Setup