[英]How to handle screen rotation/orientation in Xamarin.Forms/XLabs?
[英]How to rotate screen manual as well automatic in Xamarin.forms
我需要將屏幕從縱向旋轉到橫向。 即使我沒有傾斜手機,我也應該留在橫向。 縱向和橫向具有不同的視圖,帶有用於更改方向的按鈕,並且位於我使用內容模板處理的同一內容頁面中。 因此,現在我想在傾斜手機或點擊任一視圖中的按鈕以更改方向時自動旋轉。
我想滿足這兩種情況。 如何在 Android 和 iOS ( Xamarin.Z6450242531912981C3683CAE88A2 ) 中實現這一點?
任何人都面臨這種用例,如果你有解決方案。 幫幫我。
使用 Xamarin.Forms 時,支持的控制設備方向的方法是使用每個單獨項目的設置。
您可以在依賴服務中調用以下方法。
創建接口:
public interface IOrientationService
{
void Landscape();
void Portrait();
}
Android:
public class OrientationService : IOrientationService
{
public void Landscape()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Landscape;
}
public void Portrait()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Portrait;
}
}
iOS:
public class OrientationService : IOrientationService
{
public void Landscape()
{
AppDelegate appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
appDelegate.allowRotation = true;
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));
}
public void Portrait()
{
AppDelegate appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
appDelegate.allowRotation = true;
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
}
}
依賴服務的使用可以參考下面的鏈接。 如何阻止旋轉以僅在 xamarin forms 中允許縱向模式?
- 我只想自動和手動旋轉我的應用程序中的一頁。
當您進行導航時,您可以在OnAppearing
事件中加載頁面之前進行定位。
protected override void OnAppearing()
{
base.OnAppearing();
DependencyService.Get<IOrientationService>().Landscape();
}
- 縱向和橫向視圖都不同。
Liek 1.,您可以使用OnAppearing()
事件在每個頁面中設置您想要的方向。
- 我不應該以任何原因鎖定特定頁面,但是當我想手動更改方向時,我也應該能夠做到這一點。 即使我沒有旋轉/傾斜手機。
如果您想手動執行此操作,請在單擊事件中調用Landscape
或Portrait
,或點擊或其他方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.