locked
Command line arguments in MVVM Light RRS feed

  • Question

  • How can I pass command line arguments in MVVM Light ?

    thanks

    Monday, May 11, 2015 6:28 AM

Answers

  • Modify your app.xaml so you have control of how your app starts:

    <Application x:Class="WpfTutorialSamples.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                             Startup="Application_Startup">
        <Application.Resources></Application.Resources>
    </Application>

    Then you can use the arguments:

    using System;
    using System.Collections.Generic;
    using System.Windows;
    
    namespace WpfTutorialSamples
    {
            public partial class App : Application
            {
    
                    private void Application_Startup(object sender, StartupEventArgs e)
                    {
                            MainWindow wnd = new MainWindow();
                            if(e.Args.Length == 1)
                                    MessageBox.Show("Now opening file: \n\n" + e.Args[0]);
                            wnd.Show();
                    }
            }
    }

    http://www.wpf-tutorial.com/wpf-application/command-line-parameters/

    You can then add them to resources so you can access them in any viewmodel, in a nice decoupled sort of a way:

    Application.Current.Resources.Add("YourKey", YourObject);


    Hope that helps.

    Technet articles: Uneventful MVVM; All my Technet Articles


    • Edited by Andy ONeill Monday, May 11, 2015 7:45 AM
    • Marked as answer by Stefano74 Monday, May 11, 2015 8:39 AM
    Monday, May 11, 2015 7:44 AM