【问题标题】:Windows Phone 7: Global Resource Not FoundWindows Phone 7:未找到全局资源
【发布时间】:2011-04-09 18:45:22
【问题描述】:

我正在尝试使用全局资源字典,但是当我尝试使用其中包含的样式时出现错误。在我的 app.xaml 中有:

 <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 的构建操作设置为 Resource,据我所知,这应该可以工作,不是吗?

【问题讨论】:

    标签: windows-phone-7 datatemplate styles resourcedictionary windows-phone


    【解决方案1】:

    this answer to a related question

    手机好像不支持这种方式合并字典。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-17
        • 1970-01-01
        • 1970-01-01
        • 2013-03-17
        • 1970-01-01
        • 2015-04-06
        • 1970-01-01
        相关资源
        最近更新 更多