Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# UWP XAML x:无法识别绑定继承的接口 在UWP XAML应用程序中使用X:BIN时,请考虑以下事项: 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; } } public partial class MainPage : Page { public ISubInterface TheObject = new ImplementationClass { A = "1", B = "2" }; //All the rest goes here }_C#_Xaml_Uwp_Xbind - Fatal编程技术网

C# UWP XAML x:无法识别绑定继承的接口 在UWP XAML应用程序中使用X:BIN时,请考虑以下事项: 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; } } public partial class MainPage : Page { public ISubInterface TheObject = new ImplementationClass { A = "1", B = "2" }; //All the rest goes here }

C# UWP XAML x:无法识别绑定继承的接口 在UWP XAML应用程序中使用X:BIN时,请考虑以下事项: 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; } } public partial class MainPage : Page { public ISubInterface TheObject = new ImplementationClass { A = "1", B = "2" }; //All the rest goes here },c#,xaml,uwp,xbind,C#,Xaml,Uwp,Xbind,在Page类中,我们有以下内容: 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;

在Page类中,我们有以下内容:

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; }
}
public partial class MainPage : Page
{
    public ISubInterface TheObject = new ImplementationClass { A = "1", B = "2" };
    //All the rest goes here
}
在主页XAML中,我们有以下代码段:

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

这会导致以下编译器错误: XamlCompiler错误WMC1110:绑定路径“A”无效:在类型“ISubInterface”上找不到属性“A”

但是,以下方法确实有效:

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

有人知道UWP XAML平台的一个已知限制是继承的接口属性不能被编译器识别吗? 或者这应该被视为一个bug? 是否有已知的解决方法

非常感谢您的帮助。
提前谢谢

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

作为一种解决方法,我们可以使用传统绑定而不是X:Bind,如下所示:

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; }
}
public partial class MainPage : Page
{
    public ISubInterface TheObject = new ImplementationClass { A = "1", B = "2" };
    //All the rest goes here
}
在.xaml中:

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

如果您编写了一个类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"){}
    };

通过文件顶部的xmlns添加基类的命名空间:

xmlns:base="using:Namespace.Of.Base.Class"

您能否尝试将
对象
定义为
IBaseInterface
而不是
ISubInterface
,然后查看编译器是否接受
对象。A
?感谢您的尝试并得出与我相同的结论。我确实通过Connect提交了一个bug: