2012-07-12 62 views
0

更新的背景圖像我在MainPage.xaml中全景控制,我想用戶能夠改變的背景中另一個頁面png圖片,我SettingsPage.xaml,通過使用一個listpicker控件。我已經成功地填充listpicker用幾個測試圖像的名字,我能夠用它來改變選擇,但我不確定如何從我的SettingsPage.xaml訪問MainPage.xaml中改變實際的圖像時,Listpicker的selectedIndex被改變。到目前爲止,我有什麼是folows:如何從另一頁WP7

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

回答

1

您可以創建一個設置.cs類可用於在獨立存儲中保存設置條目,其中一個設置可能是th e背景圖像。

看到這裏執行樣本:

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

如果你只需要同時應用程序運行時的設置,最簡單的方式來完成你想要的是使用一個公共字符串對象(例如所謂YourString)的App.xaml.cs

內然後,你可以從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。

+0

好吧,如果我這樣做,我想我的問題是什麼是創造的URI,然後設置的正確方法該URI等於全景背景(假設我添加'X:名稱=「panBackgroundImage」'作爲圖像刷名) – Matthew 2012-07-13 07:45:51

+0

變種名稱=(Application.Current如APP).YourString; BitmapImage image = new BitmapImage(new Uri(name,UriKind.Relative)); ImageBrush brush = new ImageBrush(); brush.ImageSource = image; panorama.Background = brush; 當然,這是以防萬一你的形象(「PanoramaBackground_Bubbles.png」爲例)是在項目的根目錄下。如果不是,則需要將文件夾名稱添加到字符串YourString(例如「/ Images /」+ name) – 2012-07-13 07:56:01

0

爲了解決這個問題,我簡單地在MainPage.xaml中設置原始默認ImageSource路徑,然後在MainPage.xaml.cs中設置OnNavigatedTo事件,相應地在SettingsPage中設置了全景背景圖像。

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