Passing parameters from the command line to a WPF application is not as straightforward as doing the same in WinForms. It's a multiple step process, which I've detailed below.
- Edit the app.xaml.cs like this:
   using System;
  using System.Collections.Generic;
  using System.Configuration;
  using System.Data;
  using System.Linq;
  using System.Windows;
  
  namespace Tool
  {
      <summary>
      
      </summary>
      public partial class App : Application
      {
          public static string[] args;
  
          void OnStartUp(object sender, StartupEventArgs e)
          {
              if (e.Args.Length > 0)
              {
                  args = e.Args;
              }
          }
      }
  }
- Edit the app.xaml like this:
   <Application x:Class="Tool.App"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               StartupUri="MainWindow.xaml" Startup="OnStartUp">
      <Application.Resources>
           
      </Application.Resources>
  </Application>
- Edit MainWindow.xaml.cs like this:
  public MainWindow()
 {
     InitializeComponent();
     
     if (App.args != null && App.args.Length > 0)
     {
  
     }
     
 }
Remember that you can pass paremeters/arguments from visual studio for debugging purposes.
- Right Click on Project and Select Properties.
- Go to Debug tab.
- Set Command Line arguments, see screenshot below.
No comments:
Post a Comment