在Windows Phone 7应用程序中,我想在用户单击按钮时对图像应用90º旋转。我正在做以下事情:
<Image Height="369" Name="imageView" Stretch="Uniform" Width="394">
<Image.RenderTransform>
<RotateTransform Angle="0" />
</Image.RenderTransform>
</Image>
在功能:
((RotateTransform)imageView.RenderTransform).Angle += 90;
到目前为止,这么好。将图像放在滚动查看器中时会出现问题。
<ScrollViewer ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" Height="389" HorizontalAlignment="Left" Margin="12,78,0,0" Name="scrollViewer" VerticalAlignment="Top" Width="409">
<Image Height="369" Name="imageView" Stretch="Uniform" Width="394">
<Image.RenderTransform>
<RotateTransform Angle="0" />
</Image.RenderTransform>
</Image>
</ScrollViewer>
在这种情况下,我收到一个错误 - InvalidCastException未处理 - 在这行代码中:
((RotateTransform)imageView.RenderTransform).Angle += 90;
答案 0 :(得分:1)
问题是imageView.RenderTransform不是RotateTransform,而是CompositeTransform,所以请尝试这样做:
((CompositeTransform)imageView.RenderTransform).Rotation += 90;
如果你想直接在XAML中将它指定为复合变换,你可以这样做:
<ScrollViewer ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" Height="389" HorizontalAlignment="Left" Margin="12,78,0,0" Name="scrollViewer" VerticalAlignment="Top" Width="409">
<StackPanel>
<Image Height="369" Name="imageView" Stretch="Uniform" Width="394" Source="/PhoneApp2;component/Images/lumia-920-rainbow.png">
<Image.RenderTransform>
<CompositeTransform Rotation="0" CenterX="197" CenterY="184" />
</Image.RenderTransform>
</Image>
<StackPanel>
</ScrollViewer>