次のような XAML グリッドの文字列表現があります。
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Canvas Background="Yellow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Label Content="textik" />
</Canvas>
</Grid>
私がする必要があるのは、この文字列から Grid オブジェクトを作成することです。私は多くのアプローチを試しましたが、これまでのところ最も近いのは以下のコードです:
string content = "<Grid xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Canvas Background=\"Yellow\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Label Content=\"textik\" /></Canvas></Grid>";
// the string is created programatically, I just put it here to see what it looks like at the end of the process
Stream stream = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(content));
object objGrid = XamlReader.Load(stream);
Grid myGrid = (Grid) objGrid;
ただし、ルート要素が見つからないことを示す XamlParsedException が発生します。
表示できない XAML コードに誤りがありますか? それともアプローチが悪い?
答えてくれてありがとう