0

我正在尝试实施以下解决方案:https ://stackoverflow.com/a/1486481/154439 。在作者的解决方案中,他写道,

我创建了一个继承自 Window 的抽象 ApplicationWindowBase 类。

我不确定他在这里是什么意思。我尝试将我的 XAML 从更改<Window ...<ApplicationWindowBase .... 没运气。

窗口的设计器生成文件继承自 System.Windows.Window,但由于该文件是设计器生成的,因此对其进行更改对我没有好处。我尝试了各种其他方法,但均未成功。

我对 WPF 和 MVVM 都很陌生。我错过了一些明显的东西吗?

4

2 回答 2

1

与凯文的评论一起使用的代码:

代码背后

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : ApplicationWindowBase
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

XAML

<myPrefix:ApplicationWindowBase x:Class="StackOverflow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:myPrefix="clr-namespace:StackOverflow"
        Title="TestWindow" Height="300" Width="300">
    <Grid>

    </Grid>
</myPrefix:ApplicationWindowBase>

只要确保你使用正确的命名空间,我的恰好是 StackOverflow。

于 2013-09-16T18:02:58.307 回答
1

首先,xaml 与否,这些都是类。因此,任何从另一个类(即 Window)继承的东西,都必须像任何其他类一样。

namespace SomeNamespace
{
    public sealed class MyWindow : Window
    {
        public object SomeNewProperty {get;set;}
    }
}

xaml 只是一种不同类型的序列化,具有自己的一组规则。但它仍然是序列化的 XML,因此所有内容都必须可追溯到其原始类型。

xaml 如何做到这一点是通过 XML 中称为“命名空间”的工具。它们非常符合我们作为 C# 开发人员对命名空间的理解,因为我们必须将 XML 命名空间映射到应用程序中的命名空间,以便 xaml 序列化程序可以将 XML 元素与 CLR 类型匹配。

Xaml 使用几种不同的方式来匹配这些命名空间。一个看起来像一个 URL,由程序集属性定义(例如,“xmlns =“http://microsoft.com/whatevs”)。当程序集在您正在编写的程序集之外时,此方法有效。在您的情况下,您必须使用其他方法来识别您的命名空间。这个专门的 XML 命名空间由 xaml 序列化程序解析,以识别命名空间和包含类的程序集。它的形式为“clr-namespace:SomeNamespace;assembly =SomeAssembly”。如果类型在当前程序集中,则可以省略程序集位。

要将它们与上面的示例放在一起,您将创建一个新窗口的实例,如下所示:

<t:MyWindow 
    xmlns:t="clr-namespace:SomeNamespace" 
    xmlns="the standard microsoft namespace here"
    t:OmitOtherStuffBecauseThisIsAnExampleLol="true">
于 2013-09-16T18:03:17.240 回答