简体   繁体   中英

Xamarin.Forms Compiled Bindings does not work on DataTemplate

I am having trouble with Use compiled bindings in a DataTemplate .

I added XamlComilation in App.xaml.cs

// https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xamlc
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace Solution.Project

And I changed my DataTemplate to

<DataTemplate x:Key="RectLayerDataTemplate" x:DataType="{x:Type viewmodels:RectLayerViewModel}">
    <forms:RectLayerView forms:ValueX="{Binding ValueX}"
                         forms:ValueY="{Binding ValueY}"
                         forms:ValueWidth="{Binding ValueWidth}"
                         forms:ValueHeight="{Binding ValueHeight}"
                         forms:Color="{Binding Color}" />
</DataTemplate>

However, DataTemplate does not applied to BindableLayout.ItemSource .

<AbsoluteLayout x:Name="CanvasLayout"
                BindableLayout.ItemsSource="{Binding LayerViewModels}" />

Did you use AbsoluteLayout to make the layout overlap, so you think it didn't work?

I create a simple sample,with the DataTemplate and set it to the StackLayout BindableLayout ,it works well.

the page.xaml:

<StackLayout x:Name="CanvasLayout" Orientation="Vertical"
             BindableLayout.ItemsSource="{Binding LayerViewModels}" />

page.xam.cs:

public MyPage()
    {
        InitializeComponent();
        BindingContext = new MyData();          
        LayerDataTemplate s = new LayerDataTemplate();
        BindableLayout.SetItemTemplate(CanvasLayout, s.template);
    }

MyData :

public class MyData
{

    public ObservableCollection<RectLayerViewModel> LayerViewModels { set; get; } = new ObservableCollection<RectLayerViewModel>();

    public MyData()
    {
        for (var x = 0; x < 6; x++)
        {
            LayerViewModels.Add(new RectLayerViewModel { Name = $"Added at {LayerViewModels.Count}", Type = 1 });
        }
    }
}

RectLayerViewModel :

public class RectLayerViewModel
{
    public string Name { get; set; }
    public int Type { get; set; }
}

DataTemplate :

<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary  xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"     
         xmlns:local ="clr-namespace:EntryCa"
         x:Class="EntryCa.LayerDataTemplate">
<DataTemplate x:Key="RectLayerDataTemplate" x:DataType="local:RectLayerViewModel">

         <Label Text="{Binding Name}"
                   TextColor="Red"
                   FontAttributes="Bold" />
  
</DataTemplate>

Sample usage of compiled bindings in a DataTemplate . Below ItemViewModel is the type of the items in a ListView.ItemsSource :

XAML:

xmlns:vm="clr-namespace:MyViewModels"
...
<DataTemplate x:Key="ListViewItemTemplate" x:DataType="vm:ItemViewModel" >
...

<ListView ... ItemTemplate="{StaticResource ListViewItemTemplate}"/>

ItemViewModel:

namespace MyViewModels
{
  public class ItemViewModel : ViewModelPropertyBase
  ...

  public abstract class ViewModelPropertyBase : INotifyPropertyChanged
  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM