Windows Phone应用程序背景图像

我正在尝试更改app.xaml中所有xaml页面的背景图像,但未成功。

我在App构造函数中尝试以下内容:

var imageBrush = new ImageBrush { ImageSource = new BitmapImage(new Uri("/Images/SomeBackgroundImage.png", UriKind.Relative)) }; RootFrame.Background = imageBrush; 

我不想在页面级别执行此操作,因为所有页面将具有相同的背景图像,具体取决于用户选择的主题。

关于我在这里做错了什么的想法?

我最终做了什么:

我根据所选主题创建了一个选择正确背景图像的方法。

 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; } 

在每个页面中,我设置了背景图像。

 LayoutRoot.Background = Utils.GetBackground(); 

虽然你的代码片段对我来说也不起作用,但是使用它

 RootFrame.Background = App.Current.Resources["MainBackground"] as ImageBrush; 

确实。 您需要将以下内容添加到App.xaml资源字典中

  

我使用自定义样式使我的框架背景变白:

         

然后,在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; } 

请注意,这是使用工具包..如果您不使用它,您应该使用’PhoneApplicationFrame’而不是工具包:TransitionFrame