Xaml 内部错误错误WMC9999:对象引用未设置为对象的实例



这已经出现了几次,我一直在 UWP 应用程序中挣扎一整天。

我的具体问题是我在数据模板中的内容模板中使用了x:Bind:

<DataTemplate x:DataType="IFactionMember">
  <Button Command="{x:Bind **Property of IFactionMember**}"> // Good
    <Button.Template>
      <ControlTemplate TargetType="Button">
        <Grid Padding="10,0">
          <TextBlock Text="{x:Bind **Property of IFactionMember**}" /> // Bad
        </Grid>
      </ControlTemplate>
    </Button.Template>
  </Button>
</DataTemplate>

您不能这样做:(

您可以改用{Binding}用于您的方案,例如:

<ListView x:Name="listview" ItemsSource="{x:Bind members}">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="local:IFactionMember">
            <Button > 
                <Button.Template>
                    <ControlTemplate TargetType="Button" >
                        <Grid Padding="10,0">
                            <TextBlock Text="{Binding  testtext}" />  
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

x:bind 缺少 {Binding} 的一些功能,在 x:Bind 中,Path默认植根于 Page,而不是 DataContext,看起来如果你在这里使用 x:bind,它会尝试在 MainPage 处查找属性,这样就找不到正确的属性。

相关内容

最新更新