Is there any way to access the native control in Xamarin.Forms at runtime?

LeloLelo Member ✭✭✭

For instance in NativeScript I can

myControl.ios. // Access the native control
myControl.android. // Access the native control

anywhere.

Is there any way to access the native control in Xamarin.Forms at runtime and manipulate it?

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I think you have to use Custom Renderer, You have not a "direct" access to the original control

  • LeloLelo Member ✭✭✭

    I thought it was what the On method did at first:

    But it seems only a set of defined actions can be made on it

    Maybe it is possible to add a method to PlatformSpecific that returns the native control. It might then be possible to work with it after adding xamarin.ios.dll/xamarin.android.dll to the project.

  • ColeXColeX Member, Xamarin Team Xamurai

    You could access native control using PlatformSpecific , but it just represents a specific type , not a instance , you can't access it to a exact UIButton.

    The correct usage

    <ContentPage xmlns:ios="clr-namespace:MyCompany.Forms.PlatformConfiguration.iOS" ...>
      ...
      <Label Text="Label Shadow Effect" ios:Shadow.IsShadowed="true" ... />
      ...
    </ContentPage>
    

    Or

    using Xamarin.Forms.PlatformConfiguration;
    using MyCompany.Forms.PlatformConfiguration.iOS;
    
    ...
    
    shadowLabel.On<iOS>().SetIsShadowed(true);
    

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/platform-specifics/#consuming-the-platform-specific

  • LeloLelo Member ✭✭✭
    edited July 10

    I am building a library. Can I add Xamarin.Forms.Platform.IOS, Xamarin.Forms.Platform.Android to the library and access the control natively that way?

  • ColeXColeX Member, Xamarin Team Xamurai
    edited July 11

    As i said above ,it just represents a specific type , once you set the property on it(like button) , all of them (UIButton in iOS ,Button in Android) will take effect ,no matter you use it in your own code or library.

  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭
    edited July 11

    Building a crossplatform library for x platforms means you need to make x libraries, one for each platform. Check Xamarin.Android and Xamarin.iOS documentation on how to build libraries.

  • LeloLelo Member ✭✭✭
    edited July 11

    I found I could make a Multiplatform library. However I don't fully understand the difference these selections

    When selecting single - and then

    I don't get access to UIKit etc in the platform specific iOS project. Why? When selecting platform specific- the iOS project is already there. And from it, I have access to UIKit. Shoulden't I have access to UIKit in the project created by "add platform implementation" as well (after selecting single)?

  • LeloLelo Member ✭✭✭
    edited July 16

    The Multiplatform project given from selecting platform-specific template has target-framework: .NET Framework selected by default - which I thought was strange because I am using Visual Studio For Mac -but I got some explanation from here. .NET Standard can't be selected for some reason.

    When creating a single-codebase type it had .NET Portable selected by default - which resulted in that I could not use Console.WriteLine(""). (WriteLine() was not inside System.Console). Here it is possible to select .NET Standard (Unlike the platform-specific type of project) - but it is only possible to select up to .Net Standard 1.5 - not higher. Selecting .Net Standard 1.5 allowed me to access Console.WriteLine() like normal however - but .NETStandard Library Version 1.6.0 is what gets added in packages.

    I didn't realise I could go in to project -> Options -> Build -> General and change target framework. (The selection between portable and .NET Standard is made in the project creation process when creating Xamarin.Forms projects). Also I believe I can go into csproj file and editing it - setting it to :NET Standard 2.0 - or something else?

  • ColeXColeX Member, Xamarin Team Xamurai

    but it is only possible to select up to .Net Standard 1.5 - not higher.

    I also see this issue , try to ask on visual studio forum: https://developercommunity.visualstudio.com/spaces/41/index.html

    Open support ticket for better help : https://support.microsoft.com/en-us/hub/4343728/support-for-business

  • LeloLelo Member ✭✭✭

    If make a library where I extend the highest order renderer> @ColeX said:

    You could access native control using PlatformSpecific , but it just represents a specific type , not a instance , you can't access it to a exact UIButton.

    The correct usage

    <ContentPage xmlns:ios="clr-namespace:MyCompany.Forms.PlatformConfiguration.iOS" ...>
      ...
      <Label Text="Label Shadow Effect" ios:Shadow.IsShadowed="true" ... />
      ...
    </ContentPage>
    

    Or

    using Xamarin.Forms.PlatformConfiguration;
    using MyCompany.Forms.PlatformConfiguration.iOS;
    
    ...
    
    shadowLabel.On<iOS>().SetIsShadowed(true);
    

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/platform-specifics/#consuming-the-platform-specific

    Is the second exported effect parameter in the platform specific project the name given in the forms project?

    <---->

Sign In or Register to comment.