本文告诉大家如何在 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 IMyInterface1 and IMyInterface2 and 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 DataTemplate for 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 ItemTemplateSelector property of ItemsControl can be used to pick which DataTemplate you want to use for each item. In the SelectTemplate method 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 the DataTemplate that matches it.


本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系

win10 uwp xaml 绑定接口的更多相关文章

  1. Win10 UWP xaml 延迟加载元素

    xaml新增x:DeferLoadStrategy里面只有Lazy,查询了百度看到MSP_甄心cherish大神说的 xaml使用x:DeferLoadStrategy="Lazy" ...

  2. win10 uwp 绑定静态属性

    Jasoon 大神问,如何绑定静态属性. 我们经常有静态属性,那么我们如何绑定 假如我们的ViewModel有一个静态属性 public static string CVTE { set; get; ...

  3. Win10 UWP开发系列:实现Master/Detail布局

    在开发XX新闻的过程中,UI部分使用了Master/Detail(大纲/细节)布局样式.Win10系统中的邮件App就是这种样式,左侧一个列表,右侧是详情页面.关于这种 样式的说明可参看MSDN文档: ...

  4. win10 uwp 商业游戏 1.1.5

    本文是在win10 uwp 商业游戏 基础上继续开发,添加一些无聊的游戏 因为在发布几个月,下载量很少,小伙伴说游戏就玩不到几分钟就不想玩,于是我就想加入其他游戏 下面我来告诉大家如何在游戏中添加多个 ...

  5. Win10 UWP开发系列:使用VS2015 Update2+ionic开发第一个Cordova App

    安装VS2015 Update2的过程是非常曲折的.还好经过不懈的努力,终于折腾成功了. 如果开发Cordova项目的话,推荐大家用一下ionic这个框架,效果还不错.对于Cordova.PhoneG ...

  6. win10 uwp MVVM 轻量框架

    如果在开发过程,遇到多个页面之间,需要传输信息,那么可能遇到设计的问题.如果因为一个页面内包含多个子页面和多个子页面之间的通信问题找不到一个好的解决方法,那么请看本文.如果因为ViewModel代码越 ...

  7. win10 uwp DataContext

    本文告诉大家DataContext的多种绑法. 适合于WPF的绑定和UWP的绑定. 我告诉大家很多个方法,所有的方法都有自己的优点和缺点,可以依靠自己喜欢的用法使用.当然,可以在新手面前秀下,一个页面 ...

  8. win10 uwp 渲染原理 DirectComposition 渲染

    本文来告诉大家一个新的技术DirectComposition,在 win7 之后(实际上是 vista),微软正在考虑一个新的渲染机制 在 Windows Vista 就引入了一个服务,桌面窗口管理器 ...

  9. win10 uwp 如何开始写 uwp 程序

    本文告诉大家如何创建一个 UWP 程序. 这是一系列的 uwp 入门博客,所以写的很简单 本文来告诉大家如何创建一个简单的程序 安装 VisualStudio 在开始写 UWP 需要安装 Visual ...

随机推荐

  1. map.(parseInt)方法详解

    偶然间碰到这样一个问题: ["1","2", "3"].map(parseInt) //[ 1, NaN, NaN ] 运行结果 [ 1, ...

  2. poj 2406 Power Strings(KMP入门,next函数理解)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 37685   Accepted: 15590 D ...

  3. Total Number of Unicorn Companies: 188

    https://www.cbinsights.com/research-unicorn-companies   Total Number of Unicorn Companies: 188 Total ...

  4. Codeforces Round #192 (Div. 2) A. Cakeminator【二维字符数组/吃掉cake,并且是一行或者一列下去,但是该行/列必须没有草莓的存在】

    A. Cakeminator time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. vue-waterfall2 基于Vue.js 瀑布流组件

    vue-waterfall2 1.宽度自适应,数据绑定特效(适用于上拉加载更多) 2.自定义程度高 3.使用极为简便,适用于PC/移动端 4.提供resize(强制刷新布局-适用于下拉刷新)/mix( ...

  6. BZOJ 4817数点涂色题解

    题目链接 考试考了一道类似的题目,然后不争气的挂掉了,于是跑过来学习这道题的解法... 我还是太菜了.... 我们可以发现任意时刻,原树中颜色相同的点的集合一定是一条链, 即上面这种状态,而这种结构是 ...

  7. 初探uni-app

    第一步:下载 第二步:安装 第三步:创建项目 第四步:项目目录 项目运行 项目效果为 打包为原生App(云端) 运行打包 使用vue 效果如下,并不好看 使用代码如下 未完,待续.... 就是对着官网 ...

  8. tftp-server服务器搭建

    学习搭建TFTP服务器(步骤来于网上) 以contos6.5为例 执行下面的命令能够看到服务是否已经启动,若已经启动则不用安装,否则需要安装下面的步骤安装tftp-server服务器 netstat ...

  9. oracle 数据库安全审计

    Oracle的审计机制是用来监视用户对ORACLE数据库所做的各种操作. 在缺省情况下,系统的审计功能是关闭的.可以在INIT.ORA参数文件中将参数AUDIT_TRAIL设置为正整数来激活. 审计功 ...

  10. 阿里云区块链共创会:BaaS正式商业化 广邀合作伙伴共建生态

    摘要: 阿里云宣布区块链服务Hyperledger Fabric版正式商业化,并发布生态合作伙伴计划. 2019年3月29日,阿里云区块链于深圳召开正式商业化共创会,宣布区块链服务Hyperledge ...