2012-12-25 26 views
0

我在此msdn articles中爲應用程序設置創建了布爾變量。現在我想在我的菜單中選擇toggleswitch,它可以設置音量開啓或關閉。我正在考慮使用這樣的東西this answer。但我不確定這是否對我的問題有好處。我應該爲圖像添加一個變量到我的AppSettings,還是有更好的方法來做到這一點?WP7 - 使用圖標切換聲音開/關

我的解決辦法:

在XAML:

<ToggleButton Name="tgbSound" BorderThickness="0" Background="Transparent" 
         Checked="tgbSound_Checked" 
         Unchecked="tgbSound_Unchecked" 
         IsChecked="{Binding Source={StaticResource appSettings}, Path=SoundSetting, Mode=TwoWay}"> 
     </ToggleButton> 

在對XAML頁面代碼:

private void tgbSound_Checked(object sender, RoutedEventArgs e) 
{ 
    SetTgbSoundContentTo("Images/volumeon.png"); 
} 

private void tgbSound_Unchecked(object sender, RoutedEventArgs e) 
{ 
    SetTgbSoundContentTo("Images/volumeoff.png"); 
} 

private void SetTgbSoundContentTo(string uri) 
{ 
    Image volumeoff = new Image(); 
    ImageSource zdroj = new BitmapImage(new Uri(uri, UriKind.Relative)); 
    volumeoff.Source = zdroj; 
    volumeoff.Height = 40; 
    if (tgbSound == null) 
     return; 
    tgbSound.Content = volumeoff; 
    tgbSound.Background = new SolidColorBrush(Colors.Transparent); 
} 
+0

你所指的答案看起來應該適合你想要做的事情。 –

回答

4

你都可以做。對於這兩種方法,我會建議你採用一種常用的方法。

在您的AppSettings中創建一個布爾屬性並在您的viewmodel中創建其包裝。 然後將視圖模型中的包裝屬性(雙向)綁定到UI元素,該元素既可以是切換開關,也可以是圖像。在toggleswitch的情況下,two way binding本身就會訣竅,但是對於圖像,您必須處理tap事件,並自己處理代碼中的開/關狀態和布爾值。

+0

我編輯了我的問題並在那裏顯示解決方案。感謝幫助 –