ファイルメニューがあります。読み込み時に、ディレクトリのファイル名からmenuItems
の一部が読み込まれます。WPF動的メニュー項目の作成エラー
<MenuItem
x:Name="LayoutLoad"
Header="Load saved layout"
HorizontalAlignment="Left"
Width="200"
/>
//(背後にあるコード)
string[] filePaths = Directory.GetFiles("Settings/layouts");
for (int i = 0; i < filePaths.Count(); i++)
{
MenuItem item = new MenuItem {
Header = System.IO.Path.GetFileName(filePaths[i]),
Name = System.IO.Path.GetFileName(filePaths[i])
};
item.Click += new RoutedEventHandler(Chooselayout);
LayoutLoad.Items.Add(item);
}
これは一瞬前までは、素晴らしい仕事をしていた、今ではコンパイルされません、上のクラッシュ:
とMenuItem item = new MenuItem {
Header = System.IO.Path.GetFileName(filePaths[i]),
Name = System.IO.Path.GetFileName(filePaths[i])
};
:
Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll
Additional information: The invocation of the constructor on type 'Form_beta.MainWindow' that matches the specified binding constraints threw an exception.
このコードと担当者それをレース:
string test = System.IO.Path.GetFileName(filePaths[i]);
それは正常に動作し、私が期待するファイルパスを返します。
ここで何が間違っていますか?ありがとうございました。
ああ、ありがとう。名前にスペースを含むファイルがディレクトリにありましたが、これはmenuItemのヘッダーとして使用できませんでした。 – anti