Windows phone 8 如何在windows phone上通过代码设置背景图像?

Windows phone 8 如何在windows phone上通过代码设置背景图像?,windows-phone-8,background,Windows Phone 8,Background,我的问题很简单,但我找不到有效的解决方案 我编写了一个Windows Phone应用程序,我必须更改背景。更改取决于应用程序的用户配置文件。如果用户是男性,应用程序将显示蓝色背景,如果用户是女性,则显示粉色背景 我尝试绑定背景,但仍然有闪烁效果(背景图像为黑色)。 当应用程序在两个页面之间导航时,它非常可见,但在同一页面上进行更改时,它不可见 我也尝试过这些解决方案,但背景一直是黑色的: stringuri=string.Format(“/Assets/Background{0}.png”,Ap

我的问题很简单,但我找不到有效的解决方案

我编写了一个Windows Phone应用程序,我必须更改背景。更改取决于应用程序的用户配置文件。如果用户是男性,应用程序将显示蓝色背景,如果用户是女性,则显示粉色背景

我尝试绑定背景,但仍然有闪烁效果(背景图像为黑色)。 当应用程序在两个页面之间导航时,它非常可见,但在同一页面上进行更改时,它不可见

我也尝试过这些解决方案,但背景一直是黑色的:

stringuri=string.Format(“/Assets/Background{0}.png”,App.Context.SelectedUser.IsMan?”男孩:“女孩”);
var imageBrush=new System.Windows.Media.imageBrush
{
ImageSource=新位图图像(新Uri(Uri,UriKind.Relative))
};
this.Background=imageBrush;
stringuri=string.Format(“/Assets/Background{0}.png”,App.Context.SelectedUser.IsMan?“Boy”:“Girl”);
var imageBrush=new System.Windows.Media.imageBrush
{
ImageSource=新位图图像(新Uri(Uri,UriKind.Relative))
};
App.RootFrame.Background=imageBrush;
我尝试使用.jpg和.png图像文件,但都不起作用

我的代码错了吗? 有人有什么建议吗


非常感谢:-)

此代码适用于Windows Phone 8中的空白silverlight应用程序

在Xaml中:

<Grid x:Name="LayoutRoot" >
    <Grid.Background>
        <ImageBrush ImageSource="/Assets/boy.png" Stretch="UniformToFill" />
    </Grid.Background>
    <Button Content="Change" Click="Button_Click" />
</Grid>

谢谢Alex,我的代码错了。新手犯的错误:-/。在我的代码中,我使用了“this”,但它不是一个好的目标。就像你写的,我必须使用网格(在我们的例子中是LayoutRoot)。
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
        bmp.UriSource = new Uri("/Assets/girl.png", UriKind.Relative);

        var imageBrush = new System.Windows.Media.ImageBrush
        {
            ImageSource = bmp
        };

        LayoutRoot.Background = imageBrush;
    }