简体   繁体   中英

How to setup project in Visual Studio 2017 for Xamarin.Forms with C++ unmanaged library?

My project is targeted at Android and iOS platforms.

I need to create a native C++ library (portable), a C# wrapper library (dll) around the C++ library and finally a Xamarin.Forms demo app using these libraries. The native and wrapper libraries are deliverables, the demo app is for proof of concept.

I did a full install of Visual Studio 2017, but cannot seem to find the correct combination of project types with all references correctly set.

How should I set this up in Visual Studio 2017?

I don't mind setting up a multitude of projects. My main concern is to share as much code as possible.

Compile your C++ lib (with clang/gcc for example) for each platforms. That shoud give a mylib.so on Android and a mylib.dylib (or mylib.framework if you intend to push it on the App Store). Your C++ should use extern C to expose the API. This part isn't related to VS templates/projects, you just need the proper binaries.

Create a .NET standard 2.0 library project. Use multi-targeting bu using <TargetFrameworks>MonoAndroid8.1;Xamarin.iOS10;netstandard2.0</TargetFrameworks> . Look for crossplatform projects on GitHub if you need examples about setting that up. Your C# code here will use P/Invoke to call C++ and handle the marshalling to expose a C#-friendly API. Again, see wrappers for popular libraries on GitHub.

Then you need to create a Xamarin.Forms project, which contains 3 projects:

  • 1 shared assembly with all the shared code and views and references your .NET Standard library project.
  • 1 iOS project which references the shared Xamarin.Forms project above and has a native reference of the dylib/framework containing the C++ code.
  • 1 Android project which references the shared Xamarin.Forms project above and has a native reference of the .so binary containing your C++ code.

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