2018-6-15-win10-uwp-xaml-绑定接口
| title | author | date | CreateTime | categories |
|---|---|---|---|---|
|
win10 uwp xaml 绑定接口
|
lindexi
|
2018-6-15 21:7:19 +0800
|
2018-2-13 17:23:3 +0800
|
Win10 UWP
|
本文告诉大家如何在 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.
2018-6-15-win10-uwp-xaml-绑定接口的更多相关文章
- win10 uwp xaml 绑定接口
本文告诉大家如何在 xaml 绑定属性使用显式继承接口 早上快乐 就在你的心问了我一个问题,他使用的属性是显式继承,但是无法在xaml绑定 我写了简单的代码,一个接口和属性 public class ...
- Win10 UWP xaml 延迟加载元素
xaml新增x:DeferLoadStrategy里面只有Lazy,查询了百度看到MSP_甄心cherish大神说的 xaml使用x:DeferLoadStrategy="Lazy" ...
- win10 uwp 绑定静态属性
Jasoon 大神问,如何绑定静态属性. 我们经常有静态属性,那么我们如何绑定 假如我们的ViewModel有一个静态属性 public static string CVTE { set; get; ...
- Win10 UWP开发系列:实现Master/Detail布局
在开发XX新闻的过程中,UI部分使用了Master/Detail(大纲/细节)布局样式.Win10系统中的邮件App就是这种样式,左侧一个列表,右侧是详情页面.关于这种 样式的说明可参看MSDN文档: ...
- win10 uwp 商业游戏 1.1.5
本文是在win10 uwp 商业游戏 基础上继续开发,添加一些无聊的游戏 因为在发布几个月,下载量很少,小伙伴说游戏就玩不到几分钟就不想玩,于是我就想加入其他游戏 下面我来告诉大家如何在游戏中添加多个 ...
- Win10 UWP开发系列:使用VS2015 Update2+ionic开发第一个Cordova App
安装VS2015 Update2的过程是非常曲折的.还好经过不懈的努力,终于折腾成功了. 如果开发Cordova项目的话,推荐大家用一下ionic这个框架,效果还不错.对于Cordova.PhoneG ...
- Win10 UWP开发实现Bing翻译
微软在WP上的发展从原来的Win7到Win8,Win8.1,到现在的Win10 UWP,什么是UWP,UWP即Windows 10 中的Universal Windows Platform简称.即Wi ...
- win10 uwp 列表模板选择器
本文主要讲ListView等列表可以根据内容不同,使用不同模板的列表模板选择器,DataTemplateSelector. 如果在 UWP 需要定义某些列的显示和其他列不同,或者某些行的显示和其他行不 ...
- win10 uwp MVVM 轻量框架
如果在开发过程,遇到多个页面之间,需要传输信息,那么可能遇到设计的问题.如果因为一个页面内包含多个子页面和多个子页面之间的通信问题找不到一个好的解决方法,那么请看本文.如果因为ViewModel代码越 ...
- win10 uwp DataContext
本文告诉大家DataContext的多种绑法. 适合于WPF的绑定和UWP的绑定. 我告诉大家很多个方法,所有的方法都有自己的优点和缺点,可以依靠自己喜欢的用法使用.当然,可以在新手面前秀下,一个页面 ...
随机推荐
- LintCode刷题笔记-- Update Bits
标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...
- SQLServer —— 视图
一.视图的概念 是存储在服务器端的一个查询块,是一张虚拟表. 表示一张表的部分数据或多张表的综合数据. 其结构和数据是建立在对表的查询基础上. 视图的使用,跟对普通的表的查询使用完全一样. 二.视图中 ...
- 【JZOJ4899】【NOIP2016提高A组集训第17场11.16】雪之国度
题目描述 雪之国度有N座城市,依次编号为1到N,又有M条道路连接了其中的城市,每一条道路都连接了不同的2个城市,任何两座不同的城市之间可能不止一条道路.雪之女王赋予了每一座城市不同的能量,其中第i座城 ...
- iOS尽量不要在viewWillDisappear:方法中移除通知
http://www.cocoachina.com/ios/20151214/14596.html 在了解控制器的生命周期之后,我们都知道viewWillAppear:方法是在控制器的view将要显示 ...
- Xici drop flower
var xici_user_api = "http://www.xici.net/apps/wedding/?method=wedding.user.getusername&from ...
- 小爬爬5:重点回顾&&移动端数据爬取1
1. ()什么是selenium - 基于浏览器自动化的一个模块 ()在爬虫中为什么使用selenium及其和爬虫之间的关联 - 可以便捷的获取动态加载的数据 - 实现模拟登陆 ()列举常见的sele ...
- 惊闻!SOIC 和 SOP 竟然是有区别的
目录 惊闻!SOIC 和 SOP 竟然是有区别的 原因 对比 结论 惊闻!SOIC 和 SOP 竟然是有区别的 原因 一直以为 SOIC 和 SOP 是一样的,只是叫法不同. 对比 今天仔细查了才发现 ...
- IO流之字符流-1
Reader和Writer抽象类 Reader是定义java的流式字符输入流模式的抽象类 Writer是定义流式字符输出的抽象类 该类的方法都返回void值并在错条件下抛出IOException异常 ...
- spingboot项目在windows环境中运行时接收参数及日志中文乱码
1.logback.xml配置 appender中添加 <param name="Encoding" value="UTF-8" /> <co ...
- SharpDX初学者教程第4部分:绘制三角形
原文 http://www.johanfalk.eu/blog/sharpdx-beginners-tutorial-part-4-drawing-a-triangle 现在我们有了一个Direct3 ...