I am using Xamarin to develop iOS and Android application.I have creates one shared project which is common for both platforms. As I am using database its common and put in shared project.I need to encrypt database, so need to add native sqlite library reference in shared project so that I can modify that code.
My question is : I have not found any way to add reference of other project/library in shared project.
Is that bug or expected behavior?
Any explanations are appreciated.
Thanks.
You add references to a shared project just as you would a normal project reference. In Visual Studio or Fire, you right-click the "References" node of the real project and choose "Add Reference", and then pick the shared project from the list.
Shared Projects work well for application developers planning to use lots of platform-specific functionality in their cross-platform apps. While PCL projects continue to be supported in Visual Studio, . NET Standard is recommended for new projects.
Shared Projects (also sometimes called Shared Asset Projects) let you write code that is shared between multiple target projects including Xamarin applications.
There are no 'References' from the usual sense of adding a project reference from a Shared Project.
There is definitely scope for a Shared Project to be opened up further than it currently is, however its not, and this is by design, currently, although limiting in some ways.
In short, Shared Projects have no generated outputs. It is from other projects that reference the Shared Project(s) that compilable outputs are generated - based on any conditional compilation symbols that you may have specified.
In order for you to add sqlite library references you need to use NuGet in order to add packages to your solution.
These NuGet packages will then also install and create your platform specific library references appropriate for each platform you are targeting.
If you take a look on your platform specific projects, you will see the References treeview node expand when you add a NuGet package and install it into those projects with the library outputs appropriate for the package.
You can then code in the Shared Project against these NuGet packages that are referenced in your projects.
Update 1:-
If you've added the package from NuGet, most things should line-up perfectly in general unless there is any platform specific stuff included.
If there is functionality that is specific to a particular platform; or for a library that is only added directly to a platform-specific project, then you can use conditional #if statements in the Shared Project to access these platform-specific differences.
Update 2:-
Example:-
1) You have a Shared Project and Android, iOS and WindowsPhone platform-specific projects that reference this Shared Project.
2) Create another new WindowsPhone Class Library Project.
3) Add the following class to it:-
public class MyWindowsPhoneClass1
{
public string SayHello()
{
return "Hello";
}
}
4) In your WindowsPhone platform specific project, add a Reference (in this case it will be a Solution reference as it is part of the same solution) to the new WindowsPhone Class Library Project you created in Step 2.
5) Enter the following code into some function in the Shared Project:-
PhoneClassLibrary1.MyWindowsPhoneClass1 o;
o = new PhoneClassLibrary1.MyWindowsPhoneClass1();
string strResponse = o.SayHello();
and compile.
You will notice you will get compilation errors for iOS and Android, as PhoneClassLibrary1 could not be found.
However....
If you now change the code to:-
#if WINDOWS_PHONE
PhoneClassLibrary1.MyWindowsPhoneClass1 o;
o = new PhoneClassLibrary1.MyWindowsPhoneClass1();
string strResponse = o.SayHello();
#endif
You will see that your project now compiles on ALL 3 platforms due to the use of conditional #if statements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With