Visual Studio 2017 - Cross Platform App - No PCL - c#

I decided to give Cross Platform Apps a go. I have been searching for tutorials for hours. A lot of them look interesting but i hit a problem at the start every time. Every tutorials tells me : File -> New Project -> Visual C# -> Cross-Platform -> Cross-Platform App (Xamarin.Forms). When i do this, i have to choose some settings, including "Blank App" and the Code Sharing Strategy. And that's where it goes wrong.
I should be able to choose Portable Class Library but it's not there. I can choose between "Shared Project" and ".NET Standard". Did I forget to install something or has the Code Sharing Strategy changed?

With Visual Studio 2017.5 they removed the option to create PCL option from the cross platform app. The standard class library is a better option than the PCL because the PCLs only allowed you to use the framework functions all the targeted platforms support. With standard libraries you have a list of functions it must support on all the platforms so you have much more available to you in the standard library than a PCL

Related

.Net Standard vs PCL Xamarin Project

I want to learn Xamarin on VS 2017. I can't find the same project as I see in most of all tutorials or guides. I have ".Net Standard" project and not Portable Class Library project. (see images).
[]
[]
I have installed all components of vs2017 but I can't see it.
[]
Why I can't see it? Are they the same?
(edit: the xamarin official site have 3 options, so I don't think they are)
Thanks
.NET Standard is the next evolution of Portable Class Libraries. Simply, they are PCLs but better. Choose a .NET Standard project and go through the tutorials as though you had used a PCL. When just starting out, you probably won't notice a difference until you start adding a lot of nuget packages.

Using F# portable class library from C# portable library

I have a WPF application that uses a F# library for calculations. I'm trying to refactor so that I have WPF Application -> C# Portable Class Library (models) -> F# Portable Class Library (calculations).
My end goal is to use Xamarin Forms to put a UI on it that I can use on other platforms. This is currently mostly an experiment to assess the viability of the approach.
Every time I try to add a reference to the F# library from the C# library, I get an error message that says "Unable to add reference to X, Portable class libraries can only reference other portable class libraries".
I can reference either library from the WPF project and I've tried every permutation that I can think of to make it work.
There are quite a few similar questions, but they either don't have an answer or state that it is fixed in VS 2013. It doesn't seem to work in VS 2015 Update 2.
Does anyone have a link to a sample that works, or a workaround to get past the problem?
Steps for VS 2015 Update 2:
Create new(tested with new solution) Class Library (Portable)
As target choose only Windows 10 or Windows 8.1(maybe other target would work too - haven't tested though)
Add F# Portable Library(tested with .Net 4.5, Windows Store, Xamarin
and with .Net 4.5, Windows Store, Silverlight 5, Xamarin)
Now you can reference F# library without any errors
Now You can change properties of C# project(Right click on c# project and
choose properties) to match the targets You would like to have (Section Library, subsection Targeting)

Is there any way to share code between UWP apps and WPF apps?

To be clear, I follow the MVVM pattern, and I want to structure my project such that I can share my model code between a UWP app and a standard WPF app. The code I want to share has no UI. I don't relish the thought of finding new tools to replace the ones that I've been using for years that take care of certain tasks like logging, connecting to a document oriented database, etc.
I attempted to start writing a UWP wrapper around some code I already have and reference the model project directly. Visual Studio refused to let that happen, showing me an error message that says "Unable to add a reference to project 'ACK.Model'". The same thing happened when I attempted to put the model in a Universal Library and reference it from a WPF app. I'm not trying to share WPF code. Just the model layer that has no reference to UI libraries.
This is a scary proposition, because it means that if I want to do anything substantial I have to choose to either jump 100% to UWP or stay 100% WPF. NewtonSoft.JSON might have a universal distribution (ASP.NET MVC), but what about ElasticSearch.NET, and other tools needed to make important apps?
I found where the "Portable Class Library" project type was hiding. PCLs will allow me to share my code across WPF and Universal apps as that was one of the options. This solves the simple case of the Model part of my code, but I (still) can't use some of the libraries I want. There are still a large number of libraries that I need that do not have PCL available.
About a year later, with the advent of Visual Studio 2017 there is a more complete solution. If you target your libraries to .Net Standard then the library is compatible with both .Net Core apps and the monolithic .Net targeted app. The support for standard .Net libraries and APIs is fairly complete, as is the support for modern C# language features.
The general advice now is this:
Target .Net Standard for all libraries
Target the appropriate platform for your actual application. (UWP or WPF).
NOTE: if your library has to interact with C libraries or applications, you have to take extra care to make sure you load the correct version.
It appears that there is a solution, but it has to be adopted by the whole tool chain you want to use. When Microsoft introduced Windows Store apps in Windows 8, they also introduced a Portable Class Library (PCL). The purpose of the PCL is to share code between different parts of your application.
When you create a PCL in Visual Studio 2015, you can specify the types of APIs you want it to be accessible from:
Universal Apps
Mono
.Net Core 5
.Net 4.6
This of course, limits the APIs available to you but most of the ones you want to use are OK as long as it's not UI related. There are other limitations as well:
Your project can only be edited in Visual Studio 2015 or greater
You don't have access to special directories from the Environment variable (i.e. user Documents directory, etc.)
You can't link to a library designed for only one of your target platforms (i.e. libgit2sharp, etc.)
There's no way to browse the API for this subset--MSDN needs to get on the stick. MSDN has updated much of the API documentation, but it's still difficult to figure out what applies to your PCL
However, you can link any library designed for a single target platform to your PCL. It's not ideal, but it's better than nothing.
The ASP.NET MVC stack has been ported to using PCLs, so you can use NewtonSoft.JSON directly as well as any other of those libraries used by that application. However, there are several libraries that have not been ported.
This arrangement forces you to think about how you want to integrate better. The .Net Core 5 seems to be stable, but support is in it's infancy. The current generation of Universal Apps as of VS 2015 update 1 uses .Net Core 5 directly.
There are several features from Nuget that are not currently supported even though work is under way:
MS Build extensions (major changes to MSBuild and the project.json structure)
Install/uninstall scripts (related to the removal of the concept of install)
Content (related to install/uninstall, but work is in progress on this)
Content transforms (related to lack of install/uninstall)
I wish I had a more complete answer. But this is as far as I got once I discovered the PCL and how it evolved for the current infrastructure.
I'm in the process of creating a game creation toolkit that incorporates version control right off the bat. I want to be able to deploy a game as a Windows 10 app, or as a standard WPF app, but due to the libraries I'm using to integrate version control I need to create the editor as a standard WPF app. I had to be a bit creative in building the shared code and importing the correct libraries.
First, my project hierarchy:
Project.Model (Portable Class Library)
Project.Model.Versioning (standard C# library)
Mvvm.Toolkit (Portable Class Library)
Editor (Standard WPF application)
I want the core PCL to be able to load a project and deserialize the JSON encoded objects. The PCL did have access to System.IO, but surprisingly it is not the same as the one defined in the standard C# library. Here's how I had to fix things:
After adding the package reference to NewtonSoft.JSON, I had to change the target framework in the packages.config file:
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="portable-net452+win81" />
All projects dependent on my Project.Model class had to install the `system.io.filesystem' package from nuget so that the System.IO.FileInfo etc. objects were the same.
While this is definitely not a panacea, it's also not a dead end. I'm sure there are more gotchas, but this will at least help with some of the problems.
a .NET Standard library could be used to share a Model-View-ViewModel architecture between a WPF project and a UWP project.
https://www.syncfusion.com/ebooks/xamarin-forms-succinctly/sharing-code-among-platforms
https://devblogs.microsoft.com/dotnet/announcing-uwp-support-for-net-standard-2-0/

Monodevelop "Portable Library" project References are not available

Greetings,
Recently i come cross a problem while developing a cross platform project.
In this project, I have to support android, ios, and wp7, what I did is to create 3 projects for these 3 platforms, and I have another project which can be shared among the 3 target platform.
So I created this project as a "Portable Library", and add reference to this project in the other projects. It worked, I can use Class defined in this shared project in the other 3 projects, but the problem is, in the portable library project, the "References" folder, "System", "System.Core", and "System.Xml" are red, when I unfold it, it says "assembly not available for .NETPortable 4.0 Profile1 Profile (in Mono 2.10.9)". I googled this piece of information, but none gave me the satisfying answer.
http://monotouch.2284126.n4.nabble.com/cross-platform-library-code-td4656600.html
So, how can fix it? What are these "System", "System.Xml", "System.Core" for? Are they files? Or within a file?
You can ignore the references being red for now. That just indicates that you don't have the PCL assemblies on your system (which you won't have unless you are on Windows), however Xamarin is working on generating PCL assemblies and hopes to have them "soon" (by May?).
In the meantime, this is "ok" because when you are targeting Mono for Android or MonoTouch on Mac OS X w/ MonoDevelop, what MonoDevelop does is link with the Mono for Android or MonoTouch frameworks instead, which is good enough for what you want do to.
Things to consider:
You won't be able to share the assembly with your WP7 app (because the assembly built by MonoDevelop won't be a true PCL assembly), but you'll be able to share the Portable Library project - i.e. just make sure to do a fresh build of the project for each platform. (you can share between MonoTouch and Mono for Android, but not with WP7)
Since the referenced assemblies are red, as you've noticed, it means that MonoDevelop won't be able to do proper code-completion. If you're a Windows guy, you could just develop the Portable Library in Visual Studio and then just share it with your iOS and Android projects. Once Mono has real PCL assemblies, this problem will go away.
There are some bugs in the current public releases of MonoDevelop and Xamarin Studio where it doesn't like certain profiles. Both of the following custom builds address this issue, depending on whether you want MonoDevelop or the sexier Xamarin Studio:
https://files.xamarin.com/~jeff/MonoDevelop-3.1.1.dmg
https://files.xamarin.com/~jeff/XamarinStudio-4.0.2442.dmg
Keep in mind that neither of these custom builds have been QA'd. the XamarinStudio link was just sent off to the QA team last night (hot off the press!) for what will become Xamarin Studio 4.0.2 (once any regressions / critical bugs that QA turn up in that build have been fixed).

I want to use a regular class library on XBox360!

why can't I use regular class libraries in XBox360 games?
I have application logic which I want to keep independent from XNA and use in both WPF and XNA applications.
Does anyone know good practice to share code between XBox/Phone7 applications and "regular" windows applications?
Have a look at Portable Class Libraries: http://msdn.microsoft.com/en-us/library/gg597391.aspx
Using the Portable Class Library
project, you can build portable
assemblies that work without
modification on the .NET Framework,
Silverlight, Windows Phone 7, or XNA
(Xbox) platforms. Without the Portable
Class Library project, you must target
a single platform and then manually
rework the class library for other
platforms. The Portable Class Library
project supports a subset of
assemblies from these platforms, and
provides a Visual Studio template that
makes it possible to build assemblies
that run without modification on these
platforms.
Edit: Check out JoDG's answer for a nicer solution.
You could try linked files...
In your Xbox project, right-click -> Add Existing File, then after selecting the file(s), click the little drop-down arrow on the "Add" button and click "Add As Link", as shown here:
This might not work if you need to share more than just files, but it's an easy way to share code.
JoDG's answer is probably the most practically useful. But just to provide additional information:
why can't I use regular class libraries in XBox360 games?
Because the different XNA platforms target different versions of the .NET framework.
XNA Game Studio itself provides a mechanism (documented here and more info here) for automatically mirroring the source files of a project between two projects. This mechanism can be used for creating copies of your library projects for each platform, as well as for your game projects.
For XNA-related work, this is the preferred method for creating cross-platform libraries. When you have to also make your library work on WPF you have to take additional steps:
On Windows, an XNA game is just like any other .NET application, and a XNA library for Windows is just like any other .NET library. Except for the fact that they reference XNA assemblies. So your WPF application can reference a Windows version of your XNA library. But if you want your application to work on systems without XNA installed, you need to remove the XNA assembly references from the Windows project for that XNA library.
XNA Game Studio will still mirror changes between the Windows library and the Xbox 360 library for you.

Categories