Source Reference for binding in a standalone resource dictionary to the a content page context

What should the binding source reference for be in a Xamarin forms 3 standalone resource dictionary? Given the following XAML:

<DataTemplate x:Key="newAddressTemplate">
    <ViewCell>
        <ViewCell.View>
            <Label TextColor="{Binding LabelColorName}" Text="{Binding Label}" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding Path=BindingContext.NewAddressCommand, Source={x:Reference Page}}" CommandParameter="{Binding AddressDestinationType}" />
                </Label.GestureRecognizers>
            </Label>
        </ViewCell.View>
    </ViewCell>
</DataTemplate>

The TapGestureRecognizer Command binding works if the resource dictionary is in the content page itself (where the content page includes x:Name="Page"), but not when it is moved to a standalone file containing only the resource dictionary. Adding x:Name="Page" to the resource dictionary tag doesn't resolve the issue. It seems like the reference should be to parent whatever that might be.

Answers

  • BrianHOMBrianHOM Member

    I should have also include that this is the error when the datatemplate is rendered.

    Xamarin.Forms.Xaml.XamlParseException: Position 67:47. Can not find the object referenced by Page

    This isn't unexpected as there is no Page object and it gives the same error if the resource dictionary has the x:Name="Page" as well.

    Again I would think that the reference should be something like Source={x:Reference Page.Parent}, but the resource dictionary doesn't have a Parent or similar property.

  • Nick4978Nick4978 USMember ✭✭

    Make sure you have x:Name="Page" in your page container. "Page" is not a built-in identifier, it's referencing a name of an object and if you didn't name anything (such as your page) with "Page" then you'll get that error.

  • BrianHOMBrianHOM Member

    I had tried that as it is required even if the resource is part of the page consuming it, but unfortunately it still results in a null reference. I've not tried it again with the most recent revision of Forms, though and perhaps that will resolve the issue.
    Thanks for the idea anyway!

  • OnurHazarOnurHazar USUniversity ✭✭✭

    @BrianHom Is this issue fixed with the latest version of xamarin forms (3.3) ?

Sign In or Register to comment.