How to use ext to specify platform in maui?

mc 4,111 Reputation points
2023-04-24T07:47:42.1233333+00:00

I know to create custom controls I need create Viewhandlers and create viewHandler PropertyMap and CommandMapper in each platform android ios or others

I create a folder MyEntry and create ViewHandler MyEntryHandler.cs and MyEntryHandler.Android.cs and MyEntryHandler.iOS.cs which are partial class.

but still tell me can not find namespace of Androidx means I can not use AndroidX in MyEntryHandler.Android.cs

  public partial class PoEntryHandler : ViewHandler<Controls.MyEntry, MyEntryHandler> 
{
     protected override AndroidX.AppCompat.Widget.AppCompatEditText CreatePlatformView() {
          return new AndroidX.AppCompat.Widget.AppCompatEditText(); 
     }
}

why?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,231 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 72,251 Reputation points Microsoft Vendor
    2023-04-25T09:19:37.49+00:00

    Hello,

    Because iOS and Windows platform do not have AndroidX.AppCompat.Widget.AppCompatEditText class.

    To fix this error, you can refer to following steps.

    Firstly, you need to add conditional-compilation for android namespace in your MyEntryHandler.Android.cs like following code.

    #if  ANDROID
    
    using AndroidX.AppCompat.Widget;
    using Android.Content;
    
    
    #endif
    

    Then, Configure filename-based multi-targeting, open your .csprj file, add xml code from above document as children of the <Project> node, this error will disappear.The build system will know: Don't compile C# code whose filename ends with .Android.cs, if you aren't building for Android.

    Best Regards, Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Alexander Petree 150 Reputation points
    2023-04-24T09:20:36.56+00:00

    public partial class PoEntryHandler : ViewHandler<Controls.MyEntry, MyEntryHandler> { protected override AndroidX.AppCompat.Widget.AppCompatEditText CreatePlatformView() { return new AndroidX.AppCompat.Widget.AppCompatEditText(); } }

    0 comments No comments