我正在尝试为Xamarin表单创建一些自定义控件...
控件应表现为一组其他控件。例如,“标签+条目”作为要插入到页面中的一个控件。
我在这里创建了一个继承自View类的简单控件。
这是带有两个标签的类的示例。在此类中,除了这些标签之外,还应该插入其他控件。
ExampleVendor\AdminBundle\Repository\UserRepository:
autowire: true
tags: ['doctrine.repository_service']
然后将此类插入页面:
public class Ton : View
{
public Ton() : base()
{
Label L;
L = new Label();
L.Parent = this;
L.Text = "XXX";
Label M;
M = new Label();
M.Parent = this;
M.Text = "YYY";
}
}
,但是控制页面中未显示。
有人知道原因吗?
谢谢。 干杯,
答案 0 :(得分:1)
视图是一个抽象类,它不能有子级。此外,它也不知道如何在其中布置子项。您必须已经从Layout<View>
或其他现有的布局类(例如Grid
,StackLayout
,RelativeLayout
等)继承了您的类。
基类取决于您需要如何布置孩子。
请参阅下面的GitHub存储库,以了解如何创建自定义控件。 https://github.com/harikrishnann/BusyButton-XamarinForms
答案 1 :(得分:1)
您创建了两个标签,但实际上并没有将它们添加到“视图”层次结构中
public class Ton : ContentView
{
public Ton() : base()
{
Label L;
L = new Label();
L.Text = "XXX";
Label M;
M = new Label();
M.Text = "YYY";
// because you have multiple elements, they must be contained in a Layout
var stack = new StackLayout();
stack.Children.Add(L);
stack.Children.Add(M);
// assign your controls to the View hierarchy
this.Content = stack;
}
}