2012-04-06 124 views
3

我试图更改app.xaml中所有我的xaml页面的背景图像,但未成功。Windows Phone应用程序背景图像

我想接下来,在App构造函数:

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

RootFrame.Background = imageBrush; 

我不想在页面级别要做到这一点,因为所有的网页将根据所选择的主题具有相同的背景图片由用户。

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

+0

你可以很容易地在每个页面上的属性添加到BaseViewModel并绑定到它(你很可能使用查找和替换做出改变,甚至) – 2012-04-06 15:57:18

+0

我不想在页面级别执行此操作。为什么在应用程序级别可以完成每个页面的添加?我会把你的想法留在我的后兜里,以防我不清楚。感谢这个想法。 – Dante 2012-04-06 15:59:16

回答

10

我最终做的事:

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

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(); 
+1

buildaction应该是图像的内容 – Naresh 2013-03-10 16:36:40

0

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

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

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

请注意,如果您AREN这是使用工具包.. 't使用它,你应该使用'PhoneApplicationFrame'而不是工具箱:TransitionFrame

+0

你的想法很有意思,但是为了改变所有页面的背景图像而创建一个模板看起来对我来说太过分了。为什么我的原始代码没有工作?这是我的主要问题... – Dante 2012-04-06 16:43:33

2

虽然你的代码片段并没有为我工作之一,使用

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

一样。您需要添加以下到你的资源字典中App.xaml

<ImageBrush x:Key="MainBackground" ImageSource="/resources/MainBackground.jpg" /> 
相关问题