[英]How can I place labels at specific positions relative to an image in Xamarin.forms C#?
I need my Xamarin app to display an image and place labels at very specific positions.我需要我的 Xamarin 应用程序来显示图像并将标签放置在非常特定的位置。 I have:
我有:
The labels must be placed correctly regardless of the shape of the screen, like pinpoints and names of places on a map.无论屏幕的形状如何,都必须正确放置标签,例如地图上的精确点和地点名称。
The XAML just defines the empty myRelativeLayout and places it in a grid. XAML 只是定义空的 myRelativeLayout 并将其放置在网格中。
This is the corresponding c# file.这是对应的c#文件。 It doesn't work.
它不起作用。 The labels seem to position themselves relatively to the layout rather than the image itself.
标签似乎相对于布局而不是图像本身定位自己。
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);
}
}
}
In a different XAML I was able to make it work statically using {ConstraintExpression ...} for the constraints.在不同的 XAML 中,我能够使用 {ConstraintExpression ...} 作为约束使其静态工作。
我通过使用 Skiasharp 库而不是布局和标签来解决这些问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.