.net WPF按钮图像在设计时显示,但在运行时不显示
我在resources文件夹中存储了两个图像,它们是将Build Action设置为resources,将Copy to Output Directory设置为not Copy。图像是“font.png”和“open.png”。但是,当我查看设计器时,按钮中显示的图像与我希望的完全相同;当我运行应用程序时,按钮是空的。下面是我的XAML,我做错了什么.net WPF按钮图像在设计时显示,但在运行时不显示,.net,wpf,image,xaml,.net,Wpf,Image,Xaml,我在resources文件夹中存储了两个图像,它们是将Build Action设置为resources,将Copy to Output Directory设置为not Copy。图像是“font.png”和“open.png”。但是,当我查看设计器时,按钮中显示的图像与我希望的完全相同;当我运行应用程序时,按钮是空的。下面是我的XAML,我做错了什么 <Window x:Class="Preferences" xmlns="http://schemas.microsoft.
<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>
我认为您的图像源URi的格式不正确。应该是这样的
pack://application:,,,/Subfolder/ResourceFile.xaml
(注意“application”后面的冒号)
我通常做的就是吃点东西
Source="Subfolder/ResourceFile.png"
更简单,更可读,更有效;-) 我认为您的URI不正确,您必须将源代码放在答案之前
如果图像是EmbeddedResources,您的URI就可以工作。看看这些答案:可能是包URI不正确(有多种格式可以工作,我不熟悉您的有效格式),这是我的问题。工作的路径是:
pack://application:,,,/Resources/Open.png