10

我正在尝试在外部 dll 中创建一个名为 DataTemplate.xaml 的 ResourceFile 并在 WP7 页面中使用它。当我在页面标题中执行以下操作时,出现错误

<ResourceDictionary Source="pack://application:,,,/WP7SharedClassLibrary;component/DataTemplate.xaml" />

错误是“当前项目不支持‘应用程序’作为包 URI 的权限组件。”

有没有其他人遇到过这个并解决了这个问题?

4

3 回答 3

13

我已经设法使用以下步骤使其工作:

  1. 使用名为“WP7ExternalResourcesTest”的“Windows Phone 应用程序”应用程序模板创建了一个标准的 WP7 应用程序。
  2. 使用名为“WP7ExternalResourcesTestLibrary”的“Windows Phone 类库”模板向同一解决方案添加了一个项目。
  3. 从库项目中删除了默认的 Class.cs 文件。
  4. 使用“XML 文件”模板添加了一个名为“External.xaml”的文件,并将“构建操作”设置为“页面”。
  5. 将以下 XAML 添加到新的 XAML 文件中:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <SolidColorBrush x:Key="ForegroundBrush" Color="Red" />
    </ResourceDictionary>
    
  6. 构建库项目,然后从 WP7ExternalResourcesTest 项目中添加对它的引用。
  7. 在 WP7ExternalResourcesTest 中,打开 App.xaml 并将Application.Resources部分更改为以下内容:

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/WP7ExternalResourcesTestLibrary;component/External.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    
  8. 在 MainPage.xaml 中,添加Foreground="{StaticResource ForegroundBrush}"TextBlock名为“PageTitle”。
  9. 在模拟器中运行应用程序。最终结果是TextBlock正确地以红色显示“页面名称”。

希望这可以帮助。

于 2011-01-24T14:35:13.457 回答
1

我在尝试共享 XAML ResourceDictionary 文件时尝试了 pack 语法并得到了相同的错误消息。我最终使用了这种语法,它对我有用。

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/MyDLLName;component/Folder/MyXAMLFile.xaml"/>                
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
于 2010-06-02T19:54:40.583 回答
0

Silverlight 不支持包 URI。这是 WPF 的一项功能。

如果您在 Silverlight 中检查对象的Source属性类型,它是. 但在 WPF 中,源是一个依赖属性,类型为ImageUriImageSource.

于 2010-07-12T23:39:12.127 回答