本文告诉大家如何在 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. 斯坦福CS课程列表

    http://exploredegrees.stanford.edu/coursedescriptions/cs/ CS 101. Introduction to Computing Principl ...

  2. Java 和 DynamoDB

    https://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/GettingStarted.Java.html 官方

  3. Directx11教程(11) 增加一个debug宏

    原文:Directx11教程(11) 增加一个debug宏       现在我们在common.h中增加一个debug的宏,在每个d3d11函数后调用,如果d3d函数出错,它能够给出程序中错误的代码行 ...

  4. 纯CSS3实现Metro Icon

    在线演示 本地下载

  5. 阿里云发布Apsara SA系列混合云存储阵列

    3月21日,2019北京阿里云峰会上,阿里云正式发布Apsara SA系列混合云存储阵列,融合IP SAN,FC SAN,NAS和OSS对象存储协议于一体,同时实现了本地数据中心架构和公共云存储的无缝 ...

  6. SqlAlchemy的简单使用

    1.SQLAlchemy SQLAlchemy是python的一个通用的ORM框架 1.1 创建数据表 from sqlalchemy.ext.declarative import declarati ...

  7. day4_python之三元表达式、列表推导式、生成器表达式

    一.三元表达式 name=input('姓名>>: ') res='SB' if name == 'alex' else 'NB' print(res) 二.列表解析 l = [] for ...

  8. 大话鸿蒙操作系统(一)-- 先聊聊 Fuchsia OS

    大话鸿蒙操作系统(一) 第一篇先不聊鸿蒙操作系统,聊聊 Google 的新系统 Fuchsia OS. 先看看 Fuchsia OS 介绍. 为什么 Google 要造新的 Fuchsia OS 操作 ...

  9. 寒哥教你学iOS - 经验漫谈

    http://www.jianshu.com/p/cb54054d3add 寒哥教你学iOS - 经验漫谈 字数2848 阅读1896 评论19 喜欢43 顺便来个广告 iOS开发者 群1734993 ...

  10. 【NS2】Installing ns-2.29 in Ubuntu 12.04

    Installing ns-2.29 in Ubuntu 12.04     Off late, we try to use(install) a old software in a new Oper ...