UWP Programmatically change the background image of a XAML C # page

I searched for a while, and honestly, I did not solve this very simple problem. I have a popup menu and I have bound the event to a popup menu item. I want to programmatically change the background image of my XAML page when I select an item.

<Grid x:Name="main">
    <Grid.Background>
        <ImageBrush Stretch="Fill" ImageSource="Assets/bg_1.jpg"/>
    </Grid.Background>
</Grid>

This is the default image that I got as the background (the grid is basically a whole page).

The C # event code is here:

private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
{
    main.Background = new ImageBrush { ImageSource = new BitmapImage(new Uri("ms-appx:///MTG Life Counter/Assets/bg_2.jpg")) , Stretch = Stretch.None};
}

When I select a menu item instead of setting the image as a background, it makes it white.

Default background

I tested and this is not an image problem. I know that I am doing something like an obvious mistake, but I am a little new to UWP and I could not find a solution.

Here is an empty background after I changed it

Thank you for your time and sorry for ignorance.

+4
1

"BaseUri"... .

:

    private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
    {
        main.Background = new ImageBrush { ImageSource = new BitmapImage(new Uri(this.BaseUri, "Assets/bg_2.jpg")), Stretch = Stretch.None };
    }
+2

Source: https://habr.com/ru/post/1653581/


All Articles