【问题标题】:Creating a reusable WINDOW control创建可重用的 WINDOW 控件
【发布时间】:2013-11-05 17:10:53
【问题描述】:

好的,这似乎很难,或者我遗漏了一些明显的东西。 我想创建将在所有产品中使用的可重复使用的窗口。这意味着该控件位于 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>

我所看到的:黑色大屏幕,仅此而已(黑色大矩形 - 没有标题栏)。任何人都可以对此有所了解吗?通过谷歌搜索,我发现其他人也有同样的问题,所以我想这不是我特有的。

禁用硬件渲染没有帮助。

【问题讨论】:

  • MahApps.Metro Project 可以让你拥有一个可复用的Window,这是一个开源项目,所以你可以看看这些人是怎么做的。
  • .NET 的哪个版本?哪个版本的 Visual Studio?
  • .NET 4.0 和 Visual Studio 2012。我怀疑它也可以在其他版本中重现。
  • 我真的很好奇这个问题的解决方案。如果您能解决此问题,请随时通知我们。我能够重现它,但我不认为找到修复程序会如此棘手。我已经用尽了所有的想法。 :)
  • 我的应用遇到了同样的问题。 App.xaml 中的&lt;ResourceDictionary.MergedDictionaries&gt; &lt;/ResourceDictionary.MergedDictionaries&gt; 中有一些异味。你能发布你的 App.xaml

标签: c# wpf window


【解决方案1】:

您需要从 CustomWindow 类中删除静态构造函数。设置 DefaultStyleKey 的目的是帮助 WPF 找到应在 Themes/Generic.xaml 中定义的默认模板。但既然你不想这样做,那么你需要删除它。

我通过将 CustomWindow 类添加到类库项目(必须导入很多依赖项)来测试您的代码,然后在 WPF 项目中使用它。使用您的构造函数,窗口的所有内容都是黑色的,一旦我删除它,一切都会完美运行。

This 是制作自己的控件的好资源

// 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 
)]

【讨论】:

  • 我会在几天后尝试这个建议,但据我记得,我尝试了很多不同的组合,包括不带/带等。
猜你喜欢
  • 2012-10-23
  • 1970-01-01
  • 2023-01-19
  • 1970-01-01
  • 1970-01-01
  • 2014-08-16
  • 1970-01-01
  • 1970-01-01
  • 2013-09-05
相关资源
最近更新 更多