[英]How can I create a custom Xamarin.Forms component that can be creating through xaml?
My apologies if this question has already been answered. 抱歉,这个问题已经回答。 I'd like to create a tab component with some specific behaviors based on Xamarin.Forms where the tab definition xaml is something similar to:
我想基于Xamarin.Forms创建具有某些特定行为的选项卡组件,其中选项卡定义xaml类似于:
<Tab OnTabChange="Some_Event_Handler">
<Tabs>
<Tab Text="xxx" />
<Tab Text="yyy" />
</Tab>
</Tab>
Are there any resources available that use custom xaml that I can review? 是否有可用的资源可以使用自定义xaml进行审核?
Any insight is greatly appreciated. 非常感谢任何见解。
The easiest way is to create a custom renderer. 最简单的方法是创建自定义渲染器。 And the easiest way to do that is to extend an existing element and give it new capabilities.
最简单的方法是扩展现有元素并赋予其新功能。
The first step is to create the element. 第一步是创建元素。 Inside the class create a static readonly BindableProperty and a non-static property which uses GetValue and SetValue.
在类内部创建一个静态只读BindableProperty和一个使用GetValue和SetValue的非静态属性。 That will support data binding.
那将支持数据绑定。
Each platform will need to know how to render your new control, so add a class such as MyLabelRenderer.cs. 每个平台都需要知道如何呈现新控件,因此添加一个类,例如MyLabelRenderer.cs。 In that file you will want to override the methods that might need to render your control.
在该文件中,您将要覆盖呈现控件所需的方法。
public class MyLabelRenderer : LabelRenderer
{
protected override void OnElementChanged (
ElementChangedEventArgs<Label> e)
{
base.OnElementChanged (e);
MyLabel el = (MyLabel)this.Element;
// custom stuff here
}
}
You then declare a local namespace and can use your custom object 然后,您声明一个本地名称空间并可以使用您的自定义对象
<local:myLabel ... >
For a detailed walk-through see http://jliberty.me/customRenderer 有关详细的演练 ,请参见http://jliberty.me/customRenderer
Hope that helps 希望能有所帮助
-jesse -杰西
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.