首页 文章

Xamarin表单 - AbsoluteLayout中的StackLayout

提问于
浏览
1

我是Xamarin Forms的新手,我一直试图让StackLayout在AbsoluteLayout中工作 . StackLayout中有2个子项:图像(300x300随机图像)和标签 . 我将展示一个说明我问题的例子 .

以下代码对我来说效果很好(我正在使用300x300图像):

public class TestedPage : ContentPage
{
    public TestPage()
    {
    BackgroundColor = Color.Brown;
    StackLayout stl = new StackLayout();
    AbsoluteLayout ab = new AbsoluteLayout();

    Image img = new Image();
    img.Source = ImageSource.FromFile("circle.png");

    Label lbl = new Label
    {
        Text = "#Text1000",
        TextColor = Color.White,
        FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
        HorizontalOptions = LayoutOptions.Center
    };

    stl.Children.Add(img);
    stl.Children.Add(lbl);

    ab.Children.Add(stl);

    Content = ab;
    }
}

问题是上面的代码只是在没有定位的情况下抛出'stl',这对我来说很重要,因为我需要把它放在我想要的位置 . 所以,你可能会问"why don't you use more parameters for Children.Add() ?",好 ab.Children.Add(stl, new Rectangle(0,0,450,450)); 不会定位'stl ' correctly, for me at least, in the above code, it will be at start of screen and a bit after center, to the right, and not origin(0,0). Also, curious that I debugged and found out that Stack, Image and Absolute all have different height and width. Above code gives me 450, 450 for ' stl ' (which is fine); 616, 360 for ' ab '; 150, 450 for ' img' .

那么,这里有什么我想念的吗?这对我来说有点混乱,因为我让它工作但它有点“马车”,不像我预期的那样行事 . 由于我没有Xamarin Forms的经验,我相信我在这里缺少一些重要的东西,这就是为什么我希望你们能给我一些线索 .

有任何想法吗 ?

任何帮助表示赞赏 . 谢谢!

1 回答

  • 1

    你可以尝试:

    AbsoluteLayout.SetLayoutFlags(stl, AbsoluteLayoutFlags.All);
    
    AbsoluteLayout.SetLayoutBounds(stl, new Rectangle(0f, 0f, 1f, 1f));
    

相关问题