尝试设置Window背景时,ImageBrush会抛出异常

时间:2013-07-12 14:18:50

标签: wpf image background imagebrush

我正在尝试将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'.

我不想将图像添加到项目中,因为我希望能够在运行时更改图像。我的目的是使用数据绑定在启动期间设置背景图片,一旦我有这个位。

源代码:

<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>

任何想法?感谢

4 个答案:

答案 0 :(得分:8)

错误。它应该设置为CONTENT而不是Resource。

包含在您的项目中(使用文件浏览器)

  

右键点击图片&gt;属性&gt;高级。

构建操作:内容

复制到输出目录:始终复制。

答案 1 :(得分:2)

您的图片的构建操作可能已设置为“内容”,将其更改为“资源”

将项目(您的图像文件)设置为资源。 (在Visual Studio中)右键单击“属性”,“构建操作”,“资源”

答案 2 :(得分:-1)

我遇到了与

相同的问题
 <Button>
     <Button.Background>
         <ImageBrush ImageSource="/Resources/icon_done.png">
     </Button.Background>
 </Button>

我在资源之前使用了/并且它正常工作,但我使用了属性图像->Properties->Build to Resources的解决方案。 这两个动作解决了我的问题。

答案 3 :(得分:-2)

添加转发&#34; /&#34;到位置

然后整个ImageSource字符串应该看起来像

<ImageBrush ImageSource="/Resources/Image1.png" Stretch="None" />