- 是否可以将带有图像的 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”