question

JakobAagesen-6346 avatar image
0 Votes"
JakobAagesen-6346 asked ColeXia-MSFT commented

subclass native control to use in xamain.forms

hi
I have read and looked at the xamarin project SubclassedNativeControls. And it works fine. Now i am trying to do something similar, just with a TextView.
I have wrapped a TextView control and what to use it in the Xamarin forms project.
this is the wrapper:
public class MyView : TextView
{
public MyView(Context context, IAttributeSet attrs) :
base(context, attrs)
{
Initialize();
}

         public MyView(Context context, IAttributeSet attrs, int defStyle) :
             base(context, attrs, defStyle)
         {
             Initialize();
         }
    
         private void Initialize()
         {
             Text = "Hello World";
             SetMinimumWidth(100);
             SetMinimumHeight(100);
             SetBackgroundColor(Android.Graphics.Color.Red);
         }
     }

and this is the xaml page in my xamarin forms project:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:mobile.friends"
xmlns:localimages="clr-namespace:mobile.friends.Extensions"
xmlns:androidLocal="clr-namespace:mobile.friends.Droid;assembly=mobile.friends.Android;targetPlatform=Android"
x:Class="mobile.friends.LoginPage">

 <RelativeLayout>
     <androidLocal:MyView x:Arguments="{x:Static androidLocal:MainActivity.Instance}" />
 </RelativeLayout>

</ContentPage>
i can't find out what the problem is, i am not seeing the control on my page.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JakobAagesen-6346 avatar image
0 Votes"
JakobAagesen-6346 answered ColeXia-MSFT commented

Thanks for the reply.
I downloaded the complete sample portfolio from Xamarin and in there they have a solution called SubclassedNativeControls, This solution, kind of do what i want and does not use SAP:

92104-image.png


My situation is that i want to use Xamarin forms for most of my application but there are some of it where i need to go native. One of the places are a page where i want to use and show google map. as i understand it, it is not supported in xamarin forms by default.

So either i want to show a complete native layout page, having to navigate to it, from an xamarin forms page and then back again or even better i want to host a native control within an xamarin forms page if possible.
the latter requires me to create a UI control that i also don't know how to do. Not much information is around on that. All i found was how to create complete layout pages.


image.png (18.5 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Do you want Google Maps for just Android, or also on iOS? There is the Xamarin.Forms.Maps package (official) or Xamarin.Forms.GoogleMaps (third party) depending on what you're looking for.

1 Vote 1 ·

You can create a native view in specific project and show it with View Custom renderer , check https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/view .

1 Vote 1 ·

This is for both Android and iOS.
I did not see the Xamarin.Forms.Maps. I will take a look at that one.

About the CustomRenderer. I had read about that, but did not se it as anything i could use because of it's level of difficulty. Maybe i should take a look at it again.

Thank you for all the help.

0 Votes 0 ·

Sure,If you have any other problem please let us know.

0 Votes 0 ·
ColeXia-MSFT avatar image
0 Votes"
ColeXia-MSFT answered ColeXia-MSFT edited

Hello,

Welcome to Microsoft Q&A!

I assume that you're developing a .NET Standard project , however , only Shared Asset Project (SAP) supports to reference a native view in xaml , refer to here .

Shared Asset Project seems to be deprecated from xamarin , at least we can't see the template in latest visual studio .

Before

91951-euqkm.png

Now

91934-2.png

Now there is no option for shared project template.


Best Regards,
Cole Xia


If the response is helpful, please click "Accept Answer" and upvote it.
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.





euqkm.png (14.4 KiB)
2.png (15.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.