我在 ResourceDictionary 'style1.xaml' 中定义了一个 DataTemplate:
<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>
在 App.xaml.cs 中,我使用以下代码合并 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;
}
}
通过调试,我可以确保 style1.xaml 已被合并,然后在 MainPage.xmal 我有一个列表框,我将 ItemTemplate 定义为
<ListBox x:Name="listbox" ItemsSource="{Binding}" CacheMode="BitmapCache" ItemTemplate="{StaticResource BlogDataTemplate}"/>
但是当我部署该应用程序时,它导致了“未指定的错误”。
那么如何在 Windows Phone 中访问 ResourceDictionary 中的 DataTemplate 呢?
提前致谢。