I’ve defined a DataTemplate in a ResourceDictionary ‘style1.xaml’:
<ResourceDictionary
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<DataTemplate x:Key="BlogDataTemplate">
<Grid Margin="0,0,6,20" Width="400" Height="210">
<Grid VerticalAlignment="Bottom" Background="#A6000000">
<TextBlock Text="{Binding title}" Style="{StaticResource PhoneTextExtraLargeStyle}" Foreground="White" Margin="6" FontSize="25" TextWrapping="Wrap"/>
</Grid>
</Grid>
</DataTemplate>
</ResourceDictionary>
While in the App.xaml.cs, I use the following code to merge the ResourceDictionary:
void LoadDictionary()
{
var dictionaries = Resources.MergedDictionaries;
string source = string.Empty;
var themeStyles = new ResourceDictionary { };
switch (Settings.fontStyle.Value)
{
case 0:
source = String.Format("/app;component/Themes/style1.xaml");
themeStyles.Source = new Uri(source, UriKind.Relative);
dictionaries.Add(themeStyles);
break;
case 1:
source = String.Format("/app;component/Themes/style2.xaml");
themeStyles.Source = new Uri(source, UriKind.Relative);
dictionaries.Add(themeStyles);
break;
default: break;
}
}
With debugging I can make sure that the style1.xaml had been merged, then in the MainPage.xmal I have a Listbox, I define the ItemTemplate as
<ListBox x:Name="listbox" ItemsSource="{Binding}" CacheMode="BitmapCache" ItemTemplate="{StaticResource BlogDataTemplate}"/>
But when I deployed the app, it caused a “Unspecified error “.
So how to access DataTemplate in ResourceDictionary in Windows Phone?
Thanks in advance.
Instead of the LoadDictionary code, try merging the ResourceDictionary with Xaml in App.xaml by doing something like (in App.xaml, in Application.Resources):