如何从App.XAML中设置wp8中根布局的背景图像

时间:2014-04-30 06:56:21

标签: c# windows-phone-8

我正在使用App.Resources在我的所有wp8页面中设置网格布局的背景图像。我在一个问题堆栈中,用户可以选择三个背景之一。我不知道如何通过App.XAML实现这一目标。我设计了界面,用户可以选择其中一个背景,但不知道如何激活所选的。

这是我的APP.XAML

<Application.Resources>
 <Color x:Key="PageTitleForgroundColor">#000000</Color>
 <SolidColorBrush x:Key="PageTitleForegroundBrush" 
               Color="{StaticResource PageTitleForgroundColor}"/>

 <ImageBrush x:Key="AppBackgroundImage" ImageSource="/Assets/Background.jpg" 
               Stretch="UniformToFill"></ImageBrush>
 <ImageBrush x:Key="AppBackgroundImage_1" ImageSource="/Assets/Background.jpg" 
               Stretch="UniformToFill"></ImageBrush>
 <ImageBrush x:Key="AppBackgroundImage_2" ImageSource="/Assets/Background.jpg" 
               Stretch="UniformToFill"></ImageBrush>

 <!-- Grid Layout Background-->
 <Style x:Key="LayoutRootStyle" TargetType="Panel">
    <Setter Property="Background" Value="{StaticResource AppBackgroundImage}"></Setter>
 </Style>

</Application.Resources>

如您所见,我在APP.XAML中设置Background属性,如何根据用户选择使其动态化(应该是AppBackgroundImage_2,AppBackgroundImage_1和AppBackgroundImage)。

2 个答案:

答案 0 :(得分:0)

我怀疑您正试图在Panorama控件中更改此背景。 全景控制(我认为Pivot控件也有一些支持后期绑定的问题)。

要动态更改背景图片,您应该按照以下步骤操作;

  • 在关联的ViewModel中创建一个Property(MainBackGroundImage),它实现了INotifyPropertyChanged接口(如果您使用的是MVVM模式,那么您已经拥有了这个基础结构)。
  • 每当您想要更改背景图片时,都可以将任何图像路径(也可以是远程URL)分配给此属性。
  • 在View中连接到ViewModel的属性更改事件,并更新将出现背景图像的控件布局:

     void viewModel_PropertyChanged(object sender,PropertyChangedEventArgs e)
        {
    
        if (e.PropertyName == “MainBackGroundImage”)
        {
              this.MainPanorama.UpdateLayout();
        }
        }
    

答案 1 :(得分:0)

执行此操作的最佳方法是使用您的视图模型并访问您的配置并使用直接绑定来执行此操作。最大的问题源于样式内容这一事实一旦您使用它们就无法更改 - 在大多数平台上,您将获得访问被拒绝的异常。

如果你一直在努力使用资源,这就是我想出的。有可能更简单的方法......

  1. 创建一个名为ObjectHolder的类。您将在资源模型中实例化它。

    public class ObjectHolder:DependencyObject {     公共对象HeldObject     {         get {return(object)GetValue(HeldObjectProperty); }         set {SetValue(HeldObjectProperty,value); }     }

    // Using a DependencyProperty as the backing store for HeldObject.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty HeldObjectProperty =
        DependencyProperty.Register("HeldObject", typeof(object), typeof(ObjectHolder), new PropertyMetadata(null));
    

    }

  2. 在您的应用XAML中,实例化此内容。你需要2个样式 - 每个背景一个(我使用纯色画笔作为背景,因为它更容易测试)..

    <SolidColorBrush x:Key="AppBackgroundImage1" Color="Blue" />
    <SolidColorBrush x:Key="AppBackgroundImage2" Color="Red" />
    
    <!-- Grid Layout Background-->
    <Style x:Key="LayoutRootStyle1" TargetType="Panel">
        <Setter Property="Background" Value="{StaticResource AppBackgroundImage1}"></Setter>
    </Style>
    <Style x:Key="LayoutRootStyle2" TargetType="Panel">
        <Setter Property="Background" Value="{StaticResource AppBackgroundImage2}"></Setter>
    </Style>
    <local:ObjectHolder x:Key="holder" HeldObject="{StaticResource LayoutRootStyle1}"></local:ObjectHolder>
    
  3. 在页面XAML中,绑定到保持的对象而不是直接绑定到样式:

  4. 在代码中,切换样式时,请执行以下操作(您的代码实际上会更复杂一些 - 我只是从一个更改为另一个):

    private void Button_Click(object sender, RoutedEventArgs e)
    {
    
        var holder = (ObjectHolder)App.Current.Resources["holder"];
        holder.HeldObject = App.Current.Resources["LayoutRootStyle2"];
    }