3

ファイル名が の XAML ファイルがありますMainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Grid x:Name="Content">         

    </Grid>
</Window>

ここで、このファイルを C# のプロジェクトにロードし、 nameContentの要素を取得し、ファイルを追加Buttonして保存します。

var doc = XDocument.Load(@"MainWindow.xaml");
var gridElement = doc.Root.Element("Content"); // it is NULL

しかし、 name のグリッド要素を取得できませんContent。なぜですか?

4

2 に答える 2

0

a を使用しSystem.Windows.Markup.XamlReaderて、ストリームからファイルをロードします。

XamlReader クラス

WPF の既定の XAML リーダーと関連する XAML オブジェクト ライターを使用して、XAML 入力を読み取り、オブジェクト グラフを作成します。

http://msdn.microsoft.com/en-us/library/system.windows.markup.xamlreader.aspx

戻りオブジェクトを使用するには、ルート型にキャストする必要があります。この例では、ルート要素は

using(var fs = new FileStream("MainWindow.xaml"))
{
    Window page = (Window)System.Windows.Markup.XamlReader.Load(fs);
}
于 2013-10-11T07:37:09.097 に答える