When I started this series, Xamarin and Xamarin.Forms did not fully support .NET Standard. The sample project for this series has still a portable class library, and as I wanted to blog on another topic, I got reminded that I never updated the project. This post will be all about the change to .NET Standard, focusing on the Xamarin.Forms project as well as the MVVMLight library, which is also available as a .NET Standard version in the meantime.
Step-by-Step
I have done the necessary conversion steps already quite a few times, and to get you through the conversion very quickly, I will show you a set of screenshots. Where applicable, I will provide also some XML snippets that you can copy and paste.
Step 1
The first step is to unload the project file. To do so, select your Xamarin Forms project in Solution Explorer and right click again on it bring up the context menu. From there, select "Unload Project".
Step 2
Next, right-click the selected Xamarin.Forms project again to make the context menu visible once again. From there select "Edit [PROJECTNAME].csproj" to open up the file.
Step 3
Now that the file is open, press "CTRL + A", followed by "DEL". Seriously, just do it. Once the file is empty, copy these lines and paste them into your now empty file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>
Step 4
This step is not really necessary, but I want to point one thing out. The old PCL-project type has listed quite a bunch of libraries in the packages.config file. These are all NuGet packages necessary to make the app actually run (they get pulled in during the install of a NuGet package as dependencies). Now that we are converting, we are getting rid of the packages.config file. You can delete right away in the Solution Explorer. We will "install" the needed packages (marked with the arrows) in
Step 5
For "installing" NuGet packages into the converted project, we are adding a new ItemGroup to the XML-File. The Package System.ValueTuple is referenced - because in this series' sample project we have some code in there that uses it. The absolute minimum you need to get it running is:
<ItemGroup>
<PackageReference Include="MvvmLightLibsStd10" Version="5.4.1" />
<PackageReference Include="Xamarin.Forms" Version="3.1.0.583944" />
</ItemGroup>
If you have other NuGet packages, just add them into this item group. They will get installed in the correct version if you follow this tutorial until the end.
You might have noticed that the MVVMLight package I am inserting here is not the same as before. This is absolutely true, but for a reason. Laurent Bugnion has published the .NET Standard version quite some time ago. If you want to read his blog post, you can find it here.
The second change I want to outline is DebugType
settings. These are also not set in that way if you create a new project that is already .NET Standard. In order to enable debugging of your Xamarin.Forms code, however, you absolutely should also add these lines:
<PropertyGroup Condition=" '$(Configuration)'=='Debug' ">
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)'=='Release' ">
<DebugType>pdbonly</DebugType>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
These properties provide the debug symbols and the pdb-files a lot of analytic services are dependent on. Simple put, copy and paste them into your project. Hit the "Save"-Button and close the file.
Step 6
Now that we have changed the project file, our already downloaded packages and created dll-files are no longer valid. To make sure we are able to compile, we need to delete the content of the obj folder in the Folder View of the Solution Explorer in Visual Studio.
Step 7
Do the same to the content of the bin folder and switch back to the Solution View by hitting the Folder View-Button again
Step 8
Now we are finally able to reload the project as the base conversion is already done.
Step 9
Trying to be optimistic and hit the Rebuild button will result in these (and maybe even more) errors. The first one is one that we can solve very fast, while the second one is only the first in a row of errors.
Step 10
To solve the assembly attributes error, just go to the Folder View again and select the Properties folder. Bring up the context menu by right-clicking on it and select the delete option. Confirm the deletion to get rid of the folder. The new project style creates the assembly information based on the project file during build, which is causing the errors.
Step 11
Now let's face the next error. As the MVVMLight .NET Standard version does no longer rely on the
CommonServiceLocator
like before, we are able to remove this reference from our ViewModelLocator
.
Step 12
Of course, we now can also remove the instantiation call for the removed
ServiceLocator
.
Step 13
In the ViewModel instance reference, replace
ServiceLocator.Current
with SimpleIoc.Default
. Hit the save button again. You might have more errors to fix. Do so, and finally save your ViewModelLocator
.
Step 14
After all the work is done, we are now able to compile the .NET Standard version of our Xamarin.Forms project. If you fixed all of your errors in the step before, you should achieve a similar result like me and get a success message.
Final steps
Now that the Xamarin.Forms project is built, you might want to try to build all other projects in the solution as well. The changed structure of the Xamarin.Forms project will have an impact also on the platform projects, that's why I absolutely recommend deleting the contents of bin and object folders there, too. This will solve you a lot of work to get things to compile again.
As always, I hope this post is helpful for some of you. If you have any questions, feel free to ping me on my social accounts or write a comment below. The next post in this series will involve again more code, so stay tuned - it will be out soon.
Please find the updated sample project here on GitHub.
Happy coding, everyone!
Posted from my blog with SteemPress : http://bit.ly/2IwywMM
This is an edited version of the initial post as it got messed up during automated posting.
You got a 13.64% upvote from @redlambo courtesy of @socialbutterfly! Make sure to use tag #redlambo to be considered for the curation post!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
This post has received a 0.24 % upvote from @drotto thanks to: @socialbutterfly.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
This post has been upvoted by @microbot with 5.0%!
You want higher upvotes?
Vote for my creator @isnochys as witness!
Did you know, that you can make some profit with your witness vote?
More profits? 100% Payout! Delegate some SteemPower to @microbot: 1 SP, 5 SP, 10 SP, custom amount
You like to bet and win 20x your bid? Have a look at @gtw and this description!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
You got a 33.33% upvote from @automation courtesy of @socialbutterfly! This is a service sponsored by @yehey. Please consider voting @yehey for Witnes. Use this short URL link https://on.king.net/witness simply click and vote, this will redirect to Steem Connect for secure connection.
Interested to earn daily? Delegate Steem Power to receive 90% payout rewards. Use this link https://on.king.net/automation to delegate SP to @Automation.
If you need an extra upvote, join us at https://SteemChat.com discord server.
Have a lovely day.
@Automation - Keep Steeming for a better future.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
You got a 10.48% upvote from @cabbage-dealer courtesy of @socialbutterfly!
Delegate today and earn a 100% share of daily rewards!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
You've received a lifting from @botox ! Consider delegating to earn passive income 20SP,50SP,100SP,200SP.
Tu viens de recevoir un lifting de @botox ! Envisager de déléguer pour gagner un revenu passif 20SP,50SP,100SP,200SP.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit