【问题标题】:Adding resources into Silverlight application将资源添加到 Silverlight 应用程序
【发布时间】:2010-07-11 20:46:05
【问题描述】:

我正在尝试将资源字典添加到我的 silverlight-4 应用程序中(在 http://msdn.microsoft.com/en-us/magazine/dd419663.aspx 文章的“将视图应用于视图模型”一章中建议)。

第一个问题:我在 MainPage 中看不到任何资源。我是否正确理解我需要手动将资源字典添加到 Silverlight 应用程序中?

第二个:当我这样做时,在 Dictionary1.xaml 文件中

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib">
    <DataTemplate DataType="{x:Type vm:MyViewModel}">
        <vw:MyView />
    </DataTemplate>
</ResourceDictionary>

我收到一个错误:无法解析符号“DataType”...

有什么好主意吗?

【问题讨论】:

  • 命名空间前缀“vm”和“vw”似乎未定义。

标签: silverlight silverlight-4.0


【解决方案1】:

ad 1:MainPage 有一个 ResourceDictionary。您可以像这样在 xaml 中向其中添加元素:

<MainPage>
  <MainPage.ResourceDictionary>
    <DataTemplate>
      <vw:MyView />
    </DataTemplate>
  </MainPage.ResourceDictionary>
  ...

您可以使用 ResourceDictionary 的 Source 和 MergedDictionaries 属性将 ResourceDictionary 添加到 MainPage.ResourceDictionary:

<MainPage>
  <MainPage.ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="Dictionary1.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </MainPage.ResourceDictionary>
  ...

ad 2:DataTemplate 在 Silverlight 框架中没有 Property DataType。 :-(

【讨论】:

  • 感谢您提供信息...您能否让我知道是否有可能使用 Silverlight 中的资源?提前谢谢你
【解决方案2】:

如果要在 ResourceDictionary 中,您还需要将 x:Key 添加到 DataTemplate 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 2016-08-09
    • 2015-02-20
    • 2013-08-08
    相关资源
    最近更新 更多