Xamarin project failing to find SkiaSharp in Android or Windows - xamarin

I confess I'm new to Xamarin and cross-platform development, so apologies if this seems a trivial question, but I've been trying to get to the bottom of it for several evenings now.
I have a basic Xamarin Forms application, which runs in Windows 8.1, Windows UWP and Android. It uses SkiaSharp for some basic graphics (based on the Skia example).
It was running in all three platforms, but I had to remove and re-add the Xamarin packages to the project and I can't get SkiaSharp to run properly any more in Windows or Android. I get a BadImageFormatException:
Additional information: Could not load file or assembly 'SkiaSharp, Version=1.56.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756' or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)
I get this exception in the x86, x64 and Any CPU configurations for the Windows 8.1 version, and from the Android version.
If I run it the UWP project, it works perfectly.
Relevant project structure:
MyProj.XMForms (Portable) - PCL containing the core Xamarin.Forms App, MainPage with a StackLayout containing a Frame with a MyView, and MyView which contains a Grid with an SKCanvasView.
MyProj.Windows (Windows 8.1) - wrapper - just contains an empty App and a MainPage which loads the MyProj.XMForms App.
MyProj.UWP (Universal Windows) - wrapper - just contains an empty App and a MainPage which loads the MyProj.XMForms App.
MyProj.Droid (Android) - wrapper - just contains a MainActivity which loads the MyProj.XMForms App.
All projects NuGet packages have Xamarin.Forms 2.3.3.180, SkiaSharp 1.56.0 and SkiaSharp.Views.Forms 1.56.0 . MyProj.Droid also has SkiaSharp.Views (none of the others will accept it).
I have discovered https://github.com/mono/SkiaSharp/issues/190 and added <ShouldIncludeNativeSkiaSharp>true</ShouldIncludeNativeSkiaSharp> to the csproj without making any difference.
I have tried removing and re-adding the SkiSharp NuGet packages.
What am I missing?
Thanks,
Ian

If you look at the exception message, there is this line:
Reference assemblies should not be loaded for execution.
That means you haven't installed the SkiaSharp NuGets into the app projects. Make sure all the apps have the SkiaSharp, SkiaSharp.Views and SkiaSharp.Views.Forms NuGets installed. The core project only needs the SkiaSharp and SkiaSharp.Views.Forms NuGets.
Also, Windows 8.x is not currently supported due to a limitation in Skia: https://github.com/mono/SkiaSharp/issues/20

Okay, I've managed to get Android working by taking the Android project from the SkiaSharp FormsSample and replacing the call to
LoadApplication(new SkiaSharpSample.FormsSample.Platform.App());
with
LoadApplication(new MyPortable.XamarinForms.App());
My original project definitely included all the same NuGets as this one, and I haven't yet identified they key difference which meant that this one worked and mine didn't, but at least I now have a solution which works in both Android and UWP.

Related

UWP store build with Xamarin Forms

We are having trouble publishing our x86/x64 UWP Xamarin Forms app to the Microsoft Store.
The app got rejected because the certification team cannot test the .appxupload package because it crashes on startup.
The app only crashes when installed from the store built .appxupload package. The .appxbundle in _Test works fine and we are using it for in-house distribution and sideloading via HockeyApp.
At first we assumed that the crash was due to faulty packaging as our app uses the desktop-bridge feature to bundle a small WPF executable with it. Our first instinct was to remove the desktop bridge part and test the packaging again. After removing it, we had the same issue, the .appxupload crashed on start.
We took it one step forward and just created a new solution with a new Xamarin Forms Template project (VS 15.7.1). Without changing a single line of code we packaged the visual studio xamarin forms uwp template project for store upload. The surprise here is that the generated .appxupload also crashes exactly the same way on startup.
We decided to find out more by experimenting on the Visual Studio XF uwp template:
Update XF to 3.1 -> still crashes
Update .NETCore.UniversalWindows to 6.1.5 -? still crashes
Remove XF from the project completely (including Xamarin.Forms.Init(..)) -> no more crash, but also no more content without Xamarin forms ... :(
We have researched the following posts which seem to be related to this issue:
https://bugzilla.xamarin.com/show_bug.cgi?id=59538
https://developercommunity.visualstudio.com/content/problem/150965/xamarin-forms-project-uwp-app-dies-on-start-when-i.html
Could not load file or assembly 'System.Private.CoreLib...'
Some of the above posts were explaining that even though the .appxupload crashes when installed locally the when downloaded and installed from the Microsoft store the package will run without a problem. Our problem with this part is that our .appxupload package needs to be tested by Microsoft before the store publish because of the desktop bridge permission. Because it crashes when locally testing it cannot be published.
Based on our experiments with the Visual Studio Xamarin forms uwp template project this seems to be an issue of UWP and Xamarin Forms together.
We are not sure how to proceed. We need to get the app running from the .appxupload package so that it can be tested by the store team. Do you have any ideas/solutions for this issue?

Move from Xamarin to native(Swift/Kotlin)?

We currently have an app in Xamarin, which is Xamarin.Android and Xamarin.iOS with a shared PCL (no Xamarin.Forms). It currently is a very complicated app and we would like to remake screen by screen the app, and move away from Xamarin to a Swift and a Kotlin app in the meantime.
Is this possible? We don't have the time nor money to remake the app completely at once.
I have looked into making new screens as external libraries, and then bind that library, but I think that's making it a bit too complicated.
you can reuse the assets and resources only but not the code because you are using Xamarin.Forms not Xamarin.Android + Xamarin.iOS.
in your project you don't have any android XML design files and no iOS storyboard even the logic is different with the .cs files because you are not using the Android Java SDK or iOS SDK directly.
If you were use mono and mono touch directly without using the PCL (Xamarin.Forms) project then you can rewrite the code with minor changes with names of method, properties... etc.

Multiple errors after updating Xamarin Forms

I'm using Visual Studio 2017, and I stared a new Xamarin Forms app:
Create New project - Cross Platform App (Xamarin)
Blank App, and I chose Portable Class Library (PCL)
The app compiles and runs.
Now I right click the solution and choose Manage Nuget packages for solution, and it tells me I'm running version 23.3.0 and I should update to 25.4.0.1
After the update and 3 VS reboots, it gives me a host of errors I have been trying to solve for an hour including googling.
All theme related.
I tried removing the styles.xml file all together. I removed the theme from the reference to the theme from the manafest and it keeps putting it back.
I can't figure out what I'm doing wrong. I have not written any code.
Xamarin Forms requires a specific version of the Android Support libraries. Do not update them.
Delete your project and create a new one.
It is possible to get round this limitation by ensuring you are using a specific version of Android and by updating all Android SDK's, but for a beginner I would recommend just using the support libraries that are installed alongside Xamarin Forms.

MvvmCross - 4.0 Beta 3 doesn't work with new UWP project?

I started a new Windows 10 UWP project today and used Nuget to add references for the beta version of MvvmCross. After adding references to MvvmCross and some HotTuna plugins (like StarterPack), all I get is the following:
I seem to be missing some stuff because I'm unable to inherit from MvxStorePage. Also, when I added HotTune to my Android project, a ToDo.txt file was added with instructions, but in the UWA project, I didn't get any of those extra files.
Am I doing something wrong?
I don't think you can inherit from MvxStorePage, because it doesn't exist in the namespace available to UWP project.
In UWP project, the Cirrious.MvvmCross.WindowsUWP namespace is used. You can check it in Visual Studio in "Class View (Ctrl + Shift + C)". And the only available view is MvxWindowsPage as below.
Based on the Universal Windows App tutorial on GitHub, the MvxWindowsPage is recommended to be used in universal app. Note that, the tutorial is for Universal Windows app for 8.1, but it's same to make it work in a win10 UWP app. Just change the namespace from Cirrious.MvvmCross.WindowsCommon.Views to Cirrious.MvvmCross.WindowsUWP.Views. By doing that, I didn't get any problem to inherit from MvxWindowsPage.

Can not compile after migrating unified api in xamarin forms

We have migrated the xamarin forms project to unified api.
But we have the strange compile error like this:
Could not AOT the assembly '.../iOS/obj/iPhone/Debug/mtouch-cache/Build/Xamarin.Forms.Labs.dll
So I changed the settings in iOS project's options.
In ios Build Tab of Project options(iOS) I changed the "don't link" to "Link all assemblies".
But the app is crashed when it is started.
xamarin version is 5.7.1(build 17).
xamarin forms's version is 1.3.5.6337.
xamarin forms labs's version is 1.0.1.5.
Please help me if you have the experience with this error.
Replace Xamarin.Forms.Labs with XLabs.Forms.
There has been a major refactoring on how the XLabs project is structured so your existing project based on the 1.x.x will break. The reason for the refactoring was simple; separate functionality that does not depend on Xamarin.Forms into different assemblies. This allows the use of functionality like IoC, Serialization and even platform specific services without referencing XF.
To upgrade:
Uninstall any existing Xamarin.Forms.Labs packages from your
project.
Install XLabs.Forms package to your projects (PCL, iOS, Droid & WP). This will update Xamarin.Forms to the latest stable release (1.4.x)
Refactor code to reference the correct namespaces.

Resources