簡體   English   中英

如何在 Xamarin.forms 中手動和自動旋轉屏幕

[英]How to rotate screen manual as well automatic in Xamarin.forms

我需要將屏幕從縱向旋轉到橫向。 即使我沒有傾斜手機,我也應該留在橫向。 縱向和橫向具有不同的視圖,帶有用於更改方向的按鈕,並且位於我使用內容模板處理的同一內容頁面中。 因此,現在我想在傾斜手機或點擊任一視圖中的按鈕以更改方向時自動旋轉。

  1. 縱向 -> 使用按鈕更改橫向,然后我應該保持橫向視圖,直到發生一次旋轉。
  2. 例如:-現在,我使用傳感器來到橫向,如果我點擊按鈕進行旋轉,我就處於橫向模式,那么我應該改變縱向並應該呆在那里。

我想滿足這兩種情況。 如何在 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 中允許縱向模式?

  1. 我只想自動和手動旋轉我的應用程序中的一頁。

當您進行導航時,您可以在OnAppearing事件中加載頁面之前進行定位。

 protected override void OnAppearing()
    {
        base.OnAppearing();
        DependencyService.Get<IOrientationService>().Landscape();
    }
  1. 縱向和橫向視圖都不同。

Liek 1.,您可以使用OnAppearing()事件在每個頁面中設置您想要的方向。

  1. 我不應該以任何原因鎖定特定頁面,但是當我想手動更改方向時,我也應該能夠做到這一點。 即使我沒有旋轉/傾斜手機。

如果您想手動執行此操作,請在單擊事件中調用LandscapePortrait ,或點擊或其他方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM