簡體   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