在WPF中显示XAML图像

3

我在页面上显示矢量图像时遇到了问题。

这应该可以,不是吗?

<ContentControl>
    <ContentControl.Resources>
        <ResourceDictionary Source="./Assets/vectorImage.xaml"></ResourceDictionary>
    </ContentControl.Resources>
</ContentControl>

恐怕问题出在从Inkscape创建的位图转换成的xaml图像文件上。vectorImage.xaml相当大(136KB),所以我不会复制整个文件,但它的开头是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Stretch="Uniform">
<Canvas Name="svg2985" Width="126" Height="198">
    <Canvas.Resources/>
    ...

编辑:

<ResourceDictionary Source="./Assets/vectorImage.xaml"></ResourceDictionary> 出现红色下划线和提示:期望 ResourceDictionary 的继承者。

当我运行应用程序时,我得到以下异常:

An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in app.exe but was not handled in user code

WinRT information: Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source'. [Line: 134 Position: 68]

编辑:

我忘记提到的一件事是这个项目是 Windows 应用商店应用程序。


但问题出在哪里呢?它无法编译?它能编译但没有显示任何内容?它抛出异常了吗? - dkozl
哦,抱歉,我编辑了帖子。 - maxlego
你正在设置 ContentControlResources 属性,但这本身并没有任何作用。你需要设置 Content 属性才能显示任何内容,但我不确定如何让你的文件显示为内容。 - Dan Puzey
1个回答

3

1)给你的ViewBox分配x:Key,这很可能是导致错误的原因。

<ViewBox x:Key="MySvgImage" ....>

2) 将资源至少移动1个级别以上ContentControl,例如将Window资源移动到此处:

<Window.Resources>
   <ResourceDictionary Source="./Assets/vectorImage.xaml"/>
</Window.Resources>

3) 将 ContentControl 更改为以下内容:

<ContentControl Content="{StaticResource MySvgImage}"/>

4)如果这确实是您的vectorImage.xamlResourceDictionary的开头,则应该像这样开始和结束:

<ResourceDictionary 
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Viewbox x:Key="MySvgImage">
      <Canvas Width="126" Height="198">
         ...
      </Canvas>
   </Viewbox>
</ResourceDictionary>

几乎了...现在我得到了异常:WinRT信息:无法从文本“data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAH4AAADGCAYAAAAUujSQAAAABHNCSVQICAgIfAhkiAAAIABJREFU....”创建“Windows.UI.Xaml.Media.ImageSource”。 - maxlego
你能在你的XAML中找到"data:image/png;base64"并告诉我它是如何使用的吗?不需要整个值,只需要知道它是如何使用的。 - dkozl
<Image xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="image2993" Canvas.Left="0" Canvas.Top="0" Source="data:image/png;base64,..." Width="126" Height="198"/> - maxlego
我也尝试过将其删除,但是我得到了以下错误信息:WinRT信息:无法从文本“m 22.895833 195.89583...”创建“Windows.UI.Xaml.Media.PathFigureCollection”。 - maxlego
看起来,由于某种原因它试图将ResourceDictionary分配给Content。你知道是哪个控件引起了问题吗?是那个ContentControl吗? - dkozl
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接