如何在Xamarin Forms中处理屏幕旋转/方向? [英] How to handle screen rotation/orientation in Xamarin Forms?

查看:683
本文介绍了如何在Xamarin Forms中处理屏幕旋转/方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xamarin Forms中是否有(半个)通用方法来处理不同视图和平台(Android,iOS,WinPhone)的旋转/方向?

Is there a (half) generic way to handle rotation/orientation in Xamarin Forms for different views and platforms (Android, iOS, WinPhone)?

UI确实会旋转,这已经足够好了,尽管它对我的布局(现在是绝对布局)造成了严重破坏.我想使用Stacklayout可以使它变得更灵活,但是当布局更加复杂时,它将在其他地方遇到障碍.

The UI does rotate, and that is nice enough, though it wreaks havoc to my layout (absolute layout right now). I suppose with a Stacklayout I could make it a litte more flexible, but would then hit a road block somewhere else when the layout is more intricate.

我可以使用相同的ViewModel以某种方式显示纵向和横向的不同视图吗? (我正在使用基于XLAB的MVVM.)

Can I somehow display different views for portrait and landscape, with the same ViewModel? (I am using XLABs based MVVM.)

我发现了可能的解决方案:

Possible solutions I have found:

http://blog.rthand.com/post/2014/07/24/Different-XAML-layouts-for-different-device-orienations-in-XamarinForms.aspx is lacking iOS and I wonder if it will handle MVVM too well, seems good though and I am investigating it right now

http://www.jimbobbennett.io/orientation-with-xamarin-forms/听起来很有希望,但是示例仅是iOS,链接的GIT存储库根本没有任何文档,只是说"Xamarin Helpers"

http://www.jimbobbennett.io/orientation-with-xamarin-forms/ sounds promising but the sample is iOS only, the linked GIT repository has no documentation at all, it just says "Xamarin Helpers"

http://www.sellsbrothers.com/posts/Details/13740 也可以通过编程方式创建视图.尽管在测试中,旋转时我没有得到ios模拟器的 size change 事件(尽管我在不同的代码位置听过). (该解决方案基于更改大小来检测旋转.)

http://www.sellsbrothers.com/posts/Details/13740 might also be a possibility for programmatically created views. Though in my tests I did not get a size changed event (though I listened at a different code place) for ios simulator when rotating. (The solution is based on size changed to detect rotation.)

推荐答案

如果您已经在使用XLabs,则可以使用IXFormsApp和属性"Orientation"以及事件处理程序"Rotation".您将必须在每个平台上添加本机观察者,并在此处设置IXFormsApp的方向",这将导致事件处理程序被调用.

If you are already using XLabs then you could use IXFormsApp and property 'Orientation' and event handler 'Rotation'. You would have to add the native observers per platform and set IXFormsApp's 'Orientation' there which would cause the event handler to invoke.

例如在iOS上:

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        var xapp = new XFormsAppiOS();
        xapp.Init(this);

        var resolverContainer = new SimpleContainer();
        resolverContainer.Register<IXFormsApp>(xapp);
        Resolver.SetResolver(resolverContainer.GetResolver());

        var a = new App();
        LoadApplication(a);

        UIDevice.Notifications.ObserveOrientationDidChange((s, e) =>
        {
            xapp.Orientation = ... // set orientation here
        });

现在您可以通过解决IXFormsApp来监视方向更改:

Now you can monitor orientation changes by resolving IXFormsApp:

        xapp = Resolver.Resolve<IXFormsApp>();
        if (xapp.Orientation == Orientation.Landscape) { ... } else { ... } 
        xapp.Rotation += (sender, args) =>
        {
            switch (args.Value)
            {
                case Orientation.LandscapeLeft:
                    break;
                default:
                    // etc.
                    break;
            }
        };

对于布局,我可以想象RelativeLayout是最方便的选择,因为您可以将方向放置在Constraint的内部.旋转时,使布局自动刷新.

As for layouts I would imagine RelativeLayout would be the most convenient choice as you could put the orientation inside the Constraint's. On rotation make the layout refresh itself.

这篇关于如何在Xamarin Forms中处理屏幕旋转/方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆