9

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

Barney Chambers
  • 2,720
  • 6
  • 42
  • 78

3 Answers3

3

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

Jordi Tormo
  • 106
  • 7
1

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.

Sten Petrov
  • 10,943
  • 1
  • 41
  • 61
  • 1
    Hi Sten, In my `Linking` in `Android Options` it is currently set to `None` is that correct, I don't see a "Don't Link" option – Barney Chambers Jun 21 '17 at 01:07
  • @BarneyChambers yes, that's what it should be. If it still doesn't find it then in your main method in the android app where Xamarin.Froms.Init is called add 'object o = typeof(ViewModels.MainViewModel)' to make sure the type can be reached compile-time – Sten Petrov Jun 21 '17 at 01:10
  • I tried every linking permutation and unfortunately this didn't solve my problem – Barney Chambers Jun 21 '17 at 01:10
  • I added the hardcoded reference but am still seeing the error – Barney Chambers Jun 21 '17 at 01:17
  • check that 1. Action set on the file is Compile and 2. your shared project is referenced in your main project. 3. Is your assembly name RestApp? If you used PCL it would be but because it is shared project it actually may be RestApp.Android (not sure)? – Yuri S Jun 21 '17 at 01:58
  • Try to remove the ;assembly=RestApp in the namespace – Kedu Jun 21 '17 at 07:46
  • This worked for me. Was adding an OnScreenSize to parody OnPlatform and it just couldn't find it when it built. Wasted hours on this... I've added an init method to my library so it references all my xaml only classes. Suddenly it builds. Thank you. – Stephen Hewison Feb 01 '19 at 21:13
0
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:cvt="clr-namespace:XamarinTest.UI.Resources.Converts;assembly=XamarinTest.UI">
    <cvt:DoubleToImageConverter x:Key="CVT_DoubleToImage"/>
    <cvt:MediaTypeToImageConverter x:Key="CVT_MediaTypeToImage"/>
</ResourceDictionary>

In my app, I add the assembly=XamarinTest.UI solved the problem. I think the dev team may removed the assembly=xxxx section in Xamarin Forms 5.0.

Jim
  • 489
  • 5
  • 11