简体   繁体   English

Xamarin形成如何在Xaml中使用自定义ui

[英]Xamarin forms how to use custom ui in Xaml

I have extended the label class to create a slightly different piece of UI and would rather add it to my page with XAML rather than C# code. 我已经扩展了标签类,以创建稍有不同的UI,并希望使用XAML而不是C#代码将其添加到我的页面中。 How can I do this? 我怎样才能做到这一点?

Code: 码:

namespace M.Helpers
{
    public class LinkingLabel: Label
    {
        public LinkingLabel(Uri uri, String labelText = null)
        {
            Text = labelText ?? uri.ToString();
            TextColor = Color.Blue;
            GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(() => Device.OpenUri(uri)) });

        }
    }
}

How can I simply write <LinkingLabel Uri="" Text=""></LinkingLabel> inside of my pages XAML. 如何在页面XAML内简单地写<LinkingLabel Uri="" Text=""></LinkingLabel> I am getting errors but no intellisense to help me import. 我遇到错误,但是没有智能提示可以帮助我导入。

First add namespace declaration attribute to the root tag in the XAML file: 首先将名称空间声明属性添加到XAML文件中的根标记:

xmlns:controls="clr-namespace:M.Helpers"

And, use it while declaring your custom control: 并且,在声明自定义控件时使用它:

<controls:LinkingLabel Uri="" Text="">
...
</controls:LinkingLabel>

Also, because you don't have a default constructor in your custom control, you will need to pass in the arguments as parameters. 另外,由于自定义控件中没有默认的构造函数,因此需要将参数作为参数传递。 More details here . 更多细节在这里

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

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