当前位置:网站首页>win10 uwp xaml 绑定接口
win10 uwp xaml 绑定接口
2022-08-04 18:38:00 【林德熙】
本文告诉大家如何在 xaml 绑定属性使用显式继承接口。
早上快乐 就在你的心问了我一个问题,他使用的属性是显式继承,但是无法在xaml绑定
我写了简单的代码,一个接口和属性
public class Foo : INotifyPropertyChanged, IF1
{
public Foo(string name)
{
_name = name;
}
private string _name;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
string IF1.Name
{
get { return _name; }
set { _name = value; OnPropertyChanged(); }
}
}
public interface IF1
{
string Name { set; get; }
}然后我尝试写一个列表,在前台绑定
public ObservableCollection<Foo> Foo { set; get; } = new ObservableCollection<Foo>()
{
new Foo("jlong"){}
}; <ListView ItemsSource="{x:Bind Foo}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Foo">
<TextBlock Text="{Binding Path=Name }"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>但是这样写出现绑定错误,因为在 Foo 是找不到 Name 属性,需要使用 IF1.Name 去拿到
我修改了代码
<TextBlock Text="{Binding (local:IF1.Name)}"></TextBlock>但是运行就出现了异常,说未指定,最后我尝试了新的方法,居然就编译通过,下面让我来告诉大家如何使用这个方法
<TextBlock Text="{x:Bind Path=(local:IF1.Name) }"></TextBlock>如果使用显式继承,那么在使用的时候需要使用他的接口来拿,但是接口不是直接写,需要先写空间,一般空间是写在最上,请看下面代码
<Page
x:Class="JoleenOneal.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:JoleenOneal" 这是空间
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">然后需要加上括号,才可以使用
为什么上面的代码无法使用,现在我还不知道。
我找到了下面的观点
The data binding team discussed adding support for interfaces a while ago but ended up not implementing it because we could not come up with a good design for it. The problem was that interfaces don’t have a hierarchy like object types do. Consider the scenario where your data source implements both
IMyInterface1andIMyInterface2and you have DataTemplates for both of those interfaces in the resources: which DataTemplate do you think we should pick up?When doing implicit data templating for object types, we first try to find a
DataTemplatefor the exact type, then for its parent, grandparent and so on. There is very well defined order of types for us to apply. When we talked about adding support for interfaces, we considered using reflection to find out all interfaces and adding them to the end of the list of types. The problem we encountered was defining the order of the interfaces when the type implements multiple interfaces.The other thing we had to keep in mind is that reflection is not that cheap, and this would decrease our perf a little for this scenario.
So what’s the solution? You can’t do this all in XAML, but you can do it easily with a little bit of code. The
ItemTemplateSelectorproperty ofItemsControlcan be used to pick whichDataTemplateyou want to use for each item. In theSelectTemplatemethod for your template selector, you receive as a parameter the item you will template. Here, you can check for what interface it implements and return theDataTemplatethat matches it.
边栏推荐
- 情绪的波动起伏
- After EasyCVR is locally connected to the national standard device to map the public network, the local device cannot play and cascade the solution
- 开篇-开启全新的.NET现代应用开发体验
- powershell和cmd对比
- VPC2187/8 电流模式 PWM 控制器 4-100VIN 超宽压启动、高度集成电源控制芯片推荐
- 什么是网站监控,网站监控软件有什么用?
- lc marathon 8.3
- Win10只读文件夹怎么删除
- Google Earth Engine APP——一键在线查看全球1984-至今年的影像同时加载一个影像分析
- Understanding of margin collapse and coincidence
猜你喜欢

2022 May 1 Mathematical Modeling Question C Explanation

2019年海淀区青少年程序设计挑战活动小学组复赛试题详细答案

How does EasyCVR call the double-speed playback of device recording through the interface?

在线生成接口文档

防火墙基础之防火墙做出口设备安全防护

Google AppSheet: 无需编程构建零代码应用

Day018 Inheritance

通配符SSL证书不支持多域名吗?

Those things about the curl command

开篇-开启全新的.NET现代应用开发体验
随机推荐
Google AppSheet: 无需编程构建零代码应用
server
数仓建模面试
面试官:MVCC是如何实现的?
EasyCVR调用云端录像API接口返回错误且无录像文件生成,是什么原因?
服务器
YOLOv7-Pose尝鲜,基于YOLOv7的关键点模型测评
智能视频监控平台EasyCVR如何使用接口批量导出iframe地址?
【RTOS训练营】关于上课和答疑
Literature Review on Involution of College Students
JS 问号?妙用
Win10只读文件夹怎么删除
BigDecimal 使用注意!!“别踩坑”
开发那些事儿:如何通过EasyCVR平台获取监控现场的人流量统计数据?
2018年南海区小学生程序设计竞赛详细答案
网络运维管理从基础到实战-自用笔记(1)构建综合园区网、接入互联网
天呐,七夕我收到9份告白~
如何进行自动化测试?
mood swings
Those things about the curl command