好吧,这似乎很难,或者我错过了一些明显的东西。
我想创建可重复使用的 WINDOW ,它将在所有产品中使用。这意味着该控件位于 WPF.Controls 程序集中。 Themes/Generic.xaml 不是一个解决方案,我需要为窗口提供自己的代码,例如自定义消息挂钩等。
这是我在 WPF.Controls.dll 中的代码:
public class CustomWindow : Window
{
static CustomWindow()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(CustomWindow),
new FrameworkPropertyMetadata(typeof(CustomWindow)));
}
现在,在另一个程序集中,我创建 XAML 文件并尝试使用它:
<controls:CustomWindow x:Class="Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:WPF.Controls;assembly=WPF.Controls"
WindowStartupLocation="CenterScreen">
<TextBlock Text="TESTING" />
</controls:CustomWindow>
我看到的是:大黑屏,没有别的,没有别的(大黑色矩形 - 没有标题栏)。任何人都可以阐明这一点吗?通过谷歌搜索,我发现其他人也遇到了同样的问题,所以我想这不是我特有的。
禁用硬件渲染没有帮助。
您需要从 CustomWindow 类中删除静态构造函数。设置 DefaultStyleKey 的目的是帮助 WPF 找到应在 Themes/Generic.xaml 中定义的默认模板。但既然您不想这样做,那么您需要将其删除。
我通过将 CustomWindow 类添加到类库项目(必须导入相当多的依赖项)来测试您的代码,然后在 WPF 项目中使用它。当你的构造函数就位时,窗口的所有内容都是黑色的,一旦我将其删除,一切都会完美运行。
This http://msdn.microsoft.com/en-us/library/ms745025%28v=vs.110%29.aspx是制作自己的控件的好资源
// Chris Eelmaa:这也是正确的,我还想补充一点,它也是
可以将 Themes/Generic.xaml 添加到您的 dll 中,然后您需要
添加程序集ThemeInfo
DLL (AssemblyInfo.cs) 的属性,
为了让它工作:
// http://blogs.magnatis.com/tim/dude-wheres-my-default-style
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific
// resource dictionaries are located
ResourceDictionaryLocation.SourceAssembly //where the
// generic resource dictionary is located
)]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)