question

nnovalbos-6961 avatar image
0 Votes"
nnovalbos-6961 asked JarvanZhang-MSFT answered

Xamarin.Forms plugin with multi-targeting project

Hello,

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

To make the plugin I am following the following example:

https://docs.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.



dotnet-xamarin
· 1
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.

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

Hi, nnovalbos-6961. What exception occured? Could you please post the details about the error log? To involve the method of the 'MultiTargeting' project, please make sure the project could build well.

0 Votes 0 ·
JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered

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.


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.

nnovalbos-6961 avatar image
0 Votes"
nnovalbos-6961 answered

Hi @JarvanZhang-MSFT , 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.

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.