【问题标题】:Inherited Window can not have a name?Inherited Window不能有名字吗?
【发布时间】:2012-07-10 18:31:04
【问题描述】:

我在命名从其基本窗口继承的窗口时遇到问题, 当我尝试为我的窗口命名时,出现以下错误。

BaseWindow 类型不能有 Name 属性。值类型和没有默认构造函数的类型可以用作 ResourceDictionary 中的项。

XAML:

<log:BaseWindow 
   x:Class="EtraabMessenger.MainWindow"
   x:Name="main"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:log="clr-namespace:EtraabMessenger.MVVM.View.Controls" 
   xmlns:VMCore="clr-namespace:EtraabMessenger.MVVM.VMCore" 
   VMCore:WindowClosingBehavior.Closing="{Binding DoCloseMainWindowCommand}"
   Height="464" Width="279">

</log:BaseWindow>

编辑:这是我的 BaseWindow 类

public abstract class BaseWindow : Window, INotifyPropertyChanged
{
    protected BaseWindow()
    {
        // Note (Important) : This message should register on all windows
        // TODO : I'm planning to move this registeration to BaseWindow class
        Messenger.Register<bool>(GeneralToken.ClientDisconnected, DisconnectFromServer);
    }

    protected abstract void DisconnectFromServer(bool isDisconnected);
    protected abstract void RegisterTokens();
    protected abstract void UnRegisterTokens();

    ....
    ....
    ....

}

任何建议都会有所帮助。

【问题讨论】:

  • Values types and types without a default constructor can be used as items within ResourceDictionary. - 真的是“不能”吗?
  • 解决方案是否像在 BaseWindow 类中添加默认(空)构造函数一样简单?
  • @RobertHarvey:实际上消息中没有“不”,如果其他引用错误的情况可以通过......
  • @H.B.我怀疑这是一个错字,因为我希望在原始措辞中看到“仅”这个词。
  • @RobertHarvey:是的,“唯一”会有所帮助...

标签: c# wpf xaml inheritance


【解决方案1】:

您的基本窗口显然,如错误所述,需要一个公共默认构造函数(一个不带参数的构造函数),它也可能不是抽象的,因为需要创建它的一个实例。

【讨论】:

  • @SaberAmani:BaseWindow 无关紧要,您在 XAML 中创建一个继承类型的实例,您需要一个不带参数的构造函数。虽然这是根对象,您可能可以使用x:Arguments,但我不确定。
  • @SaberAmani:等等,当然,基本窗口很重要,这就是 instatiated,抱歉。
  • 还有一点,我的 BaseWindow 是一个抽象类,带有受保护的构造函数,没有任何参数。
  • @SaberAmani:听起来不太好,大多数 XAML 都需要公共成员。
  • @SaberAmani:当您尝试在 XAML 中创建实例时,它确实会导致问题,这仅适用于公共构造函数,也不能是抽象的,我刚刚测试过。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-11
  • 1970-01-01
  • 2019-03-27
  • 1970-01-01
相关资源
最近更新 更多