我正在为一个我们无法控制的棱镜应用程序编写模块。要求是在其中一个区域中显示Web浏览器控件。不幸的是,每个窗口都派生自一个具有AllowsTransparency=true
的CustomWindow类。让AllowTransparency=true
阻止WebBrowser控件显示。
我可以右键单击并将鼠标悬停在控件上并知道有一个网页加载(谷歌),所以我几乎可以肯定我面临的问题与透明度和win32控件(其中的WebBrowser)有关根据我的知识,它是一个包裹的win32控件。)
所以,我已经决定我唯一的行动方法是尝试覆盖Window样式,关闭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。我能够覆盖这种风格吗?或者我是否尝试错误地执行此操作?
答案 0 :(得分:1)
对于Windows,设置隐式样式在每种情况下都不起作用。您必须为该样式指定一个键,并找到一种在需要该窗口的窗口上显式设置样式的方法。
使用ResourceKey
可能会有所帮助,具体取决于您的架构。