C# WinUI 3 desktop application. How to set background image of relative panel or page

Viewed 27

I just want to set a background image on a relative panel, and a page. I have tried to set the background image in a stub program on the main window. I am able to set background colors but not set an image to be the background. Here is my code. I used ImageBrush ImageSource but nothing shows up. I physically copied the image file into the project, into the 'Assets' folder. What am I doing wrong? Thank you for your help.

<Window
    x:Class="App_WinUI3_Background_Sandbox.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:App_WinUI3_Background_Sandbox"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Border>
        <Border.Background>
            <!--<ImageBrush ImageSource="Assets/githubglobe.png"/>-->
            THIS DOES NOT WORK.
            <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                THIS WORKS!!!!
                <GradientStop Offset="0.0" Color="Yellow" />
                <GradientStop Offset="0.25" Color="Red" />
                <GradientStop Offset="0.75" Color="Blue" />
                <GradientStop Offset="1.0" Color="LimeGreen" />
            </LinearGradientBrush>
        </Border.Background>
        <StackPanel
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Orientation="Horizontal">
            <StackPanel.Background>
                <ImageBrush ImageSource="Assets/githubglobe.png" />
                THIS DID NOT WORK EITHER
            </StackPanel.Background>
            <Button
                x:Name="myButton"
                Click="myButton_Click">
                Click Me
            </Button>
        </StackPanel>
    </Border>
</Window>
1 Answers

I figured it out a few minutes later. Set the 'Build Action' property of the image from 'None' to 'Content' in the solution explorer. Now the background image works perfectly!!!

Related