在Xamarin.Forms中的方向更改上添加布局 [英] Add a Layout on Orientation change in Xamarin.Forms

查看:70
本文介绍了在Xamarin.Forms中的方向更改上添加布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现的是,在将屏幕从纵向更改为横向的现有页面上添加布局.我已经设法使用 void OnSizeAllocated(double width,double height)检测方向变化.但是我无法在此事件上添加布局.

What I need to achieve is, Adding a Layout to the existing page on Changing the screen from Portrait to Landscape. I have managed to detect orientation change using void OnSizeAllocated(double width, double height) . But I cannot add a layout on this event.

我的示例C#代码为

   public class MyLayoutView : ContentPage
   {
      StackLayout portraitcontent = null;
      StackLayout landscapecontent = null;
      StackLayout footer = null;

   public MyLayoutView ()
    {

    portraitcontent = new StackLayout ();
    landscapecontent = new StackLayout ();
    footer = new StackLayout ();

    this.Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0);
    this.BackgroundColor = Color.FromHex ("#ffffff");
    this.Content = new StackLayout
        {
            Children =
            {
                portraitcontent ,
                landscapecontent ,
                footer 
            }
            };
         }
   }

OnSizeAllocated方法

   protected override void OnSizeAllocated(double width, double height)
    {
        base.OnSizeAllocated(width, height);

        if (width < height) {
            // In Portrait
            portraitcontent .IsVisible = true;
            landscapecontent.IsVisible = false;
            Debug.WriteLine ("Application Is in portrait");
        } else {
            // In Landscape
            portraitcontent .IsVisible = false;
            landscapecontent.IsVisible = true;

            landscapecontent.Children.Clear ();

            Label LandscapeLabel= new Label
            {
              Text = "<Each Time Text Changes>",
              HorizontalOptions = LayoutOptions.StartAndExpand,
              TextColor=Xamarin.Forms.Color.FromHex("#000000")
            };

            landscapecontent.Children.Add(LandscapeLabel); 

            Debug.WriteLine ("Application Is in Landscape");
        }
    }

抛出阻止重新进入时无法修改集合错误.每次方向更改时,我都需要在stacklayout中添加一个新标签.如何以适当的方式实现它?

It throws cannot modify the collection while reenterancy is blocked error. I need to add a new label in stacklayout each time when orientation changes. How can I achieve it in a proper way??

预先感谢

推荐答案

打开方向更改时,将多次调用分配的大小.我认为您必须进行更多的验证,例如检查孩子的数量,以避免视图重复.

On Size allocated is invoked multiple times when orientation change. I think you have to do more validations like checking the number of chidren to avoid duplication of views.

    if(landscapecontent.Children.Count>1)
     { 
       //Don't add any views
     }

只要我尝试过,就没有用于旋转的事件处理程序.代码中的一种技巧是在方向更改时更改视图.

As long as I tried there is no event handler for rotation. Itz kind of hack in the code to change views when orientation changes.

即使您可以使用接受的答案在这里 ..希望它能达到您的目的..

Even though you can use the accepted answer here.. Hope it serves your purpose..

这篇关于在Xamarin.Forms中的方向更改上添加布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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