简体   繁体   English

如何创建可以通过xaml创建的自定义Xamarin.Forms组件?

[英]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.

相关问题 如何使用 XAML 在 Xamarin.Forms 的 ListView 中选择一个项目 - How can I select an item in a ListView in Xamarin.Forms with XAML 如何在Xamarin.Forms中为ToolbarItem创建自定义渲染器? - How can I make a custom renderer for ToolbarItem in Xamarin.Forms? 如何创建 Xamarin.Forms 卡片轮播? - How can I create a Xamarin.Forms cards carousel? 如何为 Xamarin.Forms 中的自定义组件创建可绑定命令? - How to create bindable command for custom component in Xamarin.Forms? 如何在(Xamarin.Forms)XAML中使用IValueConverter转换完整的对象? - How can I convert a complete object with IValueConverter in (Xamarin.Forms) XAML? 如何在Xamarin.forms xaml中水平显示ListView中的项目? - How can I display items in a ListView horizontally in Xamarin.forms xaml? 如何在Xamarin.forms中创建自定义datagrid - How to Creating Custom datagrid in Xamarin.forms 如果我可以在C#文件中完成所有工作,为什么还要在Xamarin.Forms项目中使用Xaml标记? - Why use Xaml markup in Xamarin.Forms projects if i can do all job in C# file? 如何在Xamarin.Forms中为轮播实现活动指示器? - How can I implement an Activity Indicator for a Carousel in Xamarin.Forms? 如何在Xamarin.Forms的TabbedPage的选项卡中放置按钮? - How can I place a button in the the tabs of a TabbedPage in Xamarin.Forms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM