While dealing with creating a Portable Class Library out of current code for a project, some workarounds are fairly obvious and some are problematic.
System.IO.Directory is non-PCL and I still need to be able to create a directory before creating files inside them.
How do you create a folder in C# without being able to call Directory.CreateDirectory(..)?
There is no built-in file and directory I/O support in PCL, since this functionality differs from platform to platform. However, to circumvent this issue you could reference PCLStorage in your portable class library project.
PCLStorage provides a portable abstraction layer library for file and directory I/O that you would reference in your portable class library. In your platform-specific application implementation, you would incorporate the corresponding implementation library of this abstraction layer.
PCLStorage is applicable to .NET Framework 4 and higher, Silverlight 4 and higher, Windows Phone 7.5 and higher, and Windows Store apps. It relies on async and await, which means that it is dependent on the BCL Async package when used e.g. with .NET 4, Silverlight and Windows Phone 7.5.
You might also want to have a look at the MvvmCross File plug-in. MvvmCross is portable "by nature" and the File plug-in provides relevant file and directory I/O functionality as synchronous methods. MvvmCross portable libraries are currently applicable to .NET Framework 4.5, Silverlight 4 and higher, Windows Phone 7.5 and higher, Windows Store apps, Xamarin.iOS and Xamarin.Android.
Related
I have developed a UWP class library , and it is already used in various projects.
Now I want to make this UWP class library to support WindowsAppSdk apps too.
When I try to refer UWP Library in WindowsAppSdk app, getting error like "Project is not compatible".
My UWP class library min supported version - Windows 10 fall creators update (16299).
Should I recreate my UWP Library as WindowsAppSdk library ?
Can anyone help me understand this
Thanks
Noorul.
Should I recreate my UWP Library as WindowsAppSdk library ?
No, but you should convert it into a .NET Standard library that can be consumed by both UWP and WinUI (.NET) applications, and any other type of application whose runtime implementation supports the version of the .NET Standard specification that you choose to target.
UWP apps cannot consume librararies that are targeted against a specific platform such as .NET and vice versa. That's where .NET Standard comes in. It enables you to share code across multiple .NET implementations and platforms.
I have PCL Library and I want to add it to standard (.net 4.6) C# console application. Everything is fine as long as I don't use any PCL specific classes inside the library. And if I do, I get an error "unsupported PCL profile". This error is not googlable. But the same library works fine in UWP application. I am searching for a solution or official explanation why I can't use PCL in non UWP application.
Yes you can. PCL is basically intersection of available API's across different platforms. The disadvantage is that the more target platforms you choose the smaller is the intersection:
Another disadvantage of PCL is that it generates separate assembly for each platform.
That's why Microsoft comes with .NET Standard - a replacement of PCL that uses different approach.
Think about .NET Standard as an interface, that defines set of API's. Then the platforms like .NET Framework, .NET Core, Xamarin.iOS, Xamarin.Android will implement the .NET Standard.
interface NetStandard1_0 {
}
interface NetStandard1_1 : NetStandard1_0{
}
interface NetStandard1_2 : NetStandard1_1{
}
net46: NetStandard1_6 {
}
dnxcode46: NetStandard1_6 {
}
As a result, you won't target specific platforms, but a version of .NET Standard instead. When your library targets .NET Standard, it can be used in any platform that implements the .NET Standards. Another advantage is you wont need separate assemblies for different platforms anymore. There will be single assembly that runs just everywhere.
However, I recommend you to wait until April 2017 when .NET Standrard 2.0 should be released. Microsoft promised that all platforms (.NET Framework, .NET Core, Xamarin.iOS, Xamarin.Android) will support this version of .NET Standard and it will have official support in Visual Studio. Also, Visual Studio projects using project.json will be converted to .csproj, so all Visual Studio projects will use the same format and it will solve a lot of compatibility issues. Cleanning of the mess that appeared in .NET last years was absolutelly necessary
Sure you can.
Just add .NET 4.6 to selected platforms:
It's appears at time when you create PCL.
More information here:
Cross-Platform Development with the Portable Class Library
Or you can change platforms in existing PCL. Just go to properties page and you will see:
Here is a good blog post about how to call UWP API from desktop App:
How to call UWP APIs from a desktop VB/C# app
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/
I hope isn't a stupid question...
Is possible to load a no silverlight assembly into a silverlight project without using a COM object?
Thanks in advance.
You can use the Portable Class Library (PCL)
Use this project to write and build portable assemblies that work
without modification on multiple platforms, such as Windows 7, Windows
8, Silverlight, Windows Phone, and Xbox 360. For example, you can
create classes that contain shared business logic for desktop apps,
Windows Store apps, and mobile apps, and you can then reference those
classes from your apps.
The Portable Class Library project supports a subset of assemblies
from the .NET Framework, Silverlight, .NET for Windows Store apps,
Windows Phone, and Xbox 360, and provides a Visual Studio template
that you can use to build assemblies that run without modification on
these platforms. If you don't use a Portable Class Library project,
you must target a single app type, and then manually rework the class
library for other app types. With the Portable Class Library project,
you can reduce the time and costs of developing and testing code by
building portable assemblies that are shared across apps for different
devices.
Also, Microsoft's Scott Hanselman has written a few articles about the PCL, this one should be a good starting point to find the resources you need to get started: Cross-Platform Portable Class Libraries with .NET are Happening
Is it possible to use Google APIs Client Library for .NET in a WP7 project?
I've tried to add a new reference to DotNetOpenAuth.dll and I'm getting the folowing error.
Reference cannot be added to DotNetOpenAuth.dll because it was not built using the Windows Phone runtime. Windows Phone projects will work only with Windows Phone assemblies.
Is it possible to solve?
Try Portable Library Tools from MSDN. Read about them here. Now reference Google .Net library in the portable library, also reference this portable library project in WP7 project. Now, write a small wrapper over the Google apis in the portable library project and use them from your WP7 project. Referencing works but I did not use it, you can give it a try once.
I would think it would be possible to use the Google APIs from WP7. I've not used them myself, but it looks like you use them by making HTTP requests which, if memory serves me right, is possible on WP7.
With regards to the DotNetAuth.dll it is more then likely built against the Full or Client Profile version of the .NET Framework and therefore can't be used by WP7. In order to do this, then a WP7 specific version of the assembly will be have to be compiled.