如何设置 WPF Window 的背景?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3885581/
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
How to set a background of WPF Window?
提问by sokolovic
I have a simple WPF window. I intend to set it's background to be one of the images I added to project as Embedded Resource. This is what I tried:
我有一个简单的 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>
But with this, i always get this: "Error 1 The file splash.jpg is not part of the project or its 'Build Action' property is not set to 'Resource'."
但是有了这个,我总是得到这个:“错误 1 文件 splash.jpg 不是项目的一部分,或者它的‘构建操作’属性未设置为‘资源’。”
Any ideas?
有任何想法吗?
回答by Aaron McIver
Go to the image within VS and set the item to be a Resource. Right click -> Properties -> Build Action -> Resource
转到 VS 中的图像并将项目设置为资源。右键单击 -> 属性 -> 构建操作 -> 资源
Update:
更新:
You need to change the path if it is in a folder. ie...Resources/background.jpg
如果路径在文件夹中,则需要更改路径。即...资源/背景.jpg
回答by Ahmed Wafi
you can use this in main.xaml.cs
您可以在 main.xaml.cs 中使用它
InitializeComponent();
ImageBrush myBrush = new ImageBrush();
myBrush.ImageSource =
new BitmapImage(new Uri("F://13.png", UriKind.Absolute));
this.Background = myBrush;
回答by DarkIcaro
the issue in my case was becouse i set the Heigth and width properties on the window and use maximized windows state , when i remove the width and heigth properties the error dissapear.
我的问题是因为我在窗口上设置了高度和宽度属性并使用最大化的窗口状态,当我删除宽度和高度属性时,错误消失了。