Forum Xamarin.Forms

Exceptions with ResourceDictionary in a ContentPage

Pierre-ChristopheDusPierre-ChristopheDus FRUniversity ✭✭✭


I try to reproduce a solution for Command Binding found here:

In the XAML of my ContentPage, I tried to reference the ViewModels repository throguh a ResourceDictionary:
-by sepcifiying directly the "ViewModels" folder in the namespace:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="" xmlns:x="" xmlns:vm="clr-namespace:DebugSample.ViewModels;assembly=DebugSample.ViewModels" x:Class="DebugSample.Pages.DebugPage"> <ContentPage.Resources> <ResourceDictionary> <vm:DebugPageViewModel x:Key="debugPageViewModel" /> </ResourceDictionary> </ContentPage.Resources> ... </ContentPage>
=> but this throws an exception: "System.IO.FileNotFoundException: Could not load file or assembly 'DebugSample.ViewModels' or one of its dependencies"

-by specifying a "local" namespace:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="" xmlns:x="" xmlns:local="clr-namespace:DebugSample;assembly=DebugSample" x:Class="DebugSample.Pages.DebugPage"> <ContentPage.Resources> <ResourceDictionary> <local:ViewModels.DebugPageViewModel x:Key="debugPageViewModel" /> </ResourceDictionary> </ContentPage.Resources> ... </ContentPage>
=> I get another exception: "Xamarin.Forms.Xaml.XamlParseException: Position 8:6. Property Resources is null or is not IEnumerable"

I don't understand these exceptions, cause this works fine in the App.xaml:
<?xml version="1.0" encoding="utf-8" ?> <Application xmlns="" xmlns:x="" xmlns:vm="clr-namespace:DebugSample.ViewModels;assembly=DebugSample.ViewModels" x:Class="DebugSample.App"> <Application.Resources> <ResourceDictionary> <vm:ViewModelLocator x:Key="Locator" /> </ResourceDictionary> </Application.Resources> </Application>

=> Would you have an explanation?


  • NMackayNMackay GBInsider, University mod


    You can reference your viewmodel like this instead if your using the VML pattern:

    <ContentPage xmlns=""
                 BindingContext="{Binding Source={x:Static mvvm:App.Locator}, Path=Homepage}"
                 Title="{Binding AppTitle, StringFormat='{0} Dashboard'}"
                 Style="{StaticResource PageStyle}"
  • Pierre-ChristopheDusPierre-ChristopheDus FRUniversity ✭✭✭

    Thanks for your return, but what is the VML Pattern?

    I've adapted your answer to my case, and I don't meet exception:
    <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="" xmlns:x="" xmlns:local="clr-namespace:DebugSample;assembly=DebugSample" BindingContext="{Binding Source={x:Static local:App.Locator}, Path=DebugPageViewModel}" x:Class="DebugSample.Pages.DebugPage"> ... </ContentPage>

    But as I mentioned in my first post, I would like to use a Command Binding on a Label: that's why I've tried to declare Resources, for specyfying the ViewModel and the Command associated to the Label.

    Based on your proposal, I have done this but nothing happens:
    <TapGestureRecognizer Command="{Binding Source={x:Static local:App.Locator}, Path=DebugPageViewModel.WithTappedCommand}" />

    What's wrong in this case?

  • NMackayNMackay GBInsider, University mod
    edited March 2016


    The code you sent wouldn't work
    VML = ViewModelLocator pattern, seeing as you vm:ViewModelLocator in your sample code I assumed you were using this approach.

    In your sample, as you have set the page binding context you just need to reference the command name (assuming you made it public).

    <TapGestureRecognizer Command="{Binding WithTappedCommand}" />

    Send me a PM and I'll send you a sample app to show this approach if you want, it's an MVVM Light sample but shows how to use this pattern.

    Also worth checking the debug window as it will highlight these binding errors.

  • Pierre-ChristopheDusPierre-ChristopheDus FRUniversity ✭✭✭

    It doesn't work if I only specify the command name:
    <TapGestureRecognizer Command="{Binding WithTappedCommand}" />

    But finally @rarenivar helped me to solve my problem, by using:
    instead of:

    But why is this syntax working in the App.xaml, but not in the page?

  • Anil.ApexAnil.Apex USMember ✭✭

    @NMackay I'm noticing build failure (only on Android and iOS. Builds nicely on UWP) when declaring ViewModelLocator inside App class's ResourceDictionary. But when ViewModelLocator is defined as static property in App class, app builds successfully in all 3 projects. I'm sure ResourceDictionary for App class is half-baked.

Sign In or Register to comment.