WPF 共享资源问题 - 没有 App.xaml,没有共享资源

发布于 2024-11-04 18:49:05 字数 475 浏览 3 评论 0原文

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

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

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

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

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

I have run into a little(well big) problem in an application I am working on.

I am working on a module for an application for my company. The application is a WinForm application, but I have been working on a WPF application(not really an application as you will see) that is going to be hosted in this WinForm application when it is complete.

To do this, I am using the WinForm element host, and I have created a "shell" user control, and then other user control windows inside of that shell user control. So it appears as a WPF application, and only uses the WinForm application as its startup project since the WPF application is really only a collection of WPF Controls.

The problem I have ran into is that since I have not created an actual "WPF Application", there is no App.xaml. This has disabled me from using Shared Resources the way I desire, especially XAML shared resources.

Is there a way I can still treat my collection of WPF User Controls as a WPF Application, and somehow use a App.xaml file for my resources. If not, what are my options for using shared resources in my application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2024-11-11 18:49:05

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 资源。

您可以在此处 和

Add a ResourceDictionary (xaml) file to your project (assuming it's a class library - WPF custom control library), merge it on top of the Generic.xaml, then
you will be able to refer to it and your StaticResource will work.

You can also include the resource in the Generic.xaml (or whatever xaml file it is) file itself.

Here is how your current dictionary should look like:

<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>

I initialized a design time instance of the above control in VS2010 and it showed the text (Text is a string DP property I manually added to the CustomControl1), which means it reads the myString resource.

You can find some more specific info here and here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文