Search code examples
c#wpfnavigationmvvmcrossapp-startup

How to get command line parameters in a WPF application using MvvmCross?


I'm trying to get command line parameters passed to my WPF application using MvvmCross.
I want to access the parameters in my MainViewModel.

Similar to how you would override the OnStartup() method (or create an event handler for the Startup event) in the App.xaml.cs and do stuff with your parameters there.

I tried inheriting my App.cs from MvxApplication<object> and using MvxNavigationViewModel<object> as the MainViewModel base class like so:

public class App : MvxApplication<object>
{
    public override void Initialize()
    {
        #region BulkRegistration

        CreatableTypes()
            .EndingWith("Service")
            .AsInterfaces()
            .RegisterAsLazySingleton();

        #endregion

        #region AppStartRegistration

        RegisterAppStart<MainViewModel, object>();

        #endregion
    }
}

The Prepare(object parameter) method in my MainViewModel never gets called though.

public class MainViewModel : MvxNavigationViewModel<object>
{
    // ...

    public override void Prepare(object parameter)
    {
        // do stuff with parameter
    }
}

How do I do this correctly?


Solution

  • You should be able to use the following regardless of whether you are in a ViewModel or where ever you are.

    var args = Environment.GetCommandLineArgs();