我已尝试<x:code>
部分,但XamlReader.Load
方法无法处理它。
XAML代码:
<Page x:Class="System.Windows.Controls.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="onLoaded"
>
<Canvas Width="500" Height="1000">
</Canvas>
<x:Code>
<![CDATA[
void onLoaded(object sender, RoutedEventArgs e)
{
Canvas cvs= this.Content as Canvas;
Image img = new Image();
BitmapImage bi = new BitmapImage();
bi.BeginInit();
string image64 = "...base64image...";
byte[] bd = Convert.FromBase64String(image64);
bi.StreamSource = new System.IO.MemoryStream(bd);
bi.EndInit();
img.Source = bi;
Thickness margin = img.Margin;
margin.Left = 10;
margin.Top = 20;
img.Margin = margin;
cvs.Children.Add(img);
}
]]>
</x:Code>
</Page>
C#代码:
FileStream fs = new FileStream(@"C:\...\XAML\img_xaml.xaml", FileMode.Open);
Window w = new Window();
object xaml = XamlReader.Load(fs);
w.Content = xaml;
w.Show();
例外 -
抛出异常:PresentationFramework.dll中的'System.Windows.Markup.XamlParseException'