I'm trying to use a global resource dictionary, but I'm getting an error when I try to use a style it contains. In my app.xaml I have:
<Application.Resources>
<ViewModel:ViewModelLocator x:Key="Locator"
开发者_JAVA技巧 d:IsDataSource="True" />
<ResourceDictionary x:Key="dict1">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/ListBox.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The in /Themes/ListBox.xaml, I have this:
<Style x:Key="CategoryListTemplate" TargetType="ListBox">
<Setter Property="Background" Value="Transparent" />
<Setter Property="SelectionMode" Value="Extended" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ListBoxItem">
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
....
I'm trying to set the style with:
<ListBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" x:Name="lstCategories" SelectionMode="Extended" Style="{StaticResource CategoryListTemplate}" ...
Yet I get an error when the Viw is loading - "XamlParseException - Cannot find a Resource with the Name/Key CategoryListTemplate [Line: 30 Position: 42]". Line 42 is the line which contains the listbox definition with Style="{StaticResource CategoryListTemplate}"
.
The build action for LitBox.xaml is set to Resource, as far as I can tell this should work shouldn't it?
See this answer to a related question.
It seems the phone doesn't support merging dictionaries in this way.
I solved this using the following in App.xaml.cs instead:
var dictionaries = Resources.MergedDictionaries;
dictionaries.Clear();
var dicts = new[]{
"/ChickenPing.Mobile;component/Themes/ThemeResources.xaml",
"/ChickenPing.Mobile;component/Themes/generic.xaml",
"/ChickenPing.Mobile;component/Themes/ListBox.xaml",
"/ChickenPing.Mobile;component/Themes/Rating.xaml",
"/ChickenPing.Mobile;component/Themes/CheckBox.xaml",
};
foreach (var dict in dicts) {
var themeStyles = new ResourceDictionary {Source = new Uri(dict, UriKind.Relative)};
dictionaries.Add(themeStyles);
}
精彩评论