CBL 2.0 and shared .NET Standard

I have a Xamarin Project that uses the new .NET Standard. In the shared .NET Standard project I would like to add Couchbase Lite 2.0 which I have installed via nuget.

The problem is when I call the var db = Database.Exists("my-database"); it gives an error because it needs the Couchbase.Lite.Support.UWP.Activate(); called first. But this is not possible in the shared .NET Standard project. Of course it works if I place the couchbase code in the UWP, iOS or Android Project.

My goal was to have the shared .NET Project have all of the couchbase code there, not repeat the same cond in each UWP, iOS and Android Projects in xamarin.

Is this possible?

Thank you

As long as you call Activate() from the top level app first, you can use Couchbase Lite from inside the .NET Standard library.

That was it… Thank you.

I was placing the Couchbase.Lite.Support.UWP.Activate(); in the app.xaml.cs OnLaunched() instead of App()

Hi Marco,

I’m trying to do excactly the same project, without full success for now.

I tried to call the respective Activate function in each projects, It worked in UWP, but not Android and Ios, thru Xamrin live player.
You can find my post here : Xamarin.forms Cross platform Oct. 2017

May you tell me how you are doing it ? Version of Visual studio, version of Couchbase, maybe some examples …

It would be great if we could help each others !

Regards,

Steeve

Hi Steeve,

I had to reformat my computer and I have been installing back all of my programs. I will re-create a test project and update. Originally I also had issues with the Android project but did get it to work.

Hi Marco,

Thank you for your answers, I’m feeling less alone in the dark !

I did preety the same as you, but it’s steal not working.

1°) What’s your target framework (API) version for your .Net Standard Library ? I kept mine with the default version 1.4.

2°) For the android project, you talked about the OnCreate(), I assume you’re talking about the MainActivity file. Is that True ?
I did add the same line Couchbase.Lite.Support.Droid.Activate(this); in this file, but before the LoadApplication (…); Did you do the same, or called Couchbase.Lite.Support.Droid.Activate(this); after LoadApplication (…) ?

3°) For Android, even without couchebase, I’ve a trouble with a .Net Standard Library, I posted a mail on the xamarin forum : https://forums.xamarin.com/discussion/105555/the-type-or-namespace-name-could-not-be-found
Can you confirme that you don’t have any probleme like that ?

Regards,

Steeve

Hi Steeve,

Below is the link where you can download my sample project.
http://pixolini.com/wp-content/uploads/2017/10/XamStand.zip

1 -I also have the .NET Standard set to version 1.4
2 - Yes for the Android Project, the OnCreate() in the MainActivity
3 - I am assuming you added the .NET Standard project reference to the Android Project?

I am using the Couchbase Mobile Database 2.0 Beta at https://developer.couchbase.com/documentation/mobile/2.0/guides/couchbase-lite/index.html

I used below link to change the Xamarin Project to .NET Standard 2.0
https://developer.xamarin.com/guides/xamarin-forms/under-the-hood/net-standard

Once you take a look at the project let me know if you have any questions. Hopefully this helps.

Marco

Hi Marco,

Thank you very much for your example.

Firstly, I learned that you have 4 projects in your solution, like if you started your solution with PCL library, and changed it to become a shared librabry project.

Secondly, I compiled it without trouble, but I had an exception when running the android project. It can’t find de library for the .Net Standard project. As always, when I add a .Net standard library in my solution, I’ve no trouble to add the reference, et no trouble to compile, but the app fail to run, as if the DLL was not send to the device, thru xamarin live player.

It’s very strange. I did try with my personnal computer too, installed a fresh VS2017 V15.4.1, create a new shared library solution+project, just added a .net standard library + reference + call of a method, same result : Exception reference not found.

If it’s working for you, I’m propably missing something, but what ?

Regards,

Steeve

Hi Steeve,

I think you are already doing this but let’s double check. Select the .NET Standard Project and Right Click. Then Click on Properties. Under the Application Settings is the Target framework set to .NET Standard 1.4? My Output type is Class Library.

Yes, I actually started with a New Shared Project Xamarin Application. Then I added a new project as .NET Standard 2.0. Then I moved the Shared Project Files to .NET Standard project and deleted the Xamarin Shared Project. I made sure the iOS, Android and UWP Projects had reference to the .NET Standard Project.

When I run the Android Project I am using the Android 7.1 API 25.

If I can think what might be causing your error I will update this post.

Hi Marco,

Thank you.

Why did you deleted the shared project ? What’s the purpose ?

I will try it anyway…

Regards,

Steeve

Steeve,

I followed the instructions at developer.xamarin in order to change the Shared/PCL project to .NET Standard 2.0. They instruct to remove it and add that logic to the new .NET Standard 2.0 project.

I used below link to change the Xamarin Project to .NET Standard 2.0
https://developer.xamarin.com/guides/xamarin-forms/under-the-hood/net-standard

If you download the new Visual Studio 2017 Preview when you create new Xamarin project the .NET Standard 2.0 is an option. The link is https://www.visualstudio.com/vs/preview/

Hope this helps.

Hi,

A new version of Xamarin Live player made possible to use a .net standard library into the solution.
This work well under Android, but not under Ios, yet.

Marco, Did you try your testApp thru Xamarin Live player, and with a real Android physical device? Did you try to make it work UWP platform?

I’m always bloked by the problem that the couchbase lite library (and dependencies) are not send to xamarin live player, even with the latest version (for info: 1.2.80 on android). (for info : No problem to compile).

Any info about that ?

Regards,

Steeve

Hey Steeve,

I have not tested with the Xamarin Live Player yet.

Marco