附加属性'%0'在类型'%1'中找不到

本文关键字:找不到 类型 属性 | 更新日期: 2023-09-27 18:12:29

我试图在PCL中使用XamlReader.Load(xamlstring)加载xaml(用于windows 8.1 &电话8.1)项目,但我总是在下面一行遇到异常"Windows.UI.Xaml.Markup.XamlParseException":

string content = "<interactivity:Interaction.Behaviors>   
                    <core:EventTriggerBehavior EventName='Tapped'>
                      <core:InvokeCommandAction Command=
                         '{Binding ElementName=ViewControlGrid,
                          Path=DataContext.ViewActionClickCommand}'
                        CommandParameter='{Binding RecordId}' />
                    </core:EventTriggerBehavior>
                </interactivity:Interaction.Behaviors>";

这是命名空间声明

private static string namespaceString = 
       @"xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
        xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
        xmlns:core='using:Microsoft.Xaml.Interactions.Core;assembly=Microsoft.Xaml.Interactions' 
        xmlns:interactivity='using:Microsoft.Xaml.Interactivity;assembly=Microsoft.Xaml.Interactions' ";

有什么办法可以解决这个问题吗?或者我做错了什么?

编辑:

我要加载的确切Xaml字符串是

    <DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:core="using:Microsoft.Xaml.Interactions.Core;assembly=Microsoft.Xaml.Interactions" xmlns:interactivity="using:Microsoft.Xaml.Interactivity;assembly=Microsoft.Xaml.Interactions" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <StackPanel Margin="0, 0, 0, 1" Background="#FFFFFFFF">
      <interactivity:Interaction.Behaviors>
         <core:EventTriggerBehavior EventName="Tapped">
            <core:InvokeCommandAction Command="{Binding ElementName=ViewControlGrid, Path=DataContext.ViewActionClickCommand}" CommandParameter="{Binding RecordId}" />
         </core:EventTriggerBehavior>
      </interactivity:Interaction.Behaviors>
      <Grid Width="{Binding ActualWidth, ElementName=ThisStackPanel}" Height="92.5">
         <Grid Width="55.5" Height="55.5" HorizontalAlignment="Left" Margin="18.5, 18.5, 0, 0" VerticalAlignment="Top" Background="#FFFFFFFF">
            <Button Margin="0" Content="{Binding DisplayValues[0]}" Foreground="#FF000000" Command="{Binding ElementName=ViewControlGrid, Path=DataContext.ViewActionClickCommand}" CommandParameter="Image" Style="{StaticResource ButtonNoStyle}" HorizontalAlignment="Center" VerticalAlignment="Center" />
         </Grid>
         <Grid Width="259" Height="92.5" HorizontalAlignment="Left" Margin="92.5, 0, 0, 0" VerticalAlignment="Top" Background="#FFFFFFFF">
            <Button Margin="0" Content="{Binding DisplayValues[1]}" Foreground="#FF000000" Command="{Binding ElementName=ViewControlGrid, Path=DataContext.ViewActionClickCommand}" CommandParameter="" Style="{StaticResource ButtonNoStyle}" HorizontalAlignment="Center" VerticalAlignment="Center" />
         </Grid>
      </Grid>
   </StackPanel>
</DataTemplate>
编辑2:

这是异常 的堆栈跟踪。
The attachable property '%0' was not found in type '%1'. [Line: 3 Position: 379]
   at Windows.UI.Xaml.Markup.XamlReader.Load(String xamlstring)
   at MyNamespace.MyProject.MyFolder.DataTemplates.TemplateConstructor.GetDataTemplateRow(List`1 zcRows, Boolean isPadding)}
编辑3:

确切方法:

 public static DataTemplate GetDataTemplateRow(List<ZCRow> zcRows , bool isPadding = false)
        {
            string rowsXaml = "<StackPanel Margin = '" + GetCardMargins(isPadding) 
                + "' Background='#FFFFFFFF' >";
            //template.
            rowsXaml += @"<interactivity:Interaction.Behaviors><core:EventTriggerBehavior EventName='Tapped'><core:InvokeCommandAction Command='{Binding ElementName=ViewControlGrid, Path=DataContext.ViewActionClickCommand}' CommandParameter='{Binding RecordId}' /></core:EventTriggerBehavior></interactivity:Interaction.Behaviors>";
            foreach(ZCRow row in zcRows)
            {
                rowsXaml += GetRowXaml(row);
            }
            rowsXaml += "</StackPanel>";
            string xaml = @"<DataTemplate " + namespaceString + "  >" + rowsXaml + "</DataTemplate>";
            Debug.WriteLine("Datatemplate is " + xaml);
            try
            {
                return (DataTemplate)XamlReader.Load(xaml);
            }
            catch(Exception e)
            {
                Debug.WriteLine(e.StackTrace);
                return null;
            }
        }

附加属性'%0'在类型'%1'中找不到

问题是您在XMLNS声明中显式地命名了程序集。它们不是正确的程序集引用,所以我只是删除了它们:

xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
  xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' 
  xmlns:core='using:Microsoft.Xaml.Interactions.Core' 
  xmlns:interactivity='using:Microsoft.Xaml.Interactivity'