【问题标题】:WPF Shared Resource Problem - No App.xaml, No Shared ResourcesWPF 共享资源问题 - 没有 App.xaml,没有共享资源
【发布时间】:2011-08-17 03:13:41
【问题描述】:

我在正在处理的应用程序中遇到了一个小(很大)问题。

我正在为我的公司开发一个应用程序模块。该应用程序是一个 WinForm 应用程序,但我一直在开发一个 WPF 应用程序(不是您将看到的真正的应用程序),该应用程序将在此 WinForm 应用程序完成后托管。

为此,我使用了 WinForm 元素宿主,并创建了一个“shell”用户控件,然后在该 shell 用户控件内创建了其他用户控件窗口。所以它显示为一个 WPF 应用程序,并且只使用 WinForm 应用程序作为其启动项目,因为 WPF 应用程序实际上只是 WPF 控件的集合。

我遇到的问题是,由于我没有创建实际的“WPF 应用程序”,因此没有 App.xaml。这使我无法按照我想要的方式使用共享资源,尤其是 XAML 共享资源。

有没有办法我仍然可以将我的 WPF 用户控件集合视为 WPF 应用程序,并以某种方式将 App.xaml 文件用于我的资源。如果不是,我有哪些选择可以在我的应用程序中使用共享资源。

【问题讨论】:

    标签: c# wpf resourcedictionary staticresource


    【解决方案1】:

    ResourceDictionary (xaml) 文件添加到您的项目中(假设它是一个类库 - WPF 自定义控件库),将其合并到Generic.xaml 之上,然后 您将能够参考它,您的StaticResource 将起作用。

    您还可以将资源包含在 Generic.xaml(或任何 xaml 文件)文件本身中。

    这是您当前字典的外观:

    <ResourceDictionary
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:sys="clr-namespace:System;assembly=mscorlib"   
      xmlns:local="clr-namespace:WpfCustomControlLibrary1">
    
      <sys:String x:Key="myString">sdfasdf</sys:String>
    
      <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Text" Value="{StaticResource myString}"/>
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl1}">
              <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}">
                <TextBlock Text="{TemplateBinding Text}"/>
              </Border>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </ResourceDictionary>
    

    我在 VS2010 中初始化了上述控件的设计时实例,它显示了文本(Text 是我手动添加到CustomControl1 的字符串 DP 属性),这意味着它读取了myString 资源。

    您可以找到一些更具体的信息herehere

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      • 2021-10-10
      • 2015-10-27
      • 1970-01-01
      • 2022-12-12
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多