在Windows Phone上旋转样式图像

时间:2012-03-05 18:24:04

标签: windows-phone-7 windows-phone

我在How to rotate an image to a particular angle in Windows Phone 7 Silverlight Application?找到的答案接近我正在寻找的答案。

我的问题是 - 如果图像是样式的一部分,我该怎么做?图像基本上是指向运动方向(轨道)的箭头。

<Style x:Key="MyBoatPushPinStyle" TargetType="maps:Pushpin">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Image x:Name="MyBoatIcon" 
                       Source="Resources/Icons/myboat.png" 
                       Stretch="None">
                    <Image.RenderTransform>
                        <RotateTransform Angle="0" />
                    </Image.RenderTransform>
                </Image>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

该样式应用于MapLayer:

<maps:MapLayer x:Name="LocationLayer">
    <maps:Pushpin Style="{StaticResource MyBoatPushPinStyle}"  
    Location="{Binding CurrentLocation}" />
</maps:MapLayer>

我无法想象的是如何在风格中引用图像,如果可以实现的话。

类似的东西:

((RotateTransform)REFERENCE_TO_IMAGE.RenderTransform).Angle = _currentTrack;

1 个答案:

答案 0 :(得分:0)

试试这个:

<maps:MapLayer x:Name="LocationLayer">
    <maps:Pushpin x:Name="PushpinLayer" Style="{StaticResource MyBoatPushPinStyle}" Location="{Binding CurrentLocation}" />
</maps:MapLayer>

Image a = FindFirstElementInVisualTree<Image>(PushpinLayer);
if (a != null)
    ((RotateTransform)a.RenderTransform).Angle = 90;

private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
    var count = VisualTreeHelper.GetChildrenCount(parentElement);
    if (count == 0)
        return null;

    for (int i = 0; i < count; i++)
    {
        var child = VisualTreeHelper.GetChild(parentElement, i);

        if (child != null && child is T)
            return (T)child;

        else
        {
            var result = FindFirstElementInVisualTree<T>(child);
            if (result != null)
                return result;

        }
    }
        return null;
}