【问题标题】:Referencing a StaticResource in XAML在 XAML 中引用静态资源
【发布时间】:2012-04-17 15:57:48
【问题描述】:

我有一个 UserControl,它引用了一个 StaticResource,它通常会在 app.xaml 中引用并且没问题...但是我的程序集是一个库项目,所以没有 app.xaml。我现在如何引用这个 StaticResource?

这是我试图引用它的 UserControl

<UserControl 
   d:DataContext="{Binding Source={x:Type main:IViewModel}, 
   Converter={StaticResource viewModelLocator}}">

这是它通常在 app.xaml 中的位置

<Application xmlns:t="http://schemas.t.com/wpf" xmlns:app="clr-namespace:T.UI">
<Application.Resources>
    <t:ViewModelLocator 
       x:Key="viewModelLocator" 
       Container="{x:Static app:ConfigurationPlugin.Container}" />
</Application.Resources>

我刚刚收到错误消息“无法解析 viewModelLocator”。

【问题讨论】:

    标签: wpf xaml staticresource viewmodellocator


    【解决方案1】:

    将资源添加到 UserControl 的资源本身应该可以解决问题;事实上,几乎每个 Wpf 元素都有一个 Resources 属性。

    <UserControl.Resources> 
      <t:ViewModelLocator x:Key="viewModelLocator"  
           Container="{x:Static app:ConfigurationPlugin.Container}" /> 
    </UserControl.Resources>
    

    【讨论】:

    • 感谢您的回答,我认为这是执行此操作的另一种方法,但收到错误消息“目标调用引发异常”,因此假设我错了。我讨厌一般的错误消息不会告诉你什么是错的!!!我会继续努力解决这个问题。
    • 对于此类异常,请连接调试器。当异常发生时,您可以获取它的详细信息,查看应为您提供更多信息的 innerExceptions 树。
    • 如果我只是运行它,它运行良好,没有错误。尝试使用设计时数据填充我的 xaml 设计视图时收到错误消息。然而,这是另一个与这个问题无关的问题,所以将它留到另一天:-) 再次感谢您的回答
    【解决方案2】:
    <UserControl.Resources>
       ....
    

    除非我不明白你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多