如何使用XamlReader.Load方法加载带有图像的XAML?

时间:2017-01-31 19:46:52

标签: c# wpf image xaml xamlreader

  • 是否可以将带有图像的XAML文件加载到WPF中 应用?
  • 以及如何将这些图像存储在文件中?

我已尝试<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'

0 个答案:

没有答案