【问题标题】:Xamarin page constrution XML and code behindXamarin 页面构造 XML 和背后的代码
【发布时间】:2019-12-17 22:52:37
【问题描述】:

我正在尝试创建一个菜单页面,即使滚动(例如按钮停在屏幕边缘),我也需要在屏幕上保留一个按钮,并且我在 XML 中有此布局,但我需要在后面的代码中使用它添加一些帧动态。这是我做的,但显示不一样。请问有什么建议吗?

<Grid RowSpacing="0" ColumnSpacing="0">
        <Grid.Margin>
            <OnPlatform x:TypeArguments="Thickness">
                <On Platform="iOS" Value="0,20,0,0" />
            </OnPlatform>
        </Grid.Margin>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Label Text="FIXED HEADER DEMO" Margin="12" Grid.Row="0" FontSize="14" />
        <Grid x:Name="ContentGrid" RowSpacing="0" ColumnSpacing="0" Grid.Row="1">
            <ScrollView x:Name="TheScroll">
                <Grid RowSpacing="0" ColumnSpacing="0">
                    <Grid.RowDefinitions>
                        <RowDefinition x:Name="ImageRow" Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Image x:Name="BearImage" Source="bear.jpg"
                           Aspect="AspectFill"
                           Grid.Row="0" />
                    <Label LineBreakMode="WordWrap"
                           Margin="12,5,12,5"
                           Grid.Row="1">
                           Text="abc"
                    </Label>
                </Grid>
            </ScrollView>

            <Label x:Name="TitleText"
                   Text="Bear found in the wild!"
                   TextColor="White"
                   BackgroundColor="#FF264778"
                   HeightRequest="40"
                   VerticalOptions="Start" VerticalTextAlignment="Center"                   
                   HorizontalTextAlignment="Center" />
        </Grid>
    </Grid>

我的代码在后面

 public void PageConstructor()
        {


            Grid thirdG = new Grid
            {
                RowDefinitions =
                {
                    new RowDefinition { Height = new GridLength(1,GridUnitType.Auto) },
                    new RowDefinition { Height = new GridLength(1,GridUnitType.Star) },
                },
            };



            var image1 = new Image
            {
                Source = "bear.jpg",
            };
            var labeltext = new Label
            {
                LineBreakMode = LineBreakMode.WordWrap,
                Text = msg + msg2 + msg3,
            };
            thirdG.Children.Add(image1, 0, 0);
            thirdG.Children.Add(labeltext, 0, 1);



            Grid secondG = new Grid
            {


            };

            SecondTitle= new Label { Text = "Second title text", TextColor = Color.Black };
            scrollV = new ScrollView { Content = thirdG };

            secondG.Children.Add(SecondTitle);

            secondG.Children.AddVertical(scrollV);



            Grid firstG = new Grid
            {
                RowDefinitions =
                {
                    new RowDefinition { Height = new GridLength(1,GridUnitType.Auto) },
                    new RowDefinition { Height = new GridLength(1,GridUnitType.Star) },
                },
            };

            firstG.Children.Add(new Label { Text = "Title" }, 0, 0);
            firstG.Children.Add(secondG, 0, 1);




            Content = firstG;



        }   

【问题讨论】:

  • 您可以在 XAML 中构建您的布局,然后在后面的代码中对其进行修改。您不必选择其中一个,您可以将这些方法结合起来。
  • 好的。那么我可以在网格单元格中添加滚动视图吗?喜欢绑定它或我如何添加它?
  • 您可以在 XAML 中执行此操作。或者myGrid.Children.Add(view,col,row);

标签: c# xaml xamarin menu grid


【解决方案1】:

根据你的代码,我发现你在Grid中使用了ScrollView,它会被标签TitleText覆盖,所以我建议你可以把scrollview放在grid之外

        <Grid>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Label
            Grid.Row="0"
            Margin="12"
            FontSize="14"
            Text="FIXED HEADER DEMO" />
        <ScrollView Grid.Row="1">
            <Grid x:Name="ContentGrid">

                <Grid.RowDefinitions>
                    <RowDefinition x:Name="ImageRow" Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <Image
                    x:Name="BearImage"
                    Grid.Row="0"
                    HeightRequest="50"
                    Source="a11.jpg"
                    WidthRequest="50" />
                <Label
                    Grid.Row="1"
                    Margin="12,5,12,5"
                    LineBreakMode="WordWrap">
                    Text=&quot;abc&quot;
                </Label>
                <Label
                    x:Name="TitleText"
                    BackgroundColor="#FF264778"
                    HeightRequest="40"
                    HorizontalTextAlignment="Center"
                    Text="Bear found in the wild!"
                    TextColor="White"
                    VerticalOptions="Start"
                    VerticalTextAlignment="Center" />
            </Grid>
        </ScrollView>
    </Grid>

后面的代码效果相同:

public Page3()
    {
        InitializeComponent();

      
        var image1 = new Image
        {
            Source = "a11.jpg",WidthRequest=70,HeightRequest=70
        };

        var label1 = new Label
        {
            Text = "abc"
        };

        var label2 = new Label
        {
            Text = "Bear found in the wild!",HeightRequest=20,VerticalTextAlignment=TextAlignment.Center,HorizontalTextAlignment=TextAlignment.Center
        };
        Grid secondG = new Grid
        {

            RowDefinitions =
            {
                new RowDefinition { Height = new GridLength(1,GridUnitType.Auto) },
                new RowDefinition { Height = new GridLength(1,GridUnitType.Star) },
            },
        };      
       var scrollV = new ScrollView { Content = secondG };

        secondG.Children.Add(image1,0,0);

        secondG.Children.Add(label1,0,1);
        secondG.Children.Add(label2);
        Grid firstG = new Grid
        {
            RowDefinitions =
            {
                new RowDefinition { Height = new GridLength(1,GridUnitType.Auto) },
                new RowDefinition { Height = new GridLength(1,GridUnitType.Star) },
            },
        };
        var labeltext = new Label
        {
            LineBreakMode = LineBreakMode.WordWrap,FontSize=14,
            Text = "FIXED HEADER DEMO"
        };
        firstG.Children.Add(labeltext, 0, 0);
        firstG.Children.Add(scrollV, 0, 1);
        Content = firstG;
    }
}

【讨论】:

    猜你喜欢
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 2020-09-03
    • 1970-01-01
    • 2015-08-17
    • 2013-12-08
    • 1970-01-01
    相关资源
    最近更新 更多