Exceptions with ResourceDictionary in a ContentPage

Hello,

I try to reproduce a solution for Command Binding found here:
forums.xamarin.com/discussion/comment/154362#Comment_154362

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="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 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="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 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="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 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?

Answers

  • NMackayNMackay GBInsider, University ✭✭✭✭✭

    @Pierre-ChristopheDus

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

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:mvvm="clr-namespace:OrderFinder;assembly=OrderFinder"
                 BindingContext="{Binding Source={x:Static mvvm:App.Locator}, Path=Homepage}"
                 Title="{Binding AppTitle, StringFormat='{0} Dashboard'}"
                 Style="{StaticResource PageStyle}"
                 x:Name="Home"
                 x:Class="OrderFinder.View.HomePage">
    
  • Pierre-ChristopheDusPierre-ChristopheDus FRUniversity ✭✭

    @NMackay
    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="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 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 ✭✭✭✭✭
    edited March 2016

    @Pierre-ChristopheDus

    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 ✭✭

    @NMackay
    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:
    xmlns:vm="clr-namespace:DebugSample.ViewModels;assembly=DebugSample"
    instead of:
    xmlns:vm="clr-namespace:DebugSample.ViewModels;assembly=DebugSample.ViewModels"

    But why is this syntax working in the App.xaml, but not in the page?
    xmlns:vm="clr-namespace:DebugSample.ViewModels;assembly=DebugSample.ViewModels"

  • 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.