简体   繁体   中英

Xamarin Portable Library PCL Preprocessor Platform

I'm trying to create and use de Portable library for a cross platform Xamarin.Forms. But the preprocessor for the platform doesn't seem set when I break in the code.

#if WINDOWS_PHONE || __ANDROID__
  Debug.WriteLine("Passed");
#endif

Using step by step I can see all the code between the preprocessor condition be skipped (tried on Android and Windows Phone). But when I try the same thing in the start project it works.

In Visual Studio the code should be highlighted when active but it's not in the PCL.

The preprocessors defined in the startup project should be shared with the library, no ?

PCL doesn't work this way. Try this:

using Xamarin.Forms;
...
if(Device.OS == TargetPlatform.Android)
    Debug.WriteLine("I'm on Android");
else if(Device.OS == TargetPlatform.iOS)
    Debug.WriteLine("This is iOS");

Here ( https://docs.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/sharing_code_options/ ) you can find the appropriate Xamarin article. What you are trying to do is Shared Assets approach and not PCL approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM