Xamarin.Forms plugin with multi-targeting project

nnovalbos 371 Reputation points
2021-05-10T14:32:06.823+00:00

Hello,

I'm having trouble making a plugin for xamarin.forms.

To make the plugin I am following the following example:

https://learn.microsoft.com/es-es/nuget/guides/create-packages-for-xamarin

The problem is that when in my example xamarin.forms project, I try to access the Log method from the 'Core' project of my example, the dependency is not resolved well.

In the CrossLoggingLibrary.shared.cs class, the CreateMultiTargeting () method throws an exception:

static IMultiTargeting CreateMultiTargeting ()  
         {  
#if NETSTANDARD1_0 || NETSTANDARD2_0  
             return null;  
#else  
             return new MultiTargetingImplementation ();  
#endif  
         }  

It goes through the if netstandard ....

To call it, I do it as I have seen in this other example:

https://github.com/jsuarezruiz/Xamarin.Forms-Samples/tree/master/MultiTargeting

CrossMultiTargeting.Current.Sample ();

Any ideas? Thank you.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,364 questions
{count} votes

2 answers

Sort by: Most helpful
  1. nnovalbos 371 Reputation points
    2021-05-11T07:48:30.703+00:00

    Hi @JarvanZhang , thanks for answering.

    When you use multi targing project template in Visual Studio, its create automatically one class as this:

    public static class CrossMultiTargeting  
        {  
            static Lazy<IMultiTargeting> implementation =   
    			new Lazy<IMultiTargeting>(() => CreateMultiTargeting(), System.Threading.LazyThreadSafetyMode.PublicationOnly);  
      
            public static bool IsSupported => implementation.Value == null ? false : true;     
            public static IMultiTargeting Current  
            {  
                get  
                {  
                    IMultiTargeting ret = implementation.Value;  
                    if (ret == null)  
                    {  
                        throw NotImplementedInReferenceAssembly();  
                    }  
                    return ret;  
                }  
            }  
            static IMultiTargeting CreateMultiTargeting()  
            {  
    #if NETSTANDARD1_0 || NETSTANDARD2_0  
                return null;  
    #else  
                return new MultiTargetingImplementation();  
    #endif  
            }  
     
            internal static Exception NotImplementedInReferenceAssembly() =>  
                new NotImplementedException("...");  
        }  
    

    The exception is throwed because the method CreateMultiTargeting return null.

    I need know how can I use this kind of project for make plugins.

    0 comments No comments

  2. JarvanZhang 23,961 Reputation points
    2021-05-11T09:16:04.103+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    The exception is throwed because the method CreateMultiTargeting return null.

    This should be the correct behavior. By default, the TargetFramework of the shared project is .Net Standard 2.0 now. So it will pass the #if NETSTANDARD1_0 || NETSTANDARD2_0 statement and return null. You could right-click the shared project->Properties to check the version of TargetFramework.

    You can also change the version to 'NETSTANDARD2_1' for test, it will retrun new MultiTargetingImplementation(); in this case.

       static IMultiTargeting CreateMultiTargeting()  
       {  
           #if NETSTANDARD2_1  
               return null;  
           #else  
                   return new MultiTargetingImplementation();  
           #endif  
       }  
    

    Best Regards,

    Jarvan Zhang


    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.