覆盖 Prism 模块中的 WPF 窗口样式

发布于 2024-12-06 10:06:14 字数 1971 浏览 1 评论 0原文

我正在为我们无法控制的棱镜应用程序编写一个模块。要求是在其中一个区域显示 Web 浏览器控件。不幸的是,每个窗口都派生自 CustomWindow 类,该类具有 AllowsTransparency=true。设置 AllowTransparency=true 会阻止显示 WebBrowser 控件。

我可以右键单击并将鼠标悬停在控件上,并知道已加载网页(google),所以我几乎可以肯定我面临的问题与透明度和 win32 控件有关(其中 WebBrowser 是一个包装的)据我所知,win32 控件)。

因此,我决定我唯一的行动方案是尝试覆盖窗口样式,关闭AllowTransparency。

这是有问题的样式(使用 Reflector 浏览 baml):

<Style x:Key="{x:Type local:CustomWindow}" TargetType="{x:Type local:CustomWindow}">
    <Setter Property="AllowsTransparency" Value="true" />
    ...
</Style>

这就是我尝试删除样式的方式:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Controls="clr-namespace:Vendor.App.WPFCommon.Controls;assembly=Vendor.App.WPFCommon">
    <Style TargetType="{x:Type Controls:CustomWindow}">
        <Setter Property="AllowsTransparency" Value="false" />
    </Style>
</ResourceDictionary>
private void LoadThemeOverrides()
{
    var assemblyName = System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().ManifestModule.Name);
    var overrides = new Uri(string.Format("{0};component/themes/overrides.xaml", assemblyName), UriKind.Relative);
    var themeManager = _container.Resolve<IThemeManager>();
    foreach (var theme in themeManager.ThemeCollection)
        theme.Sources.Add(overrides);
    var rd = new ResourceDictionary {Source = overrides};
    Application.Current.Resources.MergedDictionaries.Add(rd);
    themeManager.ChangeTheme(themeManager.CurrentTheme);
}

ResourceDictionary 正在正确加载,因此问题不是 URI。我调试了rd,我可以在其中看到我的风格。

上面的代码在验证用户/密码的登录窗口和显示的主应用程序窗口之间运行。它们是两个不同的窗口,但是它们都派生自 CustomWindow。

使用 WPF Inspector,我可以看到 CustomWindows 仍然将 AllowTransparency 设置为 true。我可以完全覆盖这种风格吗?或者我试图错误地执行此操作?

I'm writing a module for a prism application that we do not control. The requirement is to show a web browser control in one of the regions. Unfortunately, each window derives from a CustomWindow class, that has AllowsTransparency=true. Having AllowTransparency=true prevents the WebBrowser control from displaying.

I can right click and hover over the control and know there is a web page loaded (google), so I'm nearly certain that the problem I'm facing has to do with transparency and win32 controls (of which the WebBrowser is a wrapped win32 control to my knowledge).

So, I've decided that my only course of action is to try and override the Window style, to turn AllowTransparency off.

This is the offending style (using Reflector to browse the baml):

<Style x:Key="{x:Type local:CustomWindow}" TargetType="{x:Type local:CustomWindow}">
    <Setter Property="AllowsTransparency" Value="true" />
    ...
</Style>

And this is how I'm trying to remove the style:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Controls="clr-namespace:Vendor.App.WPFCommon.Controls;assembly=Vendor.App.WPFCommon">
    <Style TargetType="{x:Type Controls:CustomWindow}">
        <Setter Property="AllowsTransparency" Value="false" />
    </Style>
</ResourceDictionary>
private void LoadThemeOverrides()
{
    var assemblyName = System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().ManifestModule.Name);
    var overrides = new Uri(string.Format("{0};component/themes/overrides.xaml", assemblyName), UriKind.Relative);
    var themeManager = _container.Resolve<IThemeManager>();
    foreach (var theme in themeManager.ThemeCollection)
        theme.Sources.Add(overrides);
    var rd = new ResourceDictionary {Source = overrides};
    Application.Current.Resources.MergedDictionaries.Add(rd);
    themeManager.ChangeTheme(themeManager.CurrentTheme);
}

The ResourceDictionary is being loaded correctly, so it's not the URI that is the problem. I debugged the rd, and I can see my style in there.

The above piece of code is run between the login window validating user/password, and the main application window being displayed. They are two different windows, however, they both derive from CustomWindow.

Using WPF Inspector, I can see that the CustomWindows still have AllowTransparency set to true. Am I able to override this style at all? Or am I attempting to do this incorrectly?

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

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

发布评论

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

评论(1

凉栀 2024-12-13 10:06:14

对于 Windows,设置隐式样式并非在所有情况下都有效。您必须为样式提供一个键,并找到一种方法在需要该键的窗口上显式设置样式。

使用 ResourceKey 可能会有所帮助,具体取决于您的架构。

With windows, setting implicit style will not work in every situation. You have to give a key to the style and find a way to set the style explicitly on the window that requires that.

Using ResourceKey might help, depending upon your architecture.

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