【问题标题】:Xamarin.Forms Compiled Bindings does not work on DataTemplateXamarin.Forms 编译绑定不适用于 DataTemplate
【发布时间】:2020-11-19 17:24:02
【问题描述】:

Use compiled bindings in a DataTemplate 有问题。

我在 App.xaml.cs

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

我将DataTemplate 更改为

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

但是,DataTemplate 不适用于BindableLayout.ItemSource

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

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms data-binding


    【解决方案1】:

    你是不是用AbsoluteLayout让布局重叠,所以你觉得没用?

    我创建了一个简单的示例,使用DataTemplate 并将其设置为StackLayout BindableLayout,效果很好。

    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);
        }
    

    我的数据

    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; }
    }
    

    数据模板

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

    【讨论】:

    • 你好,@Leo Zhu - MSFT。很抱歉再次打扰您。我已经有工作样本,它使用DataTemplateSelector。所以我不认为AbsoluteLayout 在这里不是问题。我试图通过x:DataType 获得相同的结果,因为我的同事比 Xamarin 更熟悉 WPF。顺便说一句,在你的代码中,我无法得到它s.template 的意思。 LayerTemplate 是具有多个 DataTemplate 的 ResourceDictionary。但是,BindableLayout.SetItemTemplate 仅适用于所有DataTemplate
    • 简而言之,有没有办法在不使用DataTemplateSelector 的情况下通过x:DataType 匹配View 和ViewModel?
    • 我找到了相同的question 我需要。
    【解决方案2】:

    compiled bindings in a DataTemplate 的示例用法。 ItemViewModel 下面是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
      ...
    

    【讨论】:

      猜你喜欢
      • 2016-01-27
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      • 2021-06-27
      • 2017-06-18
      • 1970-01-01
      • 2021-07-01
      相关资源
      最近更新 更多