wpf ImageBrush 尝试设置窗口背景时抛出异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17617086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImageBrush throws exception when trying to set Window background
提问by user1683456
I'm trying to set the background of my WPF window to an image but I'm getting this exception when I try to run it:
我正在尝试将 WPF 窗口的背景设置为图像,但是当我尝试运行它时出现此异常:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll Additional information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '8' and line position '10'.
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll Additional information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '8' and line position '10'.
I don't want to add the image to the project, as I would like to be able to change the image at runtime. My intention is to use databinding to set the background picture during start-up once I have this bit working.
我不想将图像添加到项目中,因为我希望能够在运行时更改图像。我的目的是在启动过程中使用数据绑定来设置背景图片,一旦我开始工作。
Source Code:
源代码:
<Window x:Class="ColinsTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Colin's Test Application"
WindowStyle="None"
WindowState="Maximized">
<Window.Background>
<ImageBrush
ImageSource="DeLaRue-Background.jpg"/>
</Window.Background>
<Grid></Grid>
</Window>
Any Ideas? Thanks
有任何想法吗?谢谢
回答by John Smith
Wrong. It should be set to CONTENT not Resource.
错误的。它应该设置为内容而不是资源。
Include in your project (use the file explorer)
包含在您的项目中(使用文件浏览器)
Right click on the image > Properties > Advanced.
右键单击图像 > 属性 > 高级。
Build Action: Content
构建操作:内容
Copy to Output Directory: Copy always.
复制到输出目录:始终复制。
回答by Mateusz Dembski
Your image's build action is probably set to Content, change it to Resource
您的图像的构建操作可能设置为内容,将其更改为资源
Set the item (your image file) to be a Resource. (In Visual Studio) Right click,Properties,Build Action,Resource
将项目(您的图像文件)设置为资源。(在 Visual Studio 中)右键单击,属性,构建操作,资源
回答by Adam Zátopek
I had the same problem with
我有同样的问题
<Button>
<Button.Background>
<ImageBrush ImageSource="/Resources/icon_done.png">
</Button.Background>
</Button>
I used /before Resources and it work correctly, but i used solution with properties image ->Properties->Build to Resources.
These two actions solved my issue.
我/之前使用过 Resources 并且它工作正常,但我使用了带有属性 image 的解决方案->Properties->Build to Resources。这两个动作解决了我的问题。
回答by Test_user
Add the forward "/" to the location
将向前的“/”添加到位置
Then the whole ImageSource string should look like
然后整个 ImageSource 字符串应该看起来像
<ImageBrush ImageSource="/Resources/Image1.png" Stretch="None" />

