2012-05-26 26 views
0

在我目前的Windows Phone 7應用中,我們想要使用兩個主題:黑暗與光明,並且我們還在我們的應用中使用了一些全景和透視控制。每個控件對於每個暗色和亮色主題都有不同的圖像。加載WP7中黑暗與光明主題的圖片

爲此,我在我的項目中做了兩個不同的主題。但是,我無法爲主題加載圖像。任何人都可以請建議如何從主題加載圖像根據是否是黑暗或光線版本?

+0

? – Blender

+1

我猜是因爲寫作的乏味。對不起對於困擾。 :) 現在你可以請來真正的問題。即我的問題 –

回答

0

我想更好的辦法是兩個單獨的主題之一爲淺色和深色主題,然後以Decsion在應用程序加載到主題能見度位。

在我們的主題爲什麼要利用每一個字,我們可以加載相似圖片這

<BitmapImage x:Key="SearchImage" UriSource="/Images/searchIcon.png" /> 
0

您可以使用下面的代碼片斷來決定主題,並相應地設置圖像。

if ((double)Application.Current.Resources["PhoneDarkThemeOpacity"] == 1) 
{ 
    //Dark Theme 
} 
else 
{ 
    //Light Theme 
} 

您還可以看看here瞭解更多詳情。

1

如果你只顯示圖像,你可以創建一個ValueConverter確定主題:

// Assumes a dark theme image is passed in the parameter variable 
public class ImageThemeValueConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if(parameter == null) return null; 

     Visibility darkBackgroundVisibility = 
      (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"]; 

     if (darkBackgroundVisibility == Visibility.Visible) 
     { 
      return new Uri(parameter.ToString(); 
     } 
     else 
     { 
      string path = parameter.ToString(); 
      path = System.IO.Path.GetDirectoryName(path) + System.IO.Path.GetFileNameWithoutExtension(path) + ".light"+System.IO.Path.GetExtension(path); 
      return new Uri(path); 
     } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

,並在您的XAML:

<Image Source="{Binding Converter={StaticResource ImageThemeValueConverter}, ConverterParameter=Images/ThemeImage.png}"/> 

如果把圖像的按鈕和圖像只使用背景和前景色(黑色和白色),你可以有一種會改變顏色的風格。

<Button Style="{StaticResource PhoneButton}"> 
    <ImageBrush ImageSource="/Images/appbar.save.png" Stretch="None"/> 
</Button> 

而且款式:

<Style TargetType="Button" x:Key="PhoneButton"> 
    <Setter Property="Width" Value="48"/> 
    <Setter Property="Height" Value="48"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Grid> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal"/> 
          <VisualState x:Name="MouseOver"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.HorizontalAlignment)" Storyboard.TargetName="ContentContainer"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <HorizontalAlignment>Stretch</HorizontalAlignment> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.VerticalAlignment)" Storyboard.TargetName="ContentContainer"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <VerticalAlignment>Stretch</VerticalAlignment> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Pressed"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ContentContainer"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneBackgroundBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="Border"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Stroke" Storyboard.TargetName="Border"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ContentContainer"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Stroke" Storyboard.TargetName="Border"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="Border"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Grid> 
         <Ellipse x:Name="Border" Fill="{TemplateBinding Background}" Canvas.Left="0" Stretch="Fill" 
            StrokeThickness="3" StrokeLineJoin="Round" Stroke="{TemplateBinding BorderBrush}"/> 
         <Grid x:Name="ContentContainer" OpacityMask="{TemplateBinding Content}" Background="{TemplateBinding Foreground}"/> 
        </Grid> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style>