2015-09-04 22 views
9

当使用X:绑定在UWP XAML应用程序考虑以下几点:UWP XAML X:绑定继承​​接口不被识别

interface IBaseInterface 
{ 
    string A { get; set; } 
} 
interface ISubInterface : IBaseInterface 
{ 
    string B { get; set; } 
} 
class ImplementationClass : ISubInterface 
{ 
    public string A { get; set; } 
    public string B { get; set; } 
} 

在Page类,我们有以下几点:

public partial class MainPage : Page 
{ 
    public ISubClass TheObject = new ImplementationClass { A = "1", B = "2" }; 
    //All the rest goes here 
} 

在MainPage XAML中,我们有以下代码片段:

<TextBlock Text={x:Bind Path=TheObject.A}></TextBlock> 

这会导致以下编译器错误: XamlCompiler错误WMC1110:无效的绑定路径“A”:房产“A”不能在类型中找到“ISubInterface”

以下但不工作:

<TextBlock Text={x:Bind Path=TheObject.B}></TextBlock> 

有谁知道这是否是一个编译器无法识别继承了接口属性的UWP XAML平台的已知限制? 或者这应该被认为是一个错误? 有没有已知的解决方法?

非常感谢帮助。 在此先感谢!

+2

你可以尝试将'TheObject'定义为'IBaseInterface'而不是'ISubInterface'并查看编译器是否接受'TheObject.A'? – dognose

回答

7

是的,在做了一些测试和研究之后,似乎使用X:Bind时编译器无法识别继承的接口属性。

作为一种变通方法,我们可以用它代替了X传统的绑定:绑定如下:

在的.xaml:

<Grid Name="MyRootGrid"> 
     <TextBlock Text="{Binding A}"></TextBlock> 
</Grid> 

在xaml.cs:

MyGrid.DataContext = TheObject; 
+0

感谢您尝试并得出与我一样的结论。我通过Connect提交了一个错误:https://connect.microsoft.com/VisualStudio/feedback/details/1770944 –

0

如果你写了一个Foo类继承IFoo。

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"){} 
    }; 

你怎么能绑定它在XAML?

我找到了一种方法来绑定它。您应该使用x:bind并使用Path=(name:interface.xx)在xaml中绑定它。

<ListView ItemsSource="{x:Bind Foo}"> 
     <ListView.ItemTemplate> 
      <DataTemplate x:DataType="local:Foo"> 
       <TextBlock Text="x:Bind Path=(local:IFoo.Name)"></TextBlock> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView>