Esempio n. 1
0
        /// <summary>
        /// Creates all the views we'll ever need. DOES NOT CONFIGURE the views at all.
        /// </summary>
        private void CreateViews()
        {
            if (ViewModelCreator == null || ViewCreator == null || WallSizer == null)
            {
                Monitor.Throw($"Cannot Initialize, ViewModelCreator={ViewModelCreator}, ViewCreator={ViewCreator}, WallSizer={WallSizer}");
                return;
            }

            var size            = CrossScreen.Current.Size;
            var maxScreenWidth  = size.Width;
            var maxScreenHeight = size.Height;
            var normalWallSize  = WallSizer.Size(maxScreenWidth, maxScreenHeight);
            var rotatedWallSize = WallSizer.Size(maxScreenHeight, maxScreenWidth);
            var maxNumViews     = Math.Max(normalWallSize.MaxNumItems, rotatedWallSize.MaxNumItems);

            Monitor.Debug($"Guessing Max Views for {size.Width}x{size.Height}");
            Monitor.Debug($"Landscape. Table = {rotatedWallSize.NumRows}x{rotatedWallSize.NumColumns}, Padding = {rotatedWallSize.PaddingX}x{rotatedWallSize.PaddingY}, ItemSize = {rotatedWallSize.ItemSize.Width}x{rotatedWallSize.ItemSize.Height}");

            // setup our initial data & views.
            for (var i = 0; i < maxNumViews; i++)
            {
                var view = ViewCreator();
                view.BindingContext = null;
                AbsoluteLayout.Children.Add(view);
            }
            numVisibleViews = maxNumViews;
            WidthRequest    = size.Width;
            HeightRequest   = size.Height;
        }
Esempio n. 2
0
        private bool SetupViewSizesAndVisibility(double screenWidth, double screenHeight)
        {
            var sizeChanged = screenWidth != lastWidth || screenHeight != lastHeight;

            if (sizeChanged)
            {
                // account for the new view size
                var newSize = WallSizer.Size(screenWidth, screenHeight);

                // go through and layout and make all views that we need visible
                double x = newSize.PaddingX, y = newSize.PaddingY;
                var    childIndex = 0;
                for (var row = 0; row < newSize.NumRows; row++)
                {
                    for (var column = 0; column < newSize.NumColumns; column++)
                    {
                        var view = AbsoluteLayout.Children[childIndex++];
                        Xamarin.Forms.AbsoluteLayout.SetLayoutBounds(view, new Rectangle(x, y, newSize.ItemSize.Width, newSize.ItemSize.Height));
                        //this might speed things up, not sure, hard to tell with Xamarin and whatever C# optimizations do here
                        if (!view.IsVisible)
                        {
                            view.IsVisible = true;
                            numVisibleViews++;
                        }

                        x += newSize.ItemSizeWithPadding.Width;
                    }
                    x  = newSize.PaddingX;
                    y += newSize.ItemSizeWithPadding.Height;
                }

                // If we can't fit a view on the screen, make it invisible
                for (; childIndex < AbsoluteLayout.Children.Count; childIndex++)
                {
                    var child = AbsoluteLayout.Children[childIndex];
                    //this might speed things up, not sure, hard to tell with Xamarin and whatever C# optimizations do here
                    if (child.IsVisible)
                    {
                        child.IsVisible = false;
                        numVisibleViews--;
                    }
                }

                lastWidth     = screenWidth;
                lastHeight    = screenHeight;
                WidthRequest  = screenWidth;
                HeightRequest = screenHeight;
                Monitor.Debug($"Size Allocated: {screenWidth}x{screenHeight}, Grid = {newSize.NumRows}x{newSize.NumColumns}, Paddings = {newSize.PaddingX}x{newSize.PaddingY}, ItemSize = {newSize.ItemSize.Width}x{newSize.ItemSize.Height}");
            }
            return(sizeChanged);
        }