Rajat Saxena
Rajat Saxena

Reputation: 3925

XamlReader throwing up undeclared prefix error

I defined a resource as follows:

<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
            <Grid x:Name="grdArticle" Height="190" Width="190" toolkit:TiltEffect.IsTiltEnabled="True">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <Image Source="samplePhoto.jpg" Grid.Row="0" Stretch="UniformToFill"/>
                <Grid Grid.Row="0" Height="55" VerticalAlignment="Bottom">
                    <Rectangle Grid.Row="0" Fill="{StaticResource PhoneAccentBrush}"/>
                    <TextBlock Grid.Row="0" Text="{Binding Title}" Canvas.ZIndex="2" VerticalAlignment="Bottom" TextWrapping="Wrap" Margin="5,5,5,5" Height="50" FontStyle="Normal">
                        <TextBlock.Foreground>
                            <SolidColorBrush Color="#BFFFFFFF"/>
                        </TextBlock.Foreground>
                    </TextBlock>
                </Grid>
            </Grid>
        </DataTemplate>

When I'm trying to parse it using:

DataTemplate dtTmplt = XamlReader.Load(PhoneApp5.Resource1.Datatemplate_lst) as DataTemplate;

I'm getting XamlParseException,saying "undeclared prefix" at line 2,position 104. I have tried many xmlns tags from the internet,but nothing seems to work.Any help?

Upvotes: 0

Views: 1041

Answers (1)

Luke Woodward
Luke Woodward

Reputation: 64969

It's the toolkit prefix that is undeclared. You need to add

xmlns:toolkit="..."

to either the <DataTemplate> or the outer <Grid> element. Replace ... with whatever you're binding xmlns:toolkit to elsewhere in your application.

Incidentally, is there a reason you're parsing the contents of a resource as XAML this way? Normally, I'd put a DataTemplate in a Resources resource-dictionary somewhere, or in Application.Resources. That way, the compiler would check that it is valid XAML.

Upvotes: 1

Related Questions