简体   繁体   中英

The attachable property '%0' was not found in type '%1'

I'm trying to load xaml using XamlReader.Load(xamlstring) in a PCL(for windows 8.1 & phone 8.1) project, but I'm always hitting the exception "Windows.UI.Xaml.Markup.XamlParseException" at the below line:

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>";

And this is the namespace declaration

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' ";

Any help to solve this issue? or What Im doing wrong?

EDIT:

The Exact Xaml string I'm trying to load is

    <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>

EDIT 2:

This is the stacktrace of the excepton

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)}

EDIT 3:

The Exact Method:

 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;
            }
        }

The problem is that you were explicitly naming the assemblies in your XMLNS declaration. They were not the correct assembly references, so I just removed them:

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'

That works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM