0

我正嘗試使用全局資源字典,但在嘗試使用它包含的樣式時出現錯誤。在我的App.xaml我:Windows Phone 7:未找到全局資源

<Application.Resources> 
     <ViewModel:ViewModelLocator x:Key="Locator" 
          d:IsDataSource="True" /> 
     <ResourceDictionary x:Key="dict1"> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Themes/ListBox.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Application.Resources> 

中/Themes/ListBox.xaml,我有這樣的:

<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"> 
      .... 

我試圖設置風格:

<ListBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" x:Name="lstCategories" SelectionMode="Extended" Style="{StaticResource CategoryListTemplate}" ... 

然而,當Viw加載時出現錯誤 - 「XamlParseException - 找不到名稱/關鍵字CategoryListTemplate [Line:30 Position:42]的資源」。第42行是包含Style="{StaticResource CategoryListTemplate}"的列表框定義的行。

LitBox.xaml的構建操作被設置爲資源,據我所知這應該工作不應該嗎?

回答

0

我解決了這個使用App.xaml.cs以下代替:

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); 
      }