簡體   English   中英

WP7-使用圖標打開/關閉聲音

[英]WP7 - switching sound on/off using icon

我在此msdn文章中使用應用程序設置為聲音創建了布爾變量。 現在,我希望在菜單toggleswitch其作為toggleswitch ,可以將音量設置為開或關。 我正在考慮使用這種答案 但是我不確定這對我的問題是否有幫助。 我應該在我的AppSettings中添加圖像變量還是有更好的方法?

我的解決方案:

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

兩者都可以。 對於這兩種方法,我都會建議您采用一種通用方法。

在您的AppSettings中創建一個布爾屬性,並在視圖模型中創建其包裝。 然后將視圖模型中的wrapper屬性綁定(雙向)到UI元素,該UI元素既可以是撥動開關,也可以是圖像。 在使用toggleswitch情況下, two way binding本身可以解決問題,但是對於圖像,您將不得不自己處理代碼中的tap事件並處理開/關狀態和布爾值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM