shady shady - 1 year ago 98
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download