Wpf-command-line

提供:Dev Guides
移動先:案内検索

WPF-コマンドライン

コマンドライン引数は、ユーザーが実行時にパラメーターまたは値のセットをWPFアプリケーションに渡すことができるメカニズムです。 これらの引数は、外部からアプリケーションを制御するために非常に重要です。たとえば、コマンドプロンプトからWord文書を開きたい場合、このコマンド「 C:\> start winword word1.docx 」を使用すると、 word1.docx ドキュメントを開きます。

コマンドライン引数は、スタートアップ関数で処理されます。 以下は、コマンドライン引数をWPFアプリケーションに渡す方法を示す簡単な例です。 WPFCommandLine という名前の新しいWPFアプリケーションを作成しましょう。

  • ツールボックスからデザインウィンドウに1つのテキストボックスをドラッグします。
  • この例では、コマンドラインパラメーターとしてtxtファイルのパスをアプリケーションに渡します。
  • プログラムは、txtファイルを読み取り、テキストボックスにすべてのテキストを書き込みます。
  • 次のXAMLコードはテキストボックスを作成し、いくつかのプロパティで初期化します。
<Window x:Class = "WPFCommandLine.MainWindow"
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
   xmlns:local = "clr-namespace:WPFCommandLine"
   mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "525">

   <Grid>
      <TextBox x:Name = "textBox" HorizontalAlignment = "Left"
         Height = "180" Margin = "100" TextWrapping = "Wrap"
         VerticalAlignment = "Top" Width = "300"/>
   </Grid>

</Window>
  • 次に示すように、App.xamlファイルでStartupイベントをサブスクライブします。
<Application x:Class = "WPFCommandLine.App"
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:local = "clr-namespace:WPFCommandLine"
   StartupUri = "MainWindow.xaml" Startup = "app_Startup">

   <Application.Resources>

   </Application.Resources>

</Application>
  • 以下に示すのは、コマンドライン引数を取得するApp.xaml.csのapp_Startupイベントの実装です。
using System.Windows;

namespace WPFCommandLine {
  ///<summary>
     ///Interaction logic for App.xaml
  ///</summary>

   public partial class App : Application {
      public static string[] Args;

      void app_Startup(object sender, StartupEventArgs e) {
        //If no command line arguments were provided, don't process them
         if (e.Args.Length == 0) return;

         if (e.Args.Length > 0) {
            Args = e.Args;
         }
      }
   }
}
  • これで、MainWindowクラスで、プログラムはtxtファイルを開き、テキストボックスにすべてのテキストを書き込みます。
  • エラーが見つかった場合、プログラムはテキストボックスにエラーメッセージを表示します。
using System;
using System.IO;
using System.Windows;

namespace WPFCommandLine {

   public partial class MainWindow : Window {

      public MainWindow() {
         InitializeComponent();
         String[] args = App.Args;

         try {
           //Open the text file using a stream reader.
            using (StreamReader sr = new StreamReader(args[0])) {
              //Read the stream to a string, and write
              //the string to the text box
               String line = sr.ReadToEnd();
               textBox.AppendText(line.ToString());
               textBox.AppendText("\n");
            }
         }
         catch (Exception e) {
            textBox.AppendText("The file could not be read:");
            textBox.AppendText("\n");
            textBox.AppendText(e.Message);
         }
      }
   }
}
  • 上記のコードをコンパイルして実行すると、このプログラムにはコマンドライン引数が必要なため、テキストボックスを含む空白のウィンドウが生成されます。 Visual Studioは、コマンドラインパラメーターを使用してアプリケーションを実行する簡単な方法を提供します。
  • ソリューションエクスプローラーでWPFプロジェクトを右クリックしてプロパティを選択すると、次のウィンドウが表示されます。

WPFコマンドライン

  • [デバッグ]オプションを選択し、コマンドライン引数にファイルパスを書き込みます。
  • Test.txtでtxtファイルを作成し、そのファイルにテキストを書き込み、任意の場所に保存します。 この場合、txtファイルは「 D:\ 」ハードドライブに保存されます。
  • プロジェクトの変更を保存し、今すぐアプリケーションをコンパイルして実行します。 プログラムがText.txtファイルから読み取るTextBoxにテキストが表示されます。

コマンドラインの出力

次に、マシン上のファイル名を Test.txt から Test1.txt に変更して、プログラムを再度実行してみます。テキストボックスにエラーメッセージが表示されます。

コマンドラインのエラー出力

上記のコードを実行し、すべての手順に従ってアプリケーションを正常に実行することをお勧めします。