专栏首页林德熙的博客win10 uwp xaml 绑定接口

win10 uwp xaml 绑定接口

本文告诉大家如何在 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.


本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • WPF will break when an exception be throw in the StylusPlugIn

    We can write a class that inherits the StylusPlugIn. And this class can get the ...

    林德熙
  • How to use code to exit the application in UWP

    I will tell you how to exit the application in UWP by the code.

    林德熙
  • WPF shows that some windows in multithreading will be locked in the PenThreadWorker constructor when

    Problem description: WPF will initialize pen thread when the window initializing...

    林德熙
  • js的bind函数那些你可能没想过的点

    加上上一篇文章中说的情况,我们可以总结到,当我们对一个bind返回的函数进行操作时,实际上这种操作是对目标函数的操作,也就是调用bind的函数。下面我们看一下e...

    theanarkh
  • NetDP:用于蚂蚁信用支付中的默认预测的工业规模分布式网络表示框架(CS.LG)

    蚂蚁信用支付是蚂蚁商业风险组中的消费信贷服务。与信用卡类似,贷款违约是该信贷产品的主要风险之一。因此,有效的违约预测算法是公司减少损失和增加利润的关键。但是,我...

    蔡小雪7100294
  • Kaggle竞赛:《NIPS 2017 Adversarial Learning Challenges》

    Batch normalization potentially helps in two ways: faster learning and higher ov...

    fishexpert
  • 如何使用CRM Document Template Profile

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。

    Jerry Wang
  • 3.1.2 使用绘图API绘制Contour的思路

    A week or so back I wrote about a package I ported/modified to create the Delaun...

    周星星9527
  • 开源的有限元分析程序SALOME

    What is SALOME? SALOME is an open-source software that provides a generic Pre- a...

    fem178
  • 线性时间中的平面不相交路径

    作者:Petr A. Golovach,Stavros G. Kolliopoulos,Giannos Stamoulis,Dimitrios M. Thili...

    罗大琦

扫码关注云+社区

领取腾讯云代金券