shady shady - 6 months ago 52
C# Question

Xamarin.Forms ViewModel in markup doesnt load dll

I'm trying to define a viewmodel class in the markup of my Xamarin.Forms App.xaml.

<xml version="1.0" encoding="utf-8" ?>
<Application xmlns=""
<viewModels:MainViewModel x:Key="MainViewModel"/>

I get this error:
Unhandled Exception:

System.IO.FileNotFoundException: Could not load file or assembly 'MyPCL.Client' or one of its dependencies.

but if i declare and initialize this class in my app.xaml.cs, it works fine. Why would this load in code behind but not in markup?


The Xamarin linker automatically tries to minimize the size of the app by removing libraries and classes that aren't referenced. Apparently declaring it in the XAML alone isn't enough to make the linker recognize that a reference exists (this may be a bug you should file with Xamarin). Adding a reference in App.xaml.cs forces the linker to retain it.

You can read more about the linker and its different settings here.