Navigate to another page when orientation changed

微笑、不失礼 提交于 2020-01-21 12:35:08

问题


I will explain what I'm trying to do on following instance:

I have two pages - MainPage.xaml (orientation Portrait) and LandscapeLeft.xaml (orientation LandscapeLeft).

I want to navigate from MainPage.xaml to LandscapeLeft.xaml when user rotate phone on Lanscape position.

I've done as follows:

XAML:

   SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"

code behind:

   protected override void OnOrientationChanged(OrientationChangedEventArgs e)
   {
       switch (e.Orientation)
       {
           case PageOrientation.LandscapeLeft:
               NavigationService.Navigate(new Uri("/LandscapeLeft.xaml", UriKind.RelativeOrAbsolute));
               break;
       }
       base.OnOrientationChanged(e);
   }

When I rotate phone from PortraitUp to LandscapeLeft position that what happens:
Firstly, content of MainPage.xaml rotates landscape and just then LandscapeLeft.xaml loads.

What I want to do is to eliminate process of content rotation of MainPage.xaml. It doesn't look good and affects performance. Simply, when I rotate the phone I want LandscapeLeft.xaml to be loaded without previous change of content orientation of MainPage.xaml.

Please, any suggestions?


回答1:


How about putting your contents in a frame. When orientation event fires, change the frame contents instead of navigating to a different page.

 ...
         case PageOrientation.LandscapeLeft:
         FrmContents= new LandscapeLeft();
 ...  

This should sort out your performance issues.




回答2:


Just put all the functionality on one page and then alter what is shown based on the orientation.




回答3:


From a "user" perspective, I don't think you should use Navigation to achieve this effect.

Navigation is really closely tied to the Back button - so it's not something a user normally expects to also be tied to the device orientation as well.

Things you could probably do are:

  • consider hosting both the portrait and the landscape controls in the mainpage.xaml - just change the "visibility" of the 2 controls depending on the orientation.
  • if possible consider using the same controls for both orientation - but with appropriate layout changes for the 2 orientations.

I've written apps that support both orientations - they seem to perform really well - what performance issues are you seeing?



来源:https://stackoverflow.com/questions/5116687/navigate-to-another-page-when-orientation-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!