【问题标题】:The attachable property '%0' was not found in type '%1'在类型“%1”中找不到可附加属性“%0”
【发布时间】:2015-09-11 09:31:17
【问题描述】:

我正在尝试在 PCL(适用于 windows 8.1 和 phone 8.1)项目中使用 XamlReader.Load(xamlstring) 加载 xaml,但我总是在以下行中遇到异常 "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;
            }
        }

【问题讨论】:

  • 例如,如果您尝试解析行 string="&lt;interactivity:Interaction.Behaviors&gt;&lt;/interactivity:Interaction.Behaviors&gt;" 会发生什么?
  • @Oliver 发生同样的异常。
  • 你能显示你正在使用的确切代码吗?两个 sn-ps 名称变量 contentnamespaceString 但您的问题提到了 xamlString
  • @PeterTorr-MSFT 更新了我的问题。
  • @MohanRajNK - 粘贴整个异常和堆栈跟踪。

标签: c# windows-runtime windows-phone-8.1 portable-class-library xamlparseexception


【解决方案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'

这行得通。

【讨论】:

  • 我尝试了您上面提到的步骤,但仍然出现相同的异常。我在这里添加了视频,重现了这个问题 - 1drv.ms/1F26430@PeterTorr-MSFT
  • 您的 ClassLibrary1 引用了 Behavior 程序集,这是一个错误(在 VS 中有一个黄色的“!”图标),并且存在编译器错误 :-)。但我会更新我的答案来解决你的问题。
  • 我删除了声明中的那些程序集命名,但我仍然面临同样的问题。这是我使用的参考路径 "C:\Program Files (x86)\Microsoft SDKs\WindowsPhoneApp\v8.1\ExtensionSDKs\BehaviorsXamlSDKManaged\12.0\References\CommonConfiguration\Neutral\" 。我也试过这个路径“C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1\ExtensionSDKs\BehaviorsXamlSDKManaged\12.0\References\CommonConfiguration\Neutral”,但它们都不起作用!
  • 我没有向 PCL 添加任何程序集
猜你喜欢
  • 2013-01-12
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 2020-11-08
  • 2015-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多