How to handle screen rotation/orientation in Xamarin Forms?

佐手、 提交于 2019-12-11 01:51:39

问题


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

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.

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/ 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 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.)


回答1:


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.

For example on 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
        });

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;
            }
        };

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.



来源:https://stackoverflow.com/questions/29308916/how-to-handle-screen-rotation-orientation-in-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!