Windows Phone Desktop Image
I am trying to change the background image for all my xaml pages in app.xaml with no success.
I am trying to do the following in the App constructor:
var imageBrush = new ImageBrush
{
ImageSource = new BitmapImage(new Uri("/Images/SomeBackgroundImage.png", UriKind.Relative))
};
RootFrame.Background = imageBrush;
I don't want to do this at the page level as all pages will have the same background image depending on the theme the user has selected.
Any ideas on what I am doing wrong here?
source to share
What I ended up with:
I created a method that selects the correct background image, depending on the theme I chose.
public static ImageBrush GetBackground()
{
var imageBrush = new ImageBrush();
if ((Visibility)App.Current.Resources["PhoneLightThemeVisibility"] == Visibility.Visible)
{
imageBrush = new ImageBrush
{
ImageSource = new BitmapImage(new Uri("/Images/Background1.png", UriKind.Relative))
};
}
else
{
imageBrush = new ImageBrush
{
ImageSource = new BitmapImage(new Uri("/Images/Background2.png", UriKind.Relative))
};
}
return imageBrush;
}
And on each page, I set a background image.
LayoutRoot.Background = Utils.GetBackground();
source to share
Although your code snippet didn't work for me using
RootFrame.Background = App.Current.Resources["MainBackground"] as ImageBrush;
does. You need to add to the resource dictionary inApp.xaml
<ImageBrush x:Key="MainBackground" ImageSource="/resources/MainBackground.jpg" />
source to share
I use my own style to make the background image white:
<ControlTemplate x:Key="WhiteTemplate" TargetType="toolkit:TransitionFrame">
<Grid x:Name="ClientArea">
<Grid.Background>
<ImageBrush ImageSource="Images/yourimage.png"
</Grid.Background>
<ContentPresenter x:Name="FirstContentPresenter" />
<ContentPresenter x:Name="SecondContentPresenter" />
</Grid>
</ControlTemplate>
Then in App.xaml.cs
private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
{
// Set the root visual to allow the application to render
if (RootVisual != RootFrame)
RootVisual = RootFrame;
// Remove this handler since it is no longer needed
RootFrame.Navigated -= CompleteInitializePhoneApplication;
// Add this to inject the media element Control template
RootFrame.Template = Current.Resources["WhiteTemplate"] as ControlTemplate;
}
Please note that this uses the Toolkit. If you are not using it, you should probably use "PhoneApplicationFrame" instead of toolkit: TransitionFrame
source to share