What kind of project I should create for Xamarin.F

2019-05-28 10:43发布

问题:

I have to create app as following.

1- App should work with Android and iOS.

2- Resolution should support to Mobile and Tabs

3- Data storage is on cloud. So, Restful API call will be there for data transactions

4- There might be local storage for configuration settings

5- There might be native code for some functionalities like implement UrbanAirship Notification, Facebook etc

When I create project for Xamarin.Forms, I get 2 options to create application:

1- Shared, 2- .Net Standard.

I have tried with both. One point is that I haven't found library that contains name like .Shared or .Portable that I have found in one of my old project.

Here is comparision. Please suggest me which type of project I should create for my next project.

Thank you.

回答1:

At the end it boils down to a matter of taste. The main difference is

  • shared projects are included in your platform projects as if the source code files were part of the projects themselves
  • .NET Standard projects are compiled to their own assembly

.NET Standard superseded the portable class libraries. The latter are deprecated and should not be used anymore. There would be no reason for anyway, since .NET Standard is way more convinient and supports a greater subset of .NET (effectively - depending on the PCL profile, but that one compatible with Xamarin was quite limited, e.g. there was very little support for the classes from System.IO).

There are pros and cons both for .NET Standard and shared projects. In shared projects you can use classes directly in your platform projects (anyway, this might be not advisable if you are trying to develop SOLID code), but you'll have to to use #ifdefs to use code that is compatible with only one of the platforms. With .NET Standard you may need to write classes containg platform dependent code twice and load them via DependencyService or - even better - a fully fledged IoC container, but therefor they are much cleaner and concerns are seperated much better.

In the times of PCL there were good reasons to go a hybrid way, e.g. to use System.IO.File from a class that was directly compiled in your platform projects, since both support it, but the PCL Xamarin Profile did not, but with .NET Standard this ain't necessary anymore.

I would always go for .NET Standard libs for the reasons mentioned.



回答2:

.NET Standard is the future and all plugins and libraries will need to follow and supported it sooner or later.

As in the new prerelease of Xamarin forms 3.0:

We’re now building with .NET Standard 2.0, using the magic of multi-targeting, while still supporting the PCL profiles and other .NET Standard versions you may need. Notice how much faster your projects load when you use .NET Standard project types!

Update To Xamarin.Forms 3.0 prerelease

.NET Standard will provide many features out of the box. Developers are now upgrading their solutions and plugins to support that standard.



回答3:

.Net Standard. is a new PCL. Use it as if it were a PCL.