简体   繁体   English

如果主题很轻,WP7 / XAML会在app start上更改图像源

[英]WP7/ XAML change image source on app start if theme is light

my image is inside the datatemplate, what I want to do it is change the image if the wp theme is light 我的图像在datatemplate内部,我想要做的是在wp主题很轻的情况下更改图像

<DataTemplate x:Key="citiesItemTemplate">
            <StackPanel Grid.Column="1"  VerticalAlignment="Top">               
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition  />
                        <ColumnDefinition  />
                        <ColumnDefinition  />                                              
                    </Grid.ColumnDefinitions>    
                    <Grid.RowDefinitions>                   
                        <RowDefinition/>
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Column="0" Height="50" Tap="ProgLngGropus_Tap" Text="{Binding Name}" FontSize="26"  Margin="12,-5,12,6"/>                   
                    <ToggleButton Grid.Column="2" x:Name="MyToggleButton" Style="{StaticResource FlipButton}">
                        <ToggleButton.Content>
                            <Image Grid.Column="2" Margin="0,-10,-33,0" Height="40" Width="40" x:Name="ArrowDownImg"  Source="/Images/appbar.dark.arrow.down.circle.rest.png" />                            
                        </ToggleButton.Content>
                    </ToggleButton>
                    <TextBlock TextWrapping="Wrap" Text="{Binding Lang}" Grid.Column="0" Grid.Row="1" x:Name="Desc"
                       Foreground="Orange" Visibility="{Binding ElementName=MyToggleButton,
                        Path=IsChecked, Converter={StaticResource ValueConverterBoolToVis}}">                        
                    </TextBlock>

                </Grid>

            </StackPanel>
        </DataTemplate> 

But Im unable to access ArrowDownImg 但我无法访问ArrowDownImg

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


    if (darkBackgroundVisibility != Visibility.Visible)
    {
        //Error in finding ArrowDownImg
     ***//ArrowDownImg.Source = "/Images/appbar.light.arrow.down.circle.rest.png"***
    }

You could also do 你也可以这样做

Image arrowDownImg = MyObjectUsingTheDataTemplate.FindVisualChild("ArrowDownImg") as Image;
arrowDownImg.Source = ...

by using the following extension methods 通过使用以下扩展方法

    public static FrameworkElement FindVisualChild(this FrameworkElement root, string name)
    {
        FrameworkElement temp = root.FindName(name) as FrameworkElement;
        if (temp != null)
            return temp;

        foreach (FrameworkElement element in root.GetVisualDescendents())
        {
            temp = element.FindName(name) as FrameworkElement;
            if (temp != null)
                return temp;
        }

        return null;
    }

    public static IEnumerable<FrameworkElement> GetVisualDescendents(this FrameworkElement root)
    {
        Queue<IEnumerable<FrameworkElement>> toDo = new Queue<IEnumerable<FrameworkElement>>();

        toDo.Enqueue(root.GetVisualChildren());
        while (toDo.Count > 0)
        {
            IEnumerable<FrameworkElement> children = toDo.Dequeue();
            foreach (FrameworkElement child in children)
            {
                yield return child;
                toDo.Enqueue(child.GetVisualChildren());
            }
        }
    }

    public static IEnumerable<FrameworkElement> GetVisualChildren(this FrameworkElement root)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
        {
            yield return VisualTreeHelper.GetChild(root, i) as FrameworkElement;
        }
    }

Answer: Here is what I did I have added the loaded event 答:这就是我所做的,我添加了加载的事件

 <ToggleButton Grid.Row="0" Grid.Column="2" Margin="0,-10,-33,0" x:Name="MyToggleButton" Style="{StaticResource FlipButton}">
                        <ToggleButton.Content>
                            <Image Loaded="ArrowDownImg_Loaded" Height="50" Width="50" x:Name="ArrowDownImg"   />                               
                        </ToggleButton.Content>
                    </ToggleButton>

and in the event: 并在事件中:

  private void ArrowDownImg_Loaded(object sender, RoutedEventArgs e)
        {
            Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
            var ArrowDownImg = sender as Image;

            if (darkBackgroundVisibility != Visibility.Visible)
            {
                ArrowDownImg.Source = new BitmapImage(new Uri("/Images/appbar.arrow.down.circle.dark.rest.png", UriKind.Relative));
            }
            else
            {
                ArrowDownImg.Source = new BitmapImage(new Uri("/Images/appbar.arrow.down.circle.light.rest.png", UriKind.Relative));
            }
        }

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

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