WPF-コマンドライン

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

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

  • 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プロジェクトを右クリックし、プロパティを選択すると、次のウィンドウが表示されます。

  • [デバッグ]オプションを選択し、コマンドライン引数にファイルパスを書き込みます。

  • Test.txtを使用してtxtファイルを作成し、そのファイルにテキストを書き込んで、任意の場所に保存します。この場合、txtファイルは「D:\」ハードドライブ。

  • プロジェクトに変更を保存し、今すぐアプリケーションをコンパイルして実行します。プログラムがText.txtファイルから読み取るTextBoxにテキストが表示されます。

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

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