Xamarin.Forms 自定义控件的制作方式是否与 WPF/Desktop 相同?

Are Xamarin.Forms custom controls made the same way as WPF/Desktop?

我已经使用 C# 和 WPF 构建桌面应用程序很长时间了。可以肯定地说(这并不意味着我很擅长)我知道如何使用此代码(删节)构建自定义控件和所需的样式:

public class HexadecimalBox : ExtendedTextBox
{
    public static readonly DependencyProperty RedProperty = DependencyProperty.Register("Red", typeof(int), typeof(HexadecimalBox), new PropertyMetadata(0, Value_PropertyChanged));

    public int Red
    {
        get => (int)GetValue(RedProperty);
        set => SetValue(RedProperty, value);
    }

    static HexadecimalBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(HexadecimalBox), new FrameworkPropertyMetadata(typeof(HexadecimalBox)));
    }

    //Overrides, methods, events registered inside the OnApplyTemplate, etc.
}

这种风格,也被删节了(在 Themes/Generic.xaml 文件中):

<!--HexaDecimalBox Style-->
<Style TargetType="{x:Type n:HexadecimalBox}">
    <Setter Property="MaxLength" Value="9"/>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type n:HexadecimalBox}">
                <Border Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" MinWidth="{TemplateBinding MinWidth}"
                        BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}" VerticalAlignment="{TemplateBinding VerticalAlignment}"
                        HorizontalAlignment="{TemplateBinding HorizontalAlignment}" SnapsToDevicePixels="True">
                    <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden"
                                  VerticalScrollBarVisibility="Hidden" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Opacity" Value="0.7"/>
        </Trigger>
    </Style.Triggers>
</Style>

所以,我想学习如何开发一个跨平台的应用程序(Android和iOS),自定义控件,基本相同UI.

我刚开始读到它,但我想确定是否可以创建类似的东西。

如何使用 Xamarin.Forms 实现该目标?那可能吗?它与 WPF 有何不同?

我以前不使用 WPF,但官方网站上有一份详细的文档可以帮助 WPFWindows Forms developers 学习使用 Xamarin 进行移动应用程序开发,通过将他们现有的知识和经验交叉引用到移动习语,并提供将桌面应用程序移植到移动设备的示例。

这是文档:Cross-Platform for Desktop Developers

这部分讲的是UI Controls Comparison

I want to learn how to develop a cross-platform app (Android and iOS), with custom controls, basically having the same UI.

如果您使用 Xamarin.forms,Android 和 iOS 确实共享相同的 UI。

本文档是关于如何 create controls 在 Xamarin.那里有样例你可以看看学习。