我有两个图像存储在资源文件夹中,其中“构建操作”设置为“资源”,“复制到输出目录”设置为“不复制”。图像是" font.png"和" open.png"。当我查看设计师时,图像也会按照我想要的方式出现在按钮中;当我运行应用程序时,按钮是空的。以下是我的XAML,我做错了什么?
<Window x:Class="Preferences"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:se="clr-namespace:TheRandomizerWPF.MarkupExtensions"
xmlns:res ="clr-namespace:TheRandomizerWPF.My"
xmlns:clr="clr-namespace:System;assembly=mscorlib"
Title="Preferences"
Icon="The Randomizer.ico"
Height="227.509"
Width="453.736"
WindowStyle="ThreeDBorderWindow"
ResizeMode="NoResize"
Style="{DynamicResource ResourceKey=WindowStyle}" >
<Window.Resources>
<Image x:Key="Font" Source="pack://application,,,Resources/Resources/font.png" />
<Image x:Key="Open" Source="Pack://application,,,Resources/Resources/open.png" />
</Window.Resources>
<StackPanel Orientation="Vertical">
<DockPanel Margin="3" Style="{DynamicResource ResourceKey=ContentPanel}">
<CheckBox Name="chkAutoUpdate" Content="Auto Update" Margin="6,3,3,3" VerticalAlignment="Center" />
</DockPanel>
<DockPanel Margin="3" Style="{DynamicResource ResourceKey=ContentPanel}">
<Label Name="lblResultFont" VerticalContentAlignment="Center" Width="160" Margin="3" Content="Default Result Font" DockPanel.Dock="Left" />
<Button Name="btnResultFont" Margin="3" Height="22" Width="22" DockPanel.Dock="Right" Content="{StaticResource ResourceKey=Font}" />
<TextBox Name="txtResultFont" Margin="3" Height="21" IsReadOnly="True" DockPanel.Dock="Left" />
</DockPanel>
<DockPanel Margin="3,3,3,0" Style="{DynamicResource ResourceKey=ContentPanel}">
<Label Name="lblTempDirectory" VerticalContentAlignment="Center" Width="160" Margin="3" Content="Temporary Directory" DockPanel.Dock="Left" />
<Button Name="btnTempDirectory" Margin="3" Width="22" Height="22" DockPanel.Dock="Right" Content="{StaticResource ResourceKey=Open}" />
<TextBox Name="txtTempDirectory" Margin="3" Height="22" IsReadOnly="True" DockPanel.Dock="Left" />
</DockPanel>
<DockPanel Margin="3,0,3,0" Style="{DynamicResource ResourceKey=ContentPanel}">
<Label Name="lblCustomGrammarDirectory" VerticalContentAlignment="Center" Width="160" Margin="3" Content="Custom Grammar Directory" DockPanel.Dock="Left" />
<Button Name="btnCustomGrammarDirectory" Margin="3" Width="22" Height="22" DockPanel.Dock="Right" Content="{StaticResource ResourceKey=Open}" />
<TextBox Name="txtCustomGrammarDirectory" Margin="3" Height="21" IsReadOnly="True" DockPanel.Dock="Left" />
</DockPanel>
<DockPanel Margin="3,0,3,3" Style="{DynamicResource ResourceKey=ContentPanel}">
<Label Name="lblThemeDirectory" VerticalContentAlignment="Center" Width="160" Margin="3" Content="Theme Directory" DockPanel.Dock="Left" />
<Button Name="btnThemeDirectory" Margin="3" Width="22" Height="22" DockPanel.Dock="Right" Content="{StaticResource ResourceKey=Open}" />
<TextBox Name="txtThemeDirectory" Margin="3" Height="21" IsReadOnly="True" DockPanel.Dock="Left" />
</DockPanel>
<DockPanel Margin="3" LastChildFill="False">
<Button Name="btnOk" Style="{DynamicResource ResourceKey=ActionButton}" DockPanel.Dock="Right" Width="80" Margin="3" Content="_Ok" IsDefault="True" />
<Button Name="btnCancel" Style="{DynamicResource ResourceKey=ActionButton}" DockPanel.Dock="Right" Width="80" Margin="3" Content="_Cancel" IsCancel="True" />
</DockPanel>
</StackPanel>
</Window>
答案 0 :(得分:0)
我认为您的图片来源URi格式不正确。它应该像
pack://application:,,,/Subfolder/ResourceFile.xaml
(注意&#34之后的冒号;应用&#34;)
我通常做的只是
Source="Subfolder/ResourceFile.png"
更简单,更易读,更有效; - )
答案 1 :(得分:0)
我认为你的URI不正确你必须把源代码放在前面的答案之前 如果图像是EmbededResources,则URI工作。