簡體   English   中英

Xamarin表單中的ContentPage輪換問題

[英]Problem with ContentPage Rotation in Xamarin Forms

我正在為我的新項目開發一些功能,我有一個內容頁面,我需要始終在Portrait中顯示。 我已經通過實現自定義渲染器並覆蓋Android的OnWindowVisibilityChanged來實現它,(此時我還沒有進入iOS)。 問題是,當我從Portrait one進入新頁面時,下一個必須支持兩個方向,但它只顯示在Portrait中。

我想有一種方法可以實現這一點,而無需為每個頁面實現自定義渲染器,我需要在具有固定方向的頁面之后顯示。

有什么建議么?

這是我對OnWindowVisibilityChanged重寫實現

protected override void OnWindowVisibilityChanged([GeneratedEnum] ViewStates visibility)
{
    base.OnWindowVisibilityChanged(visibility);

    var activity = (Activity)Context;

    if (visibility.Equals(ViewStates.Gone))
    {
        // go back to previous orientation
        activity.RequestedOrientation = _previousOrientation;
    }
    else if (visibility.Equals(ViewStates.Visible))
    {
        if (_previousOrientation.Equals(ScreenOrientation.Sensor))
        {
            _previousOrientation = activity.RequestedOrientation;
        }

        activity.RequestedOrientation = ScreenOrientation.Portrait;
    }
}

這是一個名為Xamarin.Plugin.DeviceOrientation的插件,你可以使用它,這將很容易解決你的問題。

例如,看看這個開關條件

switch (orientation)
        {
            case DeviceOrientations.Free:
                CrossDeviceOrientation.Current.UnlockOrientation();
                break;
            case DeviceOrientations.Landscape:
                CrossDeviceOrientation.Current.LockOrientation(Plugin.DeviceOrientation.Abstractions.DeviceOrientations.Landscape);
                break;
            case DeviceOrientations.Portrait:
                CrossDeviceOrientation.Current.LockOrientation(Plugin.DeviceOrientation.Abstractions.DeviceOrientations.Portrait);
                break;
            default:
                break;
        }

只有一頁你可以在onApperaing()上調用它, CrossDeviceOrientation.Current.LockOrientation(Plugin.DeviceOrientation.Abstractions.DeviceOrientations.Portrait);

並再次釋放方向調用onDiappearing()

CrossDeviceOrientation.Current.UnlockOrientation();

有關更多信息以及如何使用,請訪問此鏈接, https://github.com/wcoder/Xamarin.Plugin.DeviceOrientation

暫無
暫無

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

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