Moo*_*ght 50 c# wpf image button
全部,我有一个小应用程序,它检查.resx文件以确保嵌入式括号的一致性(以便不会发生不匹配的"... {0}"字符串的运行时错误).我有MainWindow.xaml的以下XAML,我的特殊问题涉及按钮上显示的图像
<Window x:Class="ResxChecker.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="174.383" Width="495.869">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="350*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="30*"/>
</Grid.RowDefinitions>
<Label Content="Select .resx file:" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Height="24" Width="Auto" Grid.ColumnSpan="1"/>
<TextBox Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Margin="10,0,0,0" Grid.Row="1" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/>
<Button Grid.Column="2" HorizontalAlignment="Right" Margin="5,0,10,0" Grid.Row="1">
<Image VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="16 " Width="16" Source="pack://siteoforigin:,,,/Resources/UserCost2013Open16.png"/>
</Button>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
图像具有"构建操作=资源","复制到输出目录=不复制" - 图像显示在设计器中但不在运行时显示.我已经看到了以下问题并阅读了相关答案,但没有解决问题:
如何让按钮图像在运行时出现?
Phi*_*hil 68
将构建操作更改为"资源".你的包网址也错了.使用:
Source="pack://application:,,,/Resource/UserCost2013Open16.png"
Run Code Online (Sandbox Code Playgroud)
或者干脆
Source="/Resource/UserCost2013Open16.png"
Run Code Online (Sandbox Code Playgroud)
Bas*_*sti 28
有2个解决方案:
1:更改图像的设置:
Build Action = Content
Copy to output directory = Copy if newer
Source="pack://siteoforigin:,,,/Resources/UserCost2013Open16.png"
Run Code Online (Sandbox Code Playgroud)
2:在源路径中使用应用程序而不是siteoforigin时,您必须采用以下方法:
a)图像将位于名为"Resources"的SubFolder中,而.exe文件将很小
Source="pack://application:,,,/Resources/UserCost2013Open16.png"
Build Action = Content
Copy to output directory = Copy if newer
Run Code Online (Sandbox Code Playgroud)
b)图像将包含在.exe中,并且不存在带有imagefile的子文件夹
Source="pack://application:,,,/Resources/UserCost2013Open16.png"
Build Action = Resource
Copy to output directory = Copy if newer
Run Code Online (Sandbox Code Playgroud)
在我的例子中,我将图像放在一个名为的单独项目中Common
,图像Resources
位于此项目中命名的文件夹下.在我的另一个项目中,我添加了一个引用Common
并设置了这样的图像源:
<Image Source="/Common;component/Resources/anImage.png"/>
Run Code Online (Sandbox Code Playgroud)
该图像具有Build Action
设置为Resource
与Copy to Output Directory
对Do not copy
.但是,由于一些奇怪的原因,直到我删除了我的解决方案中的每个汇编文件并创建了一个Clean Solution
和Build Solution
.不知道为什么,但是一旦我重建了一切,它都开始在运行时工作.我仍然无法弄清楚为什么它在Design Time工作.
假设你有
设置你Build Action
的Resource
正确使用设置该路径 URI-PACK-FORMAT
在我的情况下它仍然没有显示.
Clean & Rebuild
不只是Build为我修好了!
在 Visual Studio 中执行以下步骤
Assets
命名文件夹。Resource
为Build Action
.<MenuItem.Icon>
,它应该正确路径。