简体   繁体   English

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

[英]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 image file name,图像文件名,
  • the image size in pixels以像素为单位的图像大小
  • the labels' coordinates in pixels以像素为单位的标签坐标
  • the labels' contents.标签的内容。

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.

相关问题 如何在Xamarin.Forms的TabbedPage的选项卡中放置按钮? - How can I place a button in the the tabs of a TabbedPage in Xamarin.Forms? 如何使用 c# 在 xamarin.forms 登录成功后更改内容 - How can i change content after login success in xamarin.forms using c# 如何使用 c# 将单个 html 行绑定到 xamarin.forms 中的多个标签 - How to bind individual html lines to multiple labels in xamarin.forms with c# 如果我可以在C#文件中完成所有工作,为什么还要在Xamarin.Forms项目中使用Xaml标记? - Why use Xaml markup in Xamarin.Forms projects if i can do all job in C# file? 如何在Xamarin.forms C#中隐藏布局? - How to hide out a layout in Xamarin.forms C#? 如何检查 Xamarin.forms C# 中数据的长时间下载 - How to check for long download of data in Xamarin.forms C# Xamarin.Forms:如何在C#代码中使用自定义字体 - Xamarin.Forms: How to use custom Fonts in C# Code 如何使用 C# 在 xamarin.forms 中获取所有启用的键盘 - How to get all enabled keyboard in xamarin.forms with C# 如何在 Xamarin.Z64502425319129281C3683CAE8A8 中的 XAML 中使用 C# 变量的值? - How to use value of a C# variable in XAML in Xamarin.Forms? 如何在其控制器中投射 XAML 项目? [C#],[Xamarin.Forms] - How do I cast an XAML item in its controller? [C#],[Xamarin.Forms]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM