Xamarin表示应用程序纵向页面从横向返回时的宽度更大

时间:2017-03-30 11:12:33

标签: android xamarin xamarin.android xamarin.forms

我正在使用Visual Studio 2015中的Xamarin Forms开发我的第一个应用程序。

我有一个带有网格的页面,它位于StackLayout中,位于ScrollView中,位于AbsoluteLayout中。

以下是该页面的构造函数:

public ConditionsPage(long reportId)
        {
            InitializeComponent();

            Title = "New Entry";

            this.ReportId = reportId;

            var columns =
                DependencyService.Get<IGetReports.IGetReportsService>().GetAllVisibleColumns(ReportId);

            Rows = new List<RowEntry>();

            foreach (var c in columns)
            {

                Label label = new Label
                {
                    Text = c.ColumnName,
                    FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
                    HorizontalOptions = LayoutOptions.Center,
                    VerticalOptions = LayoutOptions.Center,
                    TextColor = Color.Black
                };

                Picker picker = new Picker
                {
                    Title = "Condition",
                    HorizontalOptions = LayoutOptions.Center,
                    VerticalOptions = LayoutOptions.Center,
                    TextColor = Color.Black
                };

                foreach (string condition in conditionOperators.Values)
                {
                    picker.Items.Add(condition);
                }

                // Create Entry for condition value input
                Entry rowEntry = new Entry()
                {
                    WidthRequest = 100,
                    HeightRequest = 50,
                    HorizontalOptions = LayoutOptions.Center,
                    VerticalOptions = LayoutOptions.Center,
                    TextColor = Color.Black,
                };
                if (c.ColumnType == ReportColumnType.Datetime)
                {
                    rowEntry.Keyboard = Keyboard.Text;
                }
                else if (c.ColumnType == ReportColumnType.Text)
                {
                    rowEntry.Keyboard = Keyboard.Text;
                }
                else
                {
                    rowEntry.Keyboard = Keyboard.Numeric;
                }


                Rows.Add(new RowEntry() {ColumnId = c.Id, Entry = rowEntry, Label = label, Picker = picker });
            }

            // Build the page.
            var absoluteLayout = new AbsoluteLayout();
            var stackLayout = new StackLayout();
            var scrollView = new ScrollView();

            var grid = new Grid()
            {
                VerticalOptions = LayoutOptions.Fill,
                HorizontalOptions = LayoutOptions.Fill,
                RowDefinitions =
                {
                    new RowDefinition { Height = GridLength.Auto },
                    new RowDefinition { Height = GridLength.Auto },
                    new RowDefinition { Height = new GridLength(1, GridUnitType.Star) },
                    new RowDefinition { Height = new GridLength(100, GridUnitType.Absolute) }
                },
                ColumnDefinitions =
                {
                    new ColumnDefinition { Width = GridLength.Auto },
                    new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                    new ColumnDefinition { Width = new GridLength(100, GridUnitType.Absolute) }
                }
            };

            int i = 0;
            foreach (var row in Rows)
            {
                grid.Children.Add(row.Label, 0, i);
                grid.Children.Add(row.Picker, 1, i);
                grid.Children.Add(row.Entry, 2, i);
                i++;
            }

            stackLayout.Children.Add(grid);
            var generateReportButton = new Button()
            {
                Text = "Generate Report"
            };
            generateReportButton.Clicked += new EventHandler(GenerateReport_OnClicked);
            stackLayout.Children.Add(generateReportButton);

            stackLayout.HorizontalOptions = LayoutOptions.Fill;
            stackLayout.VerticalOptions = LayoutOptions.Fill;

            scrollView.HorizontalOptions = LayoutOptions.Fill;
            scrollView.VerticalOptions = LayoutOptions.Fill;

            scrollView.Content = stackLayout;

            absoluteLayout.Layout(new Rectangle(0,0, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));

            AbsoluteLayout.SetLayoutFlags(scrollView, AbsoluteLayoutFlags.PositionProportional);

            absoluteLayout.Children.Add(scrollView);



            absoluteLayout.HorizontalOptions = LayoutOptions.Fill;
            absoluteLayout.VerticalOptions = LayoutOptions.Fill;

            Content = absoluteLayout;
        }

我有一个应用程序在“生成”之后转到的另一个页面。单击按钮:

Navigation.PushAsync(new ReportPage(ReportId, Rows));

当我按下报告页面上的后退按钮时,我看到纵向中的条件页面,但宽度远大于设备屏幕,我只能看到前两列/第三列不可见/。 / p>

我在条件活动中有一个属性如下:

 [Activity(Label = "Report Conditions", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
    ScreenOrientation = ScreenOrientation.Portrait)]

以及报告活动:

[Activity(Label = "Report", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
    ScreenOrientation = ScreenOrientation.Landscape)]

我也添加了自定义页面渲染器,如下所示:

对于条件活动:

 public class ConditionsCustomContentPageRenderer : Xamarin.Forms.Platform.Android.PageRenderer
    {
        //private ScreenOrientation _previousOrientation = ScreenOrientation.Unspecified;

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

            var activity = (Activity)Context;

            if (visibility == ViewStates.Gone)
            {
                // Revert to previous orientation
                activity.RequestedOrientation = ScreenOrientation.Portrait;
                //_previousOrientation == ScreenOrientation.Unspecified ? ScreenOrientation.Portrait : _previousOrientation;
            }
            else if (visibility == ViewStates.Visible)
            {
                //if (_previousOrientation == ScreenOrientation.Unspecified)
                //{
                //    _previousOrientation = activity.RequestedOrientation;
                //}

                activity.RequestedOrientation = ScreenOrientation.Portrait;
            }
        }
    }

报告活动:

public class ReportCustomContentPageRenderer : Xamarin.Forms.Platform.Android.PageRenderer
    {
        private ScreenOrientation _previousOrientation = ScreenOrientation.Unspecified;

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

            var activity = (Activity)Context;

            if (visibility == ViewStates.Gone)
            {
                // Revert to previous orientation
                activity.RequestedOrientation = _previousOrientation == ScreenOrientation.Unspecified ? ScreenOrientation.Portrait : _previousOrientation;
            }
            else if (visibility == ViewStates.Visible)
            {
                if (_previousOrientation == ScreenOrientation.Unspecified)
                {
                    _previousOrientation = activity.RequestedOrientation;
                }

                activity.RequestedOrientation = ScreenOrientation.Landscape;
            }
        }
    }

我遗失了一些东西,但我不确定到底是什么。

非常感谢任何帮助!

0 个答案:

没有答案