TOC

This article is currently in the process of being translated into Norwegian Bokmål (~45% done).

A WPF application:

Command-line parameters in WPF

Command-line parameters are a technique where you can pass a set of parameters to an application that you wish to start, to somehow influence it. The most common example is to make the application open with a specific file, e.g. in an editor. You can try this yourself with the built-in Notepad application of Windows, by running (select Run from the Start menu or press [WindowsKey-R]):

notepad.exe c:\Windows\win.ini

Dette vil starte notisblokken (Notepad) med win.ini åpnet (du må muligens justere stien til filen for ditt system). Notepad ser enkelt og greit bare etter et eller flere parametre og benytter dem. Din applikasjon kan gjøre det samme.

Command-line parameters are passed to your WPF application through the Startup event, which we subscribed to in the App.xaml article. We will do the same in this example, and then use the value passed on to through the method arguments. First, the App.xaml file:

<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>

alt vi gjør her er å abonnere på oppstartshendelsen (Startup), og erstatter verdien i StartupUri-egenskapen. Hendelsen blir dermed implementert i App.xaml.cs.

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();
		}
	}
}

The StartupEventArgs is what we use here. It's passed into the Application Startup event, with the name e. It has the property Args, which is an array of strings. Command-line parameters are separated by spaces, unless the space is inside a quoted string.

Testing av kommandolinje-parameter

If you run the above example, nothing will happen, because no command-line parameters have been specified. Fortunately, Visual Studio makes it easy to test this in your application. From the Project menu select "[Project name] properties" and then go to the Debug tab, where you can define a command-line parameter. It should look something like this:

Forsøk å kjøre applikasjonen og se hvordan den responderer til parameteret ditt.

Selvfølgelig er ikke meldingen spesielt nyttig. Istedenfor er det mer ønskelig å sende parameteret til koden som klargjør hovedvinduet eller en offentlig tilgjengelig metode der, som dette:

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();
			// The OpenFile() method is just an example of what you could do with the
			// parameter. The method should be declared on your MainWindow class, where
			// you could use a range of methods to process the passed file path
			if(e.Args.Length == 1)
				wnd.OpenFile(e.Args[0]);
			wnd.Show();
		}
	}
}

Kommandolinje muligheter

I dette eksempelet tester vi om det er bare et parameter, og bruker det som filnavn. I virkeligheten vil du kanskje innhente flere parametre og bruke dem til å sette opsjoner – for eksempel slå av og på enkelte funksjoner. Det gjøres ved å gå gjennom hele listen med parametre som ble gitt og benytte den informasjonen som er nødvendig for å fortsette. Men det er utenfor rammene til denne veilederen.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!
Table of Contents