1
我在ResourceDictionary中 'style1.xaml' 定义一个DataTemplate:访问的DataTemplate在资源字典中的Windows Phone
<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?
在此先感谢。
嗨codechinchilla, 我做了什么你已发布在您的答案,但它会抛出异常找不到资源与名称/关键BlogDataTemplate如何摆脱此异常? –
嘿Android_Crazy - 你可以发布你的App.xaml和你正在使用的资源字典xaml吗? – codechinchilla
其实我解决了这个问题。感谢您的回复。 –