创建一个可重用的窗口控制 [英] Creating a reusable WINDOW control
问题描述
好吧,这似乎是很辛苦,还是我失去了一些东西明显。
我想创建可重用的窗口,这将在所有产品中使用。这意味着该控制是WPF.Controls组件内。主题/ Generic.xaml是不是一个解决方案,我需要提供我自己的代码为窗口,如自定义消息钩子等。
下面是我在WPF代码.Controls.dll:
公共类CustomWindow:窗口
{
静态CustomWindow()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof运算(CustomWindow),
新FrameworkPropertyMetadata(typeof运算(CustomWindow)));
}
现在,另一个程序,我创建XAML文件,并尝试使用这个:
<控制:CustomWindow X:类=Views.MainWindow
的xmlns =HTTP://模式.microsoft.com / WinFX的/ 2006 / XAML /演示
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:控制=CLR的命名空间:WPF.Controls;装配= WPF.Controls
WindowStartupLocation =中心屏幕>
< TextBlock的文本=测试/>
< /控制:CustomWindow>
我看到:大黑的屏幕,没有别的,无所不及(黑色大矩形 - 没有标题栏)。任何人都可以摆脱在这个一些启示?带着几分google搜索,我发现,别人有同样的问题,所以我想它不是具体的我。
禁用硬件渲染于事无补。
您需要从您删除静态构造函数CustomWindow级。设置DefaultStyleKey的目的是帮助WPF找到你defaulttemplate应在主题/ Generic.xaml定义。但既然你不想这样做,那么你需要将其删除。
我加入CustomWindow类类库项目测试你的代码(不得不进口相当多的依赖),然后用它在一个WPF项目。有了你的构造所有窗口的内容是黑色的,有一次我删除一切都工作正常。
的这是在做自己的控件
一个很好的资源
//克里斯Eelmaa:这是正确的,另外,我想补充一点,它也
可以添加主题/ Generic.xaml到您的DLL,然后你需要
装配ThemeInfo
属性添加到您的DLL(AssemblyInfo.cs中),
,以便为它工作:
块引用>
// http://blogs.magnatis.com/tim/dude-wheres-my-default风格
[总成:ThemeInfo(
ResourceDictionaryLocation.None,//其中主题特定
//资源字典都位于
ResourceDictionaryLocation.SourceAssembly //其中
/ /通用资源字典位于
)
Okay, this seems to be very hard, or I am missing something obvious. I want to create reusable WINDOW which will be used all over the products. It means that the control is inside WPF.Controls assembly. Themes/Generic.xaml is not a solution, I need to provide my own code for the window, such as custom message hook, etc.
Here is my code in WPF.Controls.dll:
public class CustomWindow : Window { static CustomWindow() { DefaultStyleKeyProperty.OverrideMetadata( typeof(CustomWindow), new FrameworkPropertyMetadata(typeof(CustomWindow))); }
Now, in another assembly, I create XAML file and try to use this:
<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>
What I see: big black screen, nothing else, nothing less(big black rectangle - no caption bar). Can anyone shed some light over this? With a bit of googling, I found that somebody else had same problem, so I guess it's not specific to me.
Disabling hardware rendering doesn't help.
解决方案You need to remove the static constructor from you CustomWindow-class. The purpose of setting the DefaultStyleKey is to help WPF find your defaulttemplate that should be defined in Themes/Generic.xaml. But since you don't want to do that then you need to remove it.
I tested your code by adding the CustomWindow class to a class library project (had to import quite a few dependencies) and then used it in a WPF project. With your constructor in place all of the content of the window was black, and once I removed it everything worked perfectly.
This is a good resource on making your own controls
// Chris Eelmaa: This is correct, also, I'd like to add that it's also possible to add Themes/Generic.xaml to your dll, and then you need to add the assembly
ThemeInfo
attribute to your DLL (AssemblyInfo.cs), in order for it to work:
// 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 )]
这篇关于创建一个可重用的窗口控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!