【问题标题】:Xamarin maximum grid row/ column definitionsXamarin 最大网格行/列定义
【发布时间】:2021-01-22 22:11:29
【问题描述】:

我最近才开始玩 xamarin,但我正在使用 xamarin 制作 roguelike,并且我想到了为玩家地图使用网格(网格中的每个 XY 位置将代表随机生成的地图)我'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''ve've''ve hit a sn's 将任何东西放在第 55 列上似乎会将它们从屏幕上推开(见下图)

到目前为止,这是我的代码:

StackLayout stackLayout = new StackLayout() { VerticalOptions = LayoutOptions.FillAndExpand };
           Grid grid = new Grid
           {
               VerticalOptions = LayoutOptions.FillAndExpand,
               HorizontalOptions = LayoutOptions.FillAndExpand,
           };
           stackLayout.Children.Add(grid);

           for (int i = 0; i < 300; i++)
           {
               grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
               grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
           }
           

           // Row 0
           // The BoxView and Label are in row 0 and column 0, and so only needs to be added to the
           // Grid.Children collection to get default row and column settings.
           grid.Children.Add(new BoxView
           {
               Color = Color.Green
           });
           grid.Children.Add(new Label
           {
               Text = "Row 0, Column 0",
               HorizontalOptions = LayoutOptions.Center,
               VerticalOptions = LayoutOptions.Center
           });

           // This BoxView and Label are in row 0 and column 1, which are specified as arguments
           // to the Add method.
           grid.Children.Add(new BoxView
           {
               Color = Color.Blue
           }, 55, 0);
           grid.Children.Add(new Label
           {
               Text = "Row 0, Column 1",
               HorizontalOptions = LayoutOptions.Center,
               VerticalOptions = LayoutOptions.Center
           }, 1, 0);

           // Row 1
           // This BoxView and Label are in row 1 and column 0, which are specified as arguments
           // to the Add method overload.
           grid.Children.Add(new BoxView
           {
               Color = Color.Teal
           }, 0, 1, 1, 2);
           grid.Children.Add(new Label
           {
               Text = "Row 1, Column 0",
               HorizontalOptions = LayoutOptions.Center,
               VerticalOptions = LayoutOptions.Center
           }, 0, 1, 1, 2); // These arguments indicate that that the child element goes in the column starting at 0 but ending before 1.
                           // They also indicate that the child element goes in the row starting at 1 but ending before 2.

           grid.Children.Add(new BoxView
           {
               Color = Color.Purple
           }, 1, 2, 1, 2);
           grid.Children.Add(new Label
           {
               Text = "Row1, Column 1",
               HorizontalOptions = LayoutOptions.Center,
               VerticalOptions = LayoutOptions.Center
           }, 1, 2, 1, 2);

           // Row 2
           // Alternatively, the BoxView and Label can be positioned in cells with the Grid.SetRow
           // and Grid.SetColumn methods.
           BoxView boxView = new BoxView { Color = Color.Red };
           Grid.SetRow(boxView, 2);
           Grid.SetColumnSpan(boxView, 2);
           Label label = new Label
           {
               Text = "Row 2, Column 0 and 1",
               HorizontalOptions = LayoutOptions.Center,
               VerticalOptions = LayoutOptions.Center
           };
           Grid.SetRow(label, 2);
           Grid.SetColumnSpan(label, 2);

           grid.Children.Add(boxView);
           grid.Children.Add(label);

           Title = "Basic Grid demo";
           Content = grid; 

所以我想问题是折叠几折,我怎样才能“缩小”网格视图以查看网格的其他单元格?此外,我是否以正确的方式进行此操作?还是有比使用网格更好的方法?

【问题讨论】:

    标签: c# xamarin mobile xamarin.forms roguelike


    【解决方案1】:

    您应该将内容变形为ScrollView 并设置Content = scrollView,然后您可以滚动查看所有元素:

        public MainPage()
        {
            InitializeComponent();
    
    
            StackLayout stackLayout = new StackLayout() { VerticalOptions = LayoutOptions.FillAndExpand };
    
    
            Grid grid = new Grid
            {
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
            };
            stackLayout.Children.Add(grid);
    
            //......
            
            grid.Children.Add(boxView);
            grid.Children.Add(label);
    
            Title = "Basic Grid demo";
    
            //warp the content into a ScrollView 
            ScrollView scrollView = new ScrollView { Content = stackLayout };
            scrollView.Orientation = ScrollOrientation.Both;
    
            Content = scrollView;
        }
    

    【讨论】:

    • 嗨,杰克,感谢您的回复。这与我正在寻找的非常接近(我将它标记为答案,因为它满足我想要的那一刻),我认为还有一种方法可以使这个滚动视图可缩放? (通过滚动视图,他们可以在地图上移动他们的视口,这对于玩家了解他们所在的地图以尝试规划他们的路径/如何使用他们的资源很有用)如果你不知道,不用担心顺便感谢您的回复:)
    猜你喜欢
    • 2019-07-11
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多