在WP7中根据黑暗和光明主题加载图像

时间:2012-05-26 08:02:02

标签: silverlight windows-phone-7 xaml generic.xaml

在我目前的Windows Phone 7应用程序中,我们想要使用两个主题:Dark和Light,我们还在我们的应用程序中使用了一些全景和旋转控件。每个控件都有不同的图像,用于每个黑暗和明亮的主题。

为此,我在我的项目中制作了两个不同的主题。但是,我无法加载主题的图像。任何人都可以建议我如何从主题加载图像,无论是暗版还是浅版?

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

您可以使用以下代码段来决定主题,并相应地设置图片。

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

您还可以查看here了解详情。

答案 2 :(得分:0)

我猜更好的方法是为光明和黑暗主题制作两个单独的主题,然后通过主题可见性位对应用程序加载进行决策。

在我们的主题中,我们可以像这样加载图像

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