繁体   English   中英

如何在 Xamarin.forms C# 中相对于图像的特定位置放置标签?

[英]How can I place labels at specific positions relative to an image in Xamarin.forms C#?

我需要我的 Xamarin 应用程序来显示图像并将标签放置在非常特定的位置。 我有:

  • 图像文件名,
  • 以像素为单位的图像大小
  • 以像素为单位的标签坐标
  • 标签的内容。

无论屏幕的形状如何,都必须正确放置标签,例如地图上的精确点和地点名称。

XAML 只是定义空的 myRelativeLayout 并将其放置在网格中。

这是对应的c#文件。 它不起作用。 标签似乎相对于布局而不是图像本身定位自己。

public partial class MyPage : ContentPage
{
    public MyPage()
    {
        InitializeComponent();

        Image myImage = new Image()
        {
            Source = ImageSource.FromFile(MainImagePath))
        };

        myRelativeLayout.Children.Add(myImage, () => myRelativeLayout.Bounds);

        foreach (Tag ft in ImageTags)
        {
            Label lb = new Label
            {
                Text = ft.TagContent,
                BackgroundColor = Color.FromHex("0x40000000"),
                TextColor = Color.White,
                FontSize = 10
            };

            Constraint ccx = Constraint.RelativeToView(myImage, (p, s) => { return s.Width * ft.X / fp.MainImage.Width; });
            Constraint ccy = Constraint.RelativeToView(myImage, (p, s) => { return s.Height * ft.Y / fp.MainImage.Height; });

            myRelativeLayout.Children.Add(lb, xConstraint: ccx, yConstraint: ccy);
        }

    }
}

在不同的 XAML 中,我能够使用 {ConstraintExpression ...} 作为约束使其静态工作。

我通过使用 Skiasharp 库而不是布局和标签来解决这些问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM