【问题标题】:How to set a background of WPF Window?如何设置 WPF 窗口的背景?
【发布时间】:2010-10-07 20:23:04
【问题描述】:

我有一个简单的 WPF 窗口。我打算将它的背景设置为我作为嵌入式资源添加到项目中的图像之一。这是我尝试过的:

<Window x:Class="A_Boggle.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="A-Boggle" Height="300" Width="625" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Name="Game">
<Window.Background>
    <ImageBrush ImageSource="background.jpg"></ImageBrush>
</Window.Background>
<Grid>
</Grid>

但是有了这个,我总是得到这个:“错误 1 ​​文件 splash.jpg 不是项目的一部分,或者它的“构建操作”属性未设置为“资源”。”

有什么想法吗?

【问题讨论】:

    标签: wpf background


    【解决方案1】:

    转到 VS 中的图像并将项目设置为资源。右键 -> 属性 -> 构建操作 -> 资源

    更新:

    如果在文件夹中,则需要更改路径。即...资源/背景.jpg

    【讨论】:

    • 对。请注意,“资源”与“嵌入式资源”不同。
    • 仍然无法正常工作,出现同样的错误。图片在 Resources 文件夹中是否重要?
    • 试过这个,我得到错误'找不到路径的一部分'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Images\marmor_hintergrund_weiss.png' .´ 知道如何让我的 xaml 在我的项目文件夹而不是 IDEs 文件夹中查看吗?
    • “程序文件”中的某些内容可能会遇到权限或访问问题。
    【解决方案2】:

    您可以在 main.xaml.cs 中使用它

      InitializeComponent();
            ImageBrush myBrush = new ImageBrush();
            myBrush.ImageSource =
                new BitmapImage(new Uri("F://13.png", UriKind.Absolute));
            this.Background = myBrush;
    

    【讨论】:

      【解决方案3】:

      我的问题是因为我在窗口上设置了高度和宽度属性并使用最大化的窗口状态,当我删除宽度和高度属性时,错误消失了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-19
        • 2011-09-10
        相关资源
        最近更新 更多