简体   繁体   English

根据WP7中的黑暗与光明主题加载图像

[英]Load Images According To Dark And Light Theme in WP7

In my current Windows Phone 7 app, we want to use two themes: Dark And Light, and we are also using some panoramic and pivot controls in our app. 在我当前的Windows Phone 7应用程序中,我们要使用两个主题:“黑暗与光明”,我们还在应用程序中使用了一些全景和枢轴控件。 Each control has a different image for each dark and light theme. 每个控件对于每个黑暗和明亮主题都有不同的图像。

For this purpose, I made two separate themes in my project. 为此,我在项目中制作了两个单独的主题。 However, I am unable to load images for the theme. 但是,我无法加载该主题的图像。 Can anyone please suggest how I can load images from the theme according to whether it is the dark or light version? 有人可以建议我如何根据主题是深色还是浅色从主题加载图像?

If you're only displaying images you can create a ValueConverter to determine the theme: 如果仅显示图像,则可以创建一个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();
    }
}

and in your xaml: 并在您的xaml中:

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

If putting the images in an button and the images only use the background and foreground colors (black and white) you can have a style that will change the color. 如果将图像放在按钮中,并且图像仅使用背景色和前景色(黑色和白色),则可以使用一种可以更改颜色的样式。

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

And the style: 和样式:

<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>

You can use the following code snippet to decide the theme, and set the images accordingly. 您可以使用以下代码片段确定主题,并相应地设置图像。

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

You can also have a look here for more details. 您也可以在这里查看更多详细信息。

I Guess Better Approach is Make Two Separate Themes One For Light And Dark Theme And Then Take Decsion On Application Loading By Theme Visibility Bit. 我猜更好的方法是将两个单独的主题分别设置为浅色和深色主题,然后按主题可见性位决定应用程序的加载。

In our Theme We Can Load Images Like That 在我们的主题中,我们可以像这样加载图像

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM