Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin - Type not found in xmlns clr-namespace

I am making a Xamarin Forms app, the solution is called RESTTest, my shared project is called RestApp.

In my shared project I have a folder called ViewModels, which contains a class called MainViewModel.cs

I have a page called MainPage.xaml which has a code-behind called MainPage.xaml.cs. In my XAML I am trying to include my Viewmodels folder like this:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:RestApp"
             x:Class="RestApp.MainPage"
             xmlns:ViewModels="clr-namespace:RestApp.ViewModels;assembly=RestApp">

But when I add binding to my page like this:

    <ContentPage.BindingContext>
         <ViewModels:MainViewModel />
    </ContentPage.BindingContext>

I am getting an unhandled exception:

Type ViewModels:MainViewModel not found in xmlns clr-namespace:RestApp.ViewModels;assembly=RestApp

What am I missing?

enter image description here

like image 383
Barney Chambers Avatar asked Jun 21 '17 00:06

Barney Chambers


2 Answers

Removing the ";assembly=RestApp" in the namespace, and setting the linker behaviour to "Link SDK Assemblies Only" worked for me and solved the problem!

like image 83
Jordi Tormo Avatar answered Oct 14 '22 16:10

Jordi Tormo


This usually happens when you have Linker optimizations that trim unused code.

Note that deserialization into a type may not be detectable by the linker as a type use.

In the properties of your project find the "Linker Behavior" option under "iOS Build" or "Android Build", and set it to either "Link SDK assemblies only" or "Don’t Link". You'll need to clean+rebuild your whole solution for the change to take effect.

like image 40
Sten Petrov Avatar answered Oct 14 '22 18:10

Sten Petrov