Managing nuget packages for .net standard xamarin forms project - xamarin.forms

I am converting xamarin forms pcl to .net standard. In the project I use skia sharp and azure mobileservices.
My understanding was that if I reference these libraries in the .net standard class library and then add a reference to this .net standard class library in my android/ios/uwp projects all would be fine.
However it seems that I need to add the nuget packages for skiasharp and azure mobileservices to each project (android/ios/uwp). Is this indeed the case? if so, how is this better than using PCL?
I got the android project working, but I needed to add all the nuget packages to the android project.
I got the uwp project working without any nuget packages, which confuses me even further.

Using PCL and .NET Standard are basically the same - both are just API contracts or a subset of the full .NET Framework. But there is a MAJOR advantage to .NET Standard: .NET Standard has far more APIs - especially if you are using 1.3+ (netstandard1.3 has the System.IO APIs which aren't available in PCL).
With regards to NuGets, you typically still need to reference the NuGet package in all the app projects because some platforms have different implementations to take advantage of platform features. Think of .NET Standard as a subset of all the platform APIs - this is why SkiaSharp works just fine for some platforms. However, Android and iOS have a different implementation to take advantage of platform features.
Although you aren't doing Android development directly (since you are using Xamarin.Forms) you wouldn't have noticed something. If you JUST include the .NET Standard package, you get the basic SkiaSharp that can, for example, convert between a Xamarin.Forms color and a SkiaSharp color. However as a result of including the package in the android app, you can additionally convert from an Android color to a SkiaSharp color.
Personally, I always include all the NuGets into the app projects just to be safe.
Here are some docs that may help you:
https://learn.microsoft.com/en-us/dotnet/standard/net-standard

Related

.NET Standard over PCL Xamarin.Forms.Core project

I'm reading that .NET Standard is now preferred over PCL class libraries to share code.
My question is should I use a .NET Standard library over a PCL for the core of a Xamarin Forms solution? Currently, it will target iOS and Android, but we are looking to Tizen for TV in the future.
You can also choose the best of both world by using .Net standard and the PackagetargetFallback attribute in order to be compatible with libraries not officialy compatible with it:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageTargetFallback>portable-net45+win8+wpa81+wp8</PackageTargetFallback>
<PropertyGroup>
<!...>
.NET Standard will be the future, so if you can you should get on board now.
But be aware. If you're planning on using NuGet packages you could run into the situation that a package (or library from another source) does not support .NET Standard yet. Or the other way around is true as well. There are libraries that only support .NET Standard as of now, so you should either install an older version (with all the risks that come with it) or find an alternative path.
Also note; a lot is happening now in .NET Standard (and Core for that matter) land so be prepared for bugs, rough tooling and not all platforms being supported just yet.

.net standard library with HttpCient for xamarin forms

I was trying to create a common library for using HttpClient to consume an API. I was planning to make it in .net standard so as to share it with my xamarin.forms project right now and any future ones. However the highest version of .net standard I managed to use was 1.0. HttpClient needs standard2.0. I have included the latest version of .netstandard nuget into my xamarin.forms.
I have noticed that xamarin.forms runs in net4.5. According to the documentations the max I can go with this is standard1.1. Is there any work around around these to a .net standard common project or is my only option to make a .net framework project/xamarin portable library?
I used to be in the same situation and I managed to pull through this. First I tried to upgrade my current PCL project to .netStandard, but it was always failing, so I decided to create a new Project A Class Library (.NETStandard), moved all my files to the new project and re-install all nuget packages.
New project configuration:
Target Framwork: .NET Standard 2.0
Output type: Class Library
Make sure you're using Microsoft Visual Studio > version 15.3
Hope it helps.
I did manage to get HttpClient working in .net standard after manually importing the class. It needed an additional reference which is why it threw an error when I downgraded from .netcore.
This seems to work sometimes and throws reference errors other times. These errors go when I close and re-open visual studios.

Project not compatible with netcoreapp2.0

I'm trying to add a full framework class library as a project reference to asp.net core 2.0 MVC project and getting the below error.
Project XYZ is not compatible with netcoreapp2.0 (.NETCoreApp,Version=v2.0).
Project XYZ supports: net462 (.NETFramework,Version=v4.6.2)
I have updated to the most recent version of Visual studio i.e, 15.3.5.
Is it even possible to reference 4.6.2 libraries in core 2.0 projects?
The first thing that you can try is to compile the library you want to consume as netstandard2.0.
Theoretically (according to the .net standard documentation), this will make it compatible with projects using net461 and later as well as netcoreapp2.0 and later.
In practice, sometimes you will end up with a problem with one of your dependencies that don't provide the same library version across different compilation targets.
In such cases you may simply need to add the .net core 2.0 as a target framework for the XYZ library.
The xml tag listing the targets is <TargetFrameworks> in the XYZ.csproj file and is not handled by the Gui of the project's properties.
So I would give a try at editing the XYZ.csproj by hand and add or replace what's listed as <TargetFrameworks> with netcoreapp2.0.
If you are adding it as additional target you need to separate them with ';' as in
<TargetFrameworks>net462;netstandard2.0;netcoreapp2.0</TargetFrameworks>
More details about this in this Microsoft doc.
Please keep in mind that this will trigger multiple compilations and will slow your build consequently...
It should be. Microsoft announced a ".NET Framework Compatibility Mode" with the release of .NET Standard 2.0. However, they didn't go into great detail about how it works exactly, or what to troubleshoot if it doesn't. Additionally, they only specific talk about it in relationship to Nuget packages, so it's possible there's some role Nuget is playing in the process, as well. Unfortunately, I've been unable to find any additional information about this feature outside of the announcement post.
That said, Microsoft's explicit recommendation is to not rely on the fact that your .NET Framework library may just happen to work in .NET Core; instead, you should be actively porting .NET Framework libraries you control to .NET Standard. I'd say you're likely going to spend more time trying to figure out why it doesn't "just work" than you would porting your code, so that it will definitely work, and be future-proof to boot.
The following solution worked for me.
Deleted bin and obj folders from all the projects in the solution, rebuild and if it still doesn't work try changing browser from debug options. for eg. If you already have chrome as default browser in Visual studio, switch to Edge or Firefox.

Entity Framework Code First Approach to Xamarin.Forms PCL app

Can I have a .NET Class Library project with Entity Framework 6.1.1 (in windowsazure.mobileservices.backend.entity nuget package) work with Xamarin.Forms PCL app? Is this achievable? If not what are the possible complications?
Question Background:
I am working on a Xamarin.Forms cross platform PCL app with Azure Mobile Services back-end, targeting Android, iOS, Windows 8.1 and WinPhone 8.1. I actually dont care about Windows 8.1 but Visual Studio does not allow me to remove this target. So it stays.
Azure Mobile Services is setup, but I need the tables to be generated automatically (strongly avoiding manually typing table definitions in azure portal). I decided to go EF code first way with windowsazure.mobileservices.backend.entity nuget package to achieve this. However, the Xamarin.Forms PCL project does not support additional assemblies.
I am thinking of adding this package into a separate class library project and referring to it from the PCL project.
Thanks for your help!
The EntityData type is the required superclass of DTOs on the server SDK. Unfortunately, it has dependencies that are not supported in a PCL project, so it has to be used with a full .NET 4.5 project.
You could instead use partial classes to share DTOs between client and server. Just put all of the non system property types in a common code file that's shared between client and server. Then, on the client, add the system property implementation (ID field, UpdatedAt optionally, Version optionally). On the server, add another partial class definition that inherits from EntityData.

Unity dependency injection for UWP

I'm creating a UWP program using the PRISM 6 library from GitHub which has support for using Unity for dependency injection. However the current release of Unity (including the latest code in GitHub) has no mention of UWP support.
Does anyone know if the current Unity release is compatible with UWP projects?
The current Unity release (4.0.1) is compatible and working on UWP.
Edit: Due to project restructuring, samples have moved to a separate repository. One of the samples that is using Unity is the SplitView sample. Simply derive your app from PrismUnityApplication.

Resources