如何根据平台目标在自定义控件中使用不同的基类? [英] How to use different base class in custom control depending on platform target?

查看:50
本文介绍了如何根据平台目标在自定义控件中使用不同的基类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于iOS的自定义编辑器,但对于UWP,我使用了另一个名为 SfRichTextEditor 的编辑器.

I have a custom editor that I use for iOS, but for UWP I use another editor called SfRichTextEditor.

现在,我不知道如何创建像可共享的类,因为它们具有相同的bindableproperties而不是我目前正在做的事情,即在它们之上创建 ContentView ,这导致:

Now I wonder how I can create like a shareable class, since they have the same bindableproperties instead of doing what I currently do, which is creating a ContentView on top of them, which is causing:

  1. 出于性能目的而不必要的嵌套

  1. unnecessary nesting for performance purposes

重复代码

似乎在某些绑定上遇到了麻烦(未经验证,但有些奇怪).

seems to have trouble with some bindings (not verified, but something strange).

<ContentView>
    <OnPlatform x:TypeArguments="View">
        <OnPlatform.Platforms>
            <On Platform="iOS">

                <controls:CustomIOSEditor/>
            </On>
            <On Platform="UWP">
                <controls:CustomUWPEditor/>
            </On>
        </OnPlatform.Platforms>
    </OnPlatform>
</ContentView>

因此,除了这种方法,我希望有一个可共享的基类,以重用代码.

So instead of this approach, I want to have a shareable base class if possible, to reuse the code.

这些是我今天拥有的x2控件.

These are the x2 controls that I have today.

我的iOS自定义控件:

My iOS custom control:

public class CustomIOSEditor : Editor // Using regular xamarin editor
{
    public static readonly BindableProperty StringResultCommandProperty =
        BindableProperty.Create(
            nameof(StringResultCommand),
            typeof(ICommand),
            typeof(CustomIOSEditor),
            default(ICommand));

    public object StringResultCommandParameter
    {
        get => GetValue(StringResultCommandParameterProperty);
        set => SetValue(StringResultCommandParameterProperty, value);
    }
}

我的UWP自定义控件:

My UWP custom control:

public class CustomUWPEditor : SfRichTextEditor // Using SfRichTextEditor instead here.
{
    public static readonly BindableProperty StringResultCommandProperty =
        BindableProperty.Create(
            nameof(StringResultCommand),
            typeof(ICommand),
            typeof(CustomUWPEditor),
            default(ICommand));

    public object StringResultCommandParameter
    {
        get => GetValue(StringResultCommandParameterProperty);
        set => SetValue(StringResultCommandParameterProperty, value);
    }
}

最新线索**更新**共享的.csproj:

Latest clue** UPDATE** Shared .csproj:

MacOS:

设置:

错误:

推荐答案

由于所有代码都是通用的,除了继承是不同的,因此最好使用编译时检查(在这种情况下,无需担心代码会混乱).

Since all code is common except the inheritance is different, it better to use a compile-time check (no worries of code being messy in this case).

Xamarin.Forms项目不支持多目标框架,与之相反的是称为MAUI的下一个进化.

Xamarin.Forms project does not support Multi-targeting frameworks, at the opposite it next evolution which is called MAUI will.

与此同时,您可以使用 MSBuild SDK Extras SDK代替默认的 Microsoft.NET.Sdk an(但请注意,它不受官方支持),但最终结果是整洁的(编译时检查).

Meanwhile you can use MSBuild SDK Extras SDK instead of the default Microsoft.NET.Sdk an (but keep in mind that it is not officially supported), but end result is neat (compile-time check).

  • YourSharedProject.csproj 中,将< Project Sdk ="Microsoft.NET.Sdk"> 更改为< Project Sdk ="MSBuild.Sdk.Extras/2.1.2> .
  • 设置项目目标平台及其版本:

如果您以netstandard2.1和iOS 10为目标,然后将< TargetFramework> netstandard2.1</TargetFramework> 更改为< TargetFrameworks> netstandard2.1; iOS10</TargetFrameworks>,首先从 netstandardxx 开始.

Example if you target netstandard2.1 and iOS 10 then change <TargetFramework>netstandard2.1</TargetFramework> to <TargetFrameworks>netstandard2.1;iOS10</TargetFrameworks> by starting with the netstandardxx first.

  • 标签Uwp:< TargetFrameworks Condition ="'$(OS)'=='Windows_NT'> $(TargetFrameworks); uap10.0.17763; netcoreapp3.1; net472</TargetFrameworks> (如果您安装了.net framework 4.7.1而不是4.7.2,用 net471 替换 net472 (对于.net.core和uwp版本也是如此).
  • Taget Uwp: <TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);uap10.0.17763;netcoreapp3.1;net472</TargetFrameworks> (if you have .net framework 4.7.1 installed instead of 4.7.2, replace net472 by net471 (same thing for .net.core and uwp versions).

最后,您的.csproj文件启动将如下所示:

At the end, your .csproj file starts will looks like this:

<Project Sdk="MSBuild.Sdk.Extras/2.1.2">
    <PropertyGroup>
        <TargetFrameworks>netstandard2.1;iOS10</TargetFrameworks>
        <TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">
$(TargetFrameworks);uap10.0.17763;netcoreapp3.1;net472</TargetFrameworks>

所有这些使您可以使用

All this will enable you to use the symbolic constants in a conditional compile time check:

public class CustomIOSEditor : 
#if __iOS__
           Editor     //will inherit from this if we are building against iOS
#endif
#if WINDOWS_UWP      //will inherit from this if we are building against uwp
           SfRichTextEditor
#endif
{
    public static readonly BindableProperty StringResultCommandProperty =
        BindableProperty.Create(
            nameof(StringResultCommand),
            typeof(ICommand),
            typeof(CustomIOSEditor),
            default(ICommand));

    public object StringResultCommandParameter
    {
        get => GetValue(StringResultCommandParameterProperty);
        set => SetValue(StringResultCommandParameterProperty, value);
    }
}


针对其他平台版本:


For targeting other platforms-version:

  • Mac版本20: Xamarin.Mac20
  • Android版本10.0: MonoAndroid10.0
  • tizen 40版: tizen40

这篇关于如何根据平台目标在自定义控件中使用不同的基类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆