2012-05-26 27 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>