[UWP] [XAML] x:绑定无法识别父接口中的属性 [英] [UWP][XAML] x:Bind does not recognise properties in parent interfaces

查看:59
本文介绍了[UWP] [XAML] x:绑定无法识别父接口中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我来说这看起来像个错误,这是一个已知的问题吗?


例如考虑字符串属性的绑定


 
< TextBlock
Text
=" { < span style ="font-size:9.5pt; font-family:Consolas; color:#A31515; background:white"> x Bind
VM
。说明, 模式 < span style ="font-si ZE:9.5pt; FONT-FAMILY:索拉;颜色:蓝色; background:white"> = OneWay}"
/>


 


视图模型的界面如下所示


   
///
< summary>


   
/// 实体专用版本的界面


   
///
< / summary>


   
public
interface
ISpecializedViewModel
IThingViewModel


   
{


   
}


 


实际上是在父界面


   
///
< summary>


   
/// 所有实体的基本界面


   
///
< / summary>


   
public
interface
IThingViewModel
IPresenterLoadedViewModel


   
{


       
///
< summary>


       
/// 获取实体的描述


       
///
< / summary>


       
string 描述{
get ;}


   
}


 


如果代码页面后面包含


       
///
< summary>


       
/// 获取数据绑定视图模型的类型版本


       
///
< / summary>


       
public
ISpecializedViewModel VM


       
{


           
得到


           
{


               
return
this .DataContext
as
ISpecializedViewModel ;


           
}


       
}


 


如果你想绑定视图模型和它继承的任何类中的值,这似乎是明显的声明。


然后页面将无法编译并生成此错误


XamlCompiler错误WMC1110:无效的绑定路径'VM.Description':在类型'ISpecializedViewModel'上找不到属性'描述'


 


但是如果后面的代码改为


       
///
< summary>


       
/// 获取数据绑定视图模型的类型版本


       
///
< / summary>


       
public
IThingViewModel VM


       
{


           
得到


           
{


               
return
this .DataContext
as
IThingViewModel ;


           
}


       
}


 


一切都很好。


我原以为编译器应该能够自己解决这个问题。


At至少这个问题可能有助于其他人试图弄清楚在类似情况下发生了什么。

解决方案

来自msdn docs 



在XAML加载时,  {x:Bind}  是
转换为你能想到的绑定对象,这个对象从数据源的属性中获取一个值。绑定对象可以选择配置为观察更改数据源属性的值并根据
这些更改刷新自身。还可以选择将其自身值的更改推送回源属性。由 
创建的绑定对象 {x:Bind}  和  {Binding}  
在很大程度上与功能相当。但是 
{x:Bind} 执行
特殊用途代码,它在编译时生成, 
{Binding}  使用
通用运行时对象检查。因此, 
{x:Bind}   bindings
(通常称为编译绑定)具有出色的性能,提供绑定表达式的编译时验证,并支持调试,允许您在代码中设置断点生成为页面的部分类的文件。
这些文件可以在您的 
obj  文件夹,
,名称为(对于C#) 
< view name> .g.cs



https://msdn.microsoft.com/en-us/windows/uwp/xaml-platform/x-bind-markup-extension



我认为x:Bind不是为了绑定接口。 如果你被绑定到特定的类,例如ThingViewModel,它可能会工作。如果你想绑定到一个接口,我会坚持使用绑定


This looks like a bug to me, is it a known issue?

For example consider the binding of a string property

  <TextBlock Text="{x:Bind VM.Description, Mode=OneWay}" />

 

Where the interface of the view-model looks like this

    /// <summary>

    /// The interface of a specialized version of an entity

    /// </summary>

    public interface ISpecializedViewModel : IThingViewModel

    {

    }

 

And the property is actually in the parent interface

    /// <summary>

    /// The base interface for all entities

    /// </summary>

    public interface IThingViewModel : IPresenterLoadedViewModel

    {

        /// <summary>

        /// Gets the description of an entity

        /// </summary>

        string Description { get; }

    }

 

If the code behind the page contains

        /// <summary>

        /// Gets a typed version of the view model for data binding

        /// </summary>

        public ISpecializedViewModel VM

        {

            get

            {

                return this.DataContext as ISpecializedViewModel;

            }

        }

 

Which would seem like the obvious declaration if you wanted to bind to values in both the view-model and any classes it inherits from.

Then the page will not compile and this error is generated

XamlCompiler error WMC1110: Invalid binding path 'VM.Description' : Property 'Description' can't be found on type 'ISpecializedViewModel'

 

However if the code behind is changed to

        /// <summary>

        /// Gets a typed version of the view model for data binding

        /// </summary>

        public IThingViewModel VM

        {

            get

            {

                return this.DataContext as IThingViewModel;

            }

        }

 

All is well.

I would have expected that the compiler should have been able to work that out for itself.

At the very least this question may help others trying to figure out what is going on in similar situations.

解决方案

From the msdn docs 

At XAML load time, {x:Bind} is converted into what you can think of as a binding object, and this object gets a value from a property on a data source. The binding object can optionally be configured to observe changes in the value of the data source property and refresh itself based on those changes. It can also optionally be configured to push changes in its own value back to the source property. The binding objects created by {x:Bind} and {Binding} are largely functionally equivalent. But {x:Bind}executes special-purpose code, which it generates at compile-time, and {Binding} uses general-purpose runtime object inspection. Consequently, {x:Bind} bindings (often referred-to as compiled bindings) have great performance, provide compile-time validation of your binding expressions, and support debugging by enabling you to set breakpoints in the code files that are generated as the partial class for your page. These files can be found in your obj folder, with names like (for C#) <view name>.g.cs.

https://msdn.microsoft.com/en-us/windows/uwp/xaml-platform/x-bind-markup-extension

I don't think x:Bind was meant for binding to interfaces.  If you were bound to a specific class for example ThingViewModel it would probably work.   If you want to bind to an interface I would stick with binding


这篇关于[UWP] [XAML] x:绑定无法识别父接口中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆