4

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

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

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

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

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

4

1 回答 1

1

将一个ResourceDictionary(xaml) 文件添加到您的项目中(假设它是一个类库 - WPF 自定义控件库),将其合并Generic.xamlStaticResource.

您还可以将资源包含在 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是我手动添加到的字符串 DP 属性CustomControl1),这意味着它读取了myString资源。

您可以在此处此处找到一些更具体的信息。

于 2011-05-02T17:18:23.240 回答