簡體   English   中英

Xamarin Forms 在 iOS 上強制頁面特定方向

[英]Xamarin Forms Forcing Page Specific Orientation on iOS

我的 XF 應用程序中有幾個頁面需要縱向或橫向,具體取決於它顯示的內容。 在 Android 上,這沒有問題,在 iOS 上也從來沒有問題,直到 iOS 16 發布。

iOS 16 顯然刪除了使用 UIDevice UIInterfaceOrientation 方法的能力,該方法位於我的 AppDelegate 中,如下所示。

MessagingCenter.Subscribe<MainPage>(this, "SetLandscapeModeOff", sender =>
            {
                UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
            });

這工作得很好,我可以簡單地在我試圖以特定方向加載的頁面上的 OnAppearing 方法中調用下面的代碼。

MessagingCenter.Send(this, "SetLandscapeModeOff");

我在這里看到 1 或 2 篇帖子談論新方法(還有很多關於 iOS 16 方法之前的內容)但是沒有一個完整到足以讓我的技能水平的人理解如何實現它們。 除了上面發布的內容外,我沒有任何起點。

編輯

我根據回復嘗試了以下解決方案。

界面:

using System;
using System.Collections.Generic;
using System.Text;

namespace MyApp
{
    public interface InterfaceOrientationService
    {
        void SetLandscape();
        void SetPortrait();
    }
}

AppDelegate.cs

[assembly: Xamarin.Forms.Dependency(typeof(MyApp.iOS.InterfaceOrientationServiceiOS))]

namespace MyApp.iOS
{
    public class InterfaceOrientationServiceiOS : InterfaceOrientationService
    {
        public InterfaceOrientationServiceiOS() { }

        public void SetLandscape()
        {
            if (UIDevice.CurrentDevice.CheckSystemVersion(16, 0))
            {
                var windowScene = (UIApplication.SharedApplication.ConnectedScenes.ToArray()[0] as UIWindowScene);

                if (windowScene != null)
                {
                    var nav = UIApplication.SharedApplication.KeyWindow?.RootViewController;
                    if (nav != null)
                    {
                        nav.SetNeedsUpdateOfSupportedInterfaceOrientations();
                        windowScene.RequestGeometryUpdate(
                            new UIWindowSceneGeometryPreferencesIOS(UIInterfaceOrientationMask.Portrait),
                            error => { }
                        );
                    }
                }
            }
            else
            {
                UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
            }
        }
        public void SetPortrait()
        {

            if (UIDevice.CurrentDevice.CheckSystemVersion(16, 0))
            {
                var windowScene = (UIApplication.SharedApplication.ConnectedScenes.ToArray()[0] as UIWindowScene);
                if (windowScene != null)
                {
                    var nav = UIApplication.SharedApplication.KeyWindow?.RootViewController;
                    if (nav != null)
                    {
                        nav.SetNeedsUpdateOfSupportedInterfaceOrientations();
                        windowScene.RequestGeometryUpdate(
                            new UIWindowSceneGeometryPreferencesIOS(UIInterfaceOrientationMask.Portrait),
                            error => { }
                        );
                    }
                }
            }
            else
            {
                UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
            }
        }
    }

    [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            LoadApplication(new App());
            return base.FinishedLaunching(app, options);
        }
    }
}

我在需要橫向的頁面上的 OnAppearing 方法:

protected override void OnAppearing()
        {
            base.OnAppearing();
            DependencyService.Get <InterfaceOrientationService>().SetLandscape();
        }

iOS16改變了頁面的改變方式,可以使用DependencyService來實現,參考如下代碼:

在Forms創建接口:

public interface InterfaceOrientationService    {        
void SetLandscape();        
void SetPortrait();    
}

iOS 項目:

using Foundation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using UIKit;
using Xamarin.Forms;
using XamarinFormsOne;
using XamarinFormsOne.iOS;

[assembly: Xamarin.Forms.Dependency(typeof(InterfaceOrientationServiceiOS))]   
namespace XamarinFormsOne.iOS    
{
    public class InterfaceOrientationServiceiOS: InterfaceOrientationService 
    {
        public InterfaceOrientationServiceiOS() { }

        public void SetLandscape() {
            if (UIDevice.CurrentDevice.CheckSystemVersion(16, 0))    
            {    
                var windowScene = (UIApplication.SharedApplication.ConnectedScenes.ToArray()[0] as UIWindowScene);

                if (windowScene != null)    
                {    
                    var nav = UIApplication.SharedApplication.KeyWindow?.RootViewController;   
                    if (nav != null)  
                    {
                   nav.SetNeedsUpdateOfSupportedInterfaceOrientations();   
                        windowScene.RequestGeometryUpdate( 
                            new UIWindowSceneGeometryPreferencesIOS(UIInterfaceOrientationMask.Portrait),
                            error => { }    
                        );
                    }
               }
            }
            else
            {
                UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
            }
        }
            public void SetPortrait() {
    
                if (UIDevice.CurrentDevice.CheckSystemVersion(16, 0))
                {
                    var windowScene = (UIApplication.SharedApplication.ConnectedScenes.ToArray()[0] as UIWindowScene);
                    if (windowScene != null)
                    {
                        var nav = UIApplication.SharedApplication.KeyWindow?.RootViewController;
                        if (nav != null)
                        {
                       nav.SetNeedsUpdateOfSupportedInterfaceOrientations();
                            windowScene.RequestGeometryUpdate(
                                new UIWindowSceneGeometryPreferencesIOS(UIInterfaceOrientationMask.Portrait),
                                error => { }
                            );
                        }
                    }
                }
                else
                {
                    UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
                }
            }
        }
    }

調用Xamrin.Forms中的接口:

protected override void OnAppearing()        
{            
base.OnAppearing();            
//SetLandscape            
DependencyService.Get<InterfaceOrientationService().SetLandscape();            
//SetPortrait            
DependencyService.Get<InterfaceOrientationService>().SetPortrait();        
}

暫無
暫無

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

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