简体   繁体   中英

MvvmCross - navigate to ViewModel with parameters Xamarin.iOS

I want to be able to navigate to a ViewModel, while sending in some parameters to it, after receiving a push notification (so the application isn't initially running).

I'm able to show a specific ViewModel by picking up that the application starts from a push notification in the AppDelegates "FinishedLaunching", but I can´t seem to figure out where to pick up the parameter I'm trying to send.

[Register("AppDelegate")]
public partial class AppDelegate : MvxApplicationDelegate
{
    UIWindow _window;

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {  
        // Some irrelevant initialization code here


        if (options != null)
        {
            if (options.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey))
            {
                var viewDispatcher = Mvx.Resolve<IMvxViewDispatcher>();
                var request = MvxViewModelRequest.GetDefaultRequest(typeof(MyViewModel));
                request.ParameterValues = ((object)"someBooleanParameter").ToSimplePropertyDictionary();
                viewDispatcher.ShowViewModel(request);
            }
        }

        return true;
    }
}

I've tried to pick up the parameter in the ViewModel in the Init method, but it doesn't seem to work.

public class MyViewModel : MvxViewModel
{
    public void Init(string parameters)
    {
        if (parameters.Equals("someBooleanParameter"))
            // do something
    } 
}

You can pass in a dictionary of arguments (key propertyName , and value). https://github.com/MvvmCross/MvvmCross/blob/4.0/MvvmCross/Droid/Droid/Views/MvxChildViewModelOwnerExtensions.cs

In the init method the properties will be filled with the values from the dictionary. The only requirement is that the names in your init methods can be found as keys in the dictionary.

So the following init method from your VM.

void Init ( string name, string something else) { }

Get filled by passing in this dictionary:

new Dictionary<string, string> { 
       { "name", simpleRoute.Name }, 
       { "code",simpleRoute.Code} 
}

This is only one of the options..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM