Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 表单编译绑定在DataTemplate上不起作用_C#_Xaml_Xamarin_Xamarin.forms_Data Binding - Fatal编程技术网

C# 表单编译绑定在DataTemplate上不起作用

C# 表单编译绑定在DataTemplate上不起作用,c#,xaml,xamarin,xamarin.forms,data-binding,C#,Xaml,Xamarin,Xamarin.forms,Data Binding,我和你有麻烦 我在App.xaml.cs中添加了XamlComilation 我将我的DataTemplate更改为 <DataTemplate x:Key="RectLayerDataTemplate" x:DataType="{x:Type viewmodels:RectLayerViewModel}"> <forms:RectLayerView forms:ValueX="{Binding ValueX}"

我和你有麻烦

我在App.xaml.cs中添加了
XamlComilation

我将我的
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>

您是否使用了
AbsoluteLayout
使布局重叠,因此您认为它不起作用

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

page.xaml:

<StackLayout x:Name="CanvasLayout" Orientation="Vertical"
             BindableLayout.ItemsSource="{Binding LayerViewModels}" />
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 });
        }
    }
}
数据模板

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>

的示例用法。下面的
ItemViewModel
列表视图中的项目类型。ItemsSource

XAML:


你好,@Leo Zhu-MSFT。很抱歉再次打扰你。我已经有了工作示例,它使用了
DataTemplateSelector
。所以我认为绝对布局在这里不是问题。我试图用
x:DataType
得到相同的结果,因为我的同事比Xamarin更熟悉WPF。顺便说一下,在你的代码中,我无法得到它
s.template
的意思
LayerTemplate
是具有多个数据模板的ResourceDictionary。但是,只有一个
DataTemplate
应用于所有对象。简言之,有没有一种方法可以通过
x:DataType
匹配视图和视图模型,而不使用
DataTemplateSelector
?我发现了我需要的相同方法。
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 });
        }
    }
}
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>
xmlns:vm="clr-namespace:MyViewModels"
...
<DataTemplate x:Key="ListViewItemTemplate" x:DataType="vm:ItemViewModel" >
...

<ListView ... ItemTemplate="{StaticResource ListViewItemTemplate}"/>
namespace MyViewModels
{
  public class ItemViewModel : ViewModelPropertyBase
  ...

  public abstract class ViewModelPropertyBase : INotifyPropertyChanged
  ...