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

use*_*456 2 wpf background image 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>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?谢谢

小智 9

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

包含在您的项目中(使用文件资源管理器)

右键单击图像>属性>高级.

构建行动:内容

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