C# Windows Phone 8页面定向
当页面已加载或正在加载时,如何检测方向?我实现了C# Windows Phone 8页面定向,c#,windows-phone-8,C#,Windows Phone 8,当页面已加载或正在加载时,如何检测方向?我实现了OrientationChanged方法。但是当我将第一个页面设置为横向时,第二个页面不会触发此方法。页面处于横向模式,但UI未处于横向模式。我的意思是页面方向是可以的,但是musntOrientationChanged会被触发吗?在此方法中,我更改UI对象的外观。如果未触发,UI将以纵向模式显示 private void PhoneApplicationPage_OrientationChanged(object sender, Orientat
OrientationChanged
方法。但是当我将第一个页面设置为横向时,第二个页面不会触发此方法。页面处于横向模式,但UI未处于横向模式。我的意思是页面方向是可以的,但是musntOrientationChanged
会被触发吗?在此方法中,我更改UI对象的外观。如果未触发,UI将以纵向模式显示
private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
if (e.Orientation == PageOrientation.Landscape || e.Orientation == PageOrientation.LandscapeLeft || e.Orientation == PageOrientation.LandscapeRight)
{
SwitchPanel.Margin = new Thickness(12, 100, 250, 0);
StatusPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
}
else
{
SwitchPanel.Margin = new Thickness(12, 100, 12, 0);
StatusPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
}
}
如何解决此问题?只需将您的代码放在一个不同的方法中,并从
OrientationChanged
和Loaded
事件调用此方法:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
this.SetOrientation(this.Orientation);
}
private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
this.SetOrientation(e.Orientation);
}
private void SetOrientation(PageOrientation orientation)
{
if (orientation == PageOrientation.Landscape || orientation == PageOrientation.LandscapeLeft || orientation == PageOrientation.LandscapeRight)
{
SwitchPanel.Margin = new Thickness(12, 100, 250, 0);
StatusPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
}
else
{
SwitchPanel.Margin = new Thickness(12, 100, 12, 0);
StatusPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
}
}
只需将代码放在一个不同的方法中,并从
OrientationChanged
和Loaded
事件调用此方法:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
this.SetOrientation(this.Orientation);
}
private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
this.SetOrientation(e.Orientation);
}
private void SetOrientation(PageOrientation orientation)
{
if (orientation == PageOrientation.Landscape || orientation == PageOrientation.LandscapeLeft || orientation == PageOrientation.LandscapeRight)
{
SwitchPanel.Margin = new Thickness(12, 100, 250, 0);
StatusPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
}
else
{
SwitchPanel.Margin = new Thickness(12, 100, 12, 0);
StatusPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
}
}
您是否将事件关联到事件处理程序?在构造函数中应该有类似于以下内容的内容
this.OrientationChanged += PhoneApplicationPage_OrientationChanged;
您是否将事件关联到事件处理程序?在构造函数中应该有类似于以下内容的内容
this.OrientationChanged += PhoneApplicationPage_OrientationChanged;
这是一个非常糟糕的解决方法。关于这件事,必须给我们一些建议。不管怎么说,我已经输入了代码并开始工作了。由于一些ui延迟,我删除了您提供的
PhoneApplicationPage\u加载的代码行。我把它放到了页面构造函数中。这是一个非常糟糕的解决方法。关于这件事,必须给我们一些建议。不管怎么说,我已经输入了代码并开始工作了。由于一些ui延迟,我删除了您提供的PhoneApplicationPage\u加载的代码行。我把它放在页面构造函数中。没有理由把这一行添加到构造函数中。因为我在xaml中声明了OrientationChanged
事件。当您尝试双击“属性”窗口中的事件列表项以实现其中任何一项时,VisualStudio会自动创建它。这在您提供的代码中并不明显。仅供参考,此代码确实存在。。。但是您看不到它,因为它位于通常隐藏的部分类(包含所有生成的代码)中,该类与您实际编写的部分类相关联。除非你真的去寻找它,否则你不会知道它在那里。所以你对我的问题的回答实际上是“是的”。)@KooKiz的答案应该是正确的。当然我知道编译器为我创建了这一行。但我只是说,对于我实际的MainPage.cs文件:)不是编译的MainPage.cs,没有理由将这一行添加到构造函数中。因为我在xaml中声明了OrientationChanged
事件。当您尝试双击“属性”窗口中的事件列表项以实现其中任何一项时,VisualStudio会自动创建它。这在您提供的代码中并不明显。仅供参考,此代码确实存在。。。但是您看不到它,因为它位于通常隐藏的部分类(包含所有生成的代码)中,该类与您实际编写的部分类相关联。除非你真的去寻找它,否则你不会知道它在那里。所以你对我的问题的回答实际上是“是的”。)@KooKiz的答案应该是正确的。当然我知道编译器为我创建了这一行。但我只是说,对于我实际的MainPage.cs文件:)不是编译的文件