我正在开发Windows Phone 8/8.1 C#/XAML .NET 4.5 应用程序,我想知道如何更改页面上一个控件/项目的方向(将其旋转 90 度)。
我的纵向页面上有一个 webBrowser(保持锁定在该方向),并且 webbrowser 需要处于横向(并且不旋转)。
如何将网络浏览器设置为旋转 90 度并保持这种状态?
我正在开发Windows Phone 8/8.1 C#/XAML .NET 4.5 应用程序,我想知道如何更改页面上一个控件/项目的方向(将其旋转 90 度)。
我的纵向页面上有一个 webBrowser(保持锁定在该方向),并且 webbrowser 需要处于横向(并且不旋转)。
如何将网络浏览器设置为旋转 90 度并保持这种状态?
所以,我想出了一种方法来自己做。
我将把答案放在社区 wiki 中,以便以后来的任何人都可以编辑并添加更多选项来执行此操作。
选项之一是旋转元素。
它可以在后面的代码中完成:
//Create a transformation
RotateTransform rt = new RotateTransform();
//and set the rotation angle
rt.Angle = 90; //number of degrees to rotate clockwise
//for counterclockwise rotation use negative number
//default rotation is around top left corner of the control,
//but you sometimes want to rotate around the center of the control
//to do that, you need to set the RenderTransFormOrigin
//of the item you're going to rotate
//I did not test this approach, maybe You're going to need to use actual coordinates
//so this bit is for information purposes only
controlToRotate.RenderTransformOrigin = new Point(0.5,0.5);
//the name of the control is controlToRotate in this instance
controlToRotate.RenderTransform = rt;
或在 XAML 中:
本例中的浏览器取自我自己的代码,所有内容都已设置好,以便项目旋转并占据分配给它的全部位置
浏览器在网格中,动态定位,网格被命名,以便我可以简单地访问它
浏览器需要指定宽度和高度,在这种情况下,我从网格的宽度和高度中获取它,否则它会以某种方式自动设置并且结果以某种方式非常小
垂直和水平对齐设置为居中,以便生成的旋转也居中,否则不是(在动态布局中)
代码:
<Grid x:Name="ContentPanel">
<phone:WebBrowser x:Name="controlToRotate"
VerticalAlignment="Center"
HorizontalAlignment="Center"
RenderTransformOrigin=".5, .5"
Width="{Binding ElementName=ContentPanel, Path=ActualHeight}"
Height="{Binding ElementName=ContentPanel, Path=ActualWidth}">
<phone:WebBrowser.RenderTransform>
<RotateTransform Angle="90"/>
</phone:WebBrowser.RenderTransform>
</phone:WebBrowser>
</Grid>