0
votes

I have a ListView that's on a ContentPage, and I want the ItemTemplate of the ListView to be user-definable - i.e. parsing the XAML for the template at runtime. However, I need to give the user the option of having a button that invokes a command on the page. See below XAML;

 <SwipeView>
<SwipeView.RightItems>
    <SwipeItems>
        <SwipeItem
                            Text = ""Delete""
                            BackgroundColor=""Red""
                            Command=""{Binding Path=ItemDeletedCommand, Source={x:Reference multiEntryPage}}""
                            CommandParameter=""{Binding .}"" />
    </SwipeItems>
</SwipeView.RightItems>
<SwipeView.Content>
                ...
</SwipeView.Content>

And my content page has the x:Name="multiEntryPage" attribute in its ContentPage element. The code I use to parse the XAML is shown here:

listView.ItemTemplate = new DataTemplate(() => new ViewCell().LoadFromXaml(xaml));

However, this gives the below error:

{Xamarin.Forms.Xaml.XamlParseException: Position 11:33. Can not find the object referenced by multiEntryPage at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.ProvideValue (System.Object& value, Xamarin.Forms.Xaml.ElementNode node, System.Object source, Xamarin.Forms.Xaml.XmlName propertyName)...

So I think it's fairly clear that the error is happening because the SwipeView XAML doesn't know about its parent at this stage, but I'm at a loss of how to resolve this. Any ideas?

Thanks!

1
What's the value of xaml ? how you get it ? Could you provide complete code ?ColeX - MSFT
The value of xaml is the xaml listed in the first part code snippetChris
Have you solved the problem?ColeX - MSFT
No, I decided to address my problem in a different way that didn't require a SwipeViewChris

1 Answers

0
votes

You can create DataTemplate in following ways

  • Creating an Inline DataTemplate

  • Creating a DataTemplate with a Type

  • Creating a DataTemplate as a Resource

Refer https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/data-templates/creating.