如何从WP7中的另一个页面更新背景图像

时间:2012-07-12 19:09:36

标签: c# windows-phone-7 xaml

我在MainPage.xaml中有一个全景控件,我希望用户能够通过使用listpicker控件更改另一个页面中的背景png图像,即SettingsPage.xaml。我已成功使用一些测试图像名称填充listpicker,我可以使用它来更改选择,但我不确定如何从我的SettingsPage.xaml访问MainPage.xaml以更改Listpicker selectedIndex时的实际图像改变了。到目前为止,我所拥有的是以下内容:

MainPage.xaml中

<controls:Panorama x:Name="panorama" Title="Application Title">
        <controls:Panorama.Background>
            <ImageBrush ImageSource="PanoramaBackground.png"/>  //the default background
        </controls:Panorama.Background>

        ...

</controls:Panorama>

SettingsPage.xaml

<toolkit:ListPicker x:Name="ThemeListPicker" Header="Theme" Grid.Row="2" Grid.ColumnSpan="2"
                                        ItemTemplate="{Binding ThemeItemTemplate}" SelectedIndex="{Binding}"
                                        SelectionChanged="ThemeListPicker_SelectionChanged"/>

SettingsPage.xaml.cs

String[] Theme = 
    {
        "Default",
        "Bubbles",
        //...
    };

public SettingsPage()
    {
        InitializeComponent();

        //Theme list picker
        this.ThemeListPicker.ItemsSource = Theme;
        this.ThemeListPicker.DataContext = ThemeListPicker.SelectedIndex;
    }

private void ThemeListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems.Count <= 0) //to eliminate IndexOutOfRangeException
        {
            return;
        }

        string selectedItem = e.AddedItems[0] as string;

        switch(selectedItem)
        {
            case "Default":
                //change panorama background here (PanoramaBackground.png)
                this.ThemeListPicker.SelectedIndex = 0;
                break;
            case "Bubbles":
                //change panorama background here (PanoramaBackground_Bubbles.png)
                this.ThemeListPicker.SelectedIndex = 1;
                break;
        }
    }

2 个答案:

答案 0 :(得分:1)

您可以创建一个Settings.cs类,该类可用于在隔离存储中保存设置条目,其中一个设置可能是背景图像。

请参阅此处了解实施示例:

http://msdn.microsoft.com/en-us/library/ff769510%28v=vs.92%29.aspx

如果您在应用程序运行时需要设置,那么实现所需内容的最简单方法是在App.xaml.cs中使用公共字符串对象(例如,名为YourString)

然后你可以从Settings.xaml页面设置它,实际上是后面的代码,当你的列表选择器选择改变如下:

private void ThemeListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.AddedItems.Count <= 0) //to eliminate IndexOutOfRangeException
    {
        return;
    }

    string selectedItem = e.AddedItems[0] as string;

    switch(selectedItem)
    {
        case "Default":
            //change panorama background here (PanoramaBackground.png)
            (Application.Current as App).YourString = "PanoramaBackground.png";
            break;
        case "Bubbles":
            //change panorama background here (PanoramaBackground_Bubbles.png)
            (Application.Current as App).YourString = "PanoramaBackground_Bubbles.png";
            break;
    }
}

然后,当您导航到MainPage.xaml页面时,检查YourString字符串是否为空。如果不是,请从中创建URI并将Panorama背景设置为该图像URI。

答案 1 :(得分:0)

要解决此问题,我只需在MainPage.xaml中设置原始默认ImageSource路径,然后在MainPage.xaml.cs中设置OnNavigatedTo事件,我相应地更新了设置页面中设置的全景背景图像。

MainPage.xaml中

<controls:Panorama x:Name="panorama" Title="Application Title"> 
    <controls:Panorama.Background> 
        <ImageBrush ImageSource="PanoramaBackground.png"/>  //the default background 
    </controls:Panorama.Background> 

    ... 

</controls:Panorama> 

MainPage.xaml.cs中

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        Uri themeUri = new Uri(Settings.Theme.Value, UriKind.Relative);  //holds the uri string of the selected background from SettingsPage

        BitmapImage image = new BitmapImage(themeUri);
        ImageBrush brush = new ImageBrush();
        brush.ImageSource = image;
        panorama.Background = brush;
    }