Microsoft 开发人员网络 > 论坛主页 > Windows Presentation Foundation (WPF) > Deriving A Window From Another Custom Window
提出问题提出问题
 

已答复Deriving A Window From Another Custom Window

  • 2007年6月16日 1:19Prog-Bman 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hey guys, I am having a heck of a time with this one. Basically I am trying to derive a window off of another window, which is derived from the plain window shipped with WPF. I know how to derive from in in code but the problem is using it in the xaml. I have done what I have so far found in the inet. This is really the only thing I have seen referring to it on my searches

    <src:myForm
      <!--Other Goodies, Intentional space, get a smile if not-->
      xmlns: src="clr-namespace:mynamespace;assembly=myassembly"
    >
    </>

    The problem with that is I get this error upon compiling

    cannot be the root of a XAML file because it was defined using XAML. Line 2 Position 2.   

    As well as some other messages regarding schema(not errors). What am I missing because this is driving me nuts.

答案

  • 2007年6月18日 15:41Carole Snyder - MSFT 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    It's not that you're defining and using the namespace in the same element.  The following is perfectly legal:  (spaces intentional to avoid emoticons)

     

    <Label Content="{x: Static sys: DateTime.Now}" xmlns: sys="clr-namespace: System;assembly=mscorlib"/>

     

    But it's not possible to inherit a from a custom window in XAML.  I forget the full reason why, but if I recall it has to do with how XAML is loaded.  What I've done in create a class that inherits from a panel such as grid or dock panel and then use that class as the root element for all of my windows.  I don't know if that will work for you though.  You could also try creating a style for Window in App.xaml.

  • 2007年6月20日 6:19Yi-Lun LuoMSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    Hello, Carole is right. You can’t inherit a Window if the parent Window is defined in XAML. But in your case, it seems that it’s not difficult to define your parent Window in pure code. So just use pure code to define your parent Window, and then you can inherit it in XAML.

全部回复

  • 2007年6月16日 4:11Matt Eland 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Looks like basically you're trying to say to XAML:

    1. The root element is myForm within the namespace I defined as src.
    2. I want to define a namespace called src that is located at ...
    I imagine XAML can't get past step one to process step two.

    I'm not aware of any way to define a namespace before the root node, but it might be possible using metadata. Otherwise, I think you're pretty much messed as far as declaring a CustomWindow in XAML. Code should be well enough, but that defeats the point.
  • 2007年6月16日 21:45Prog-Bman 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    So basically there is no inheritance for windows in WPF(xaml), because that makes no sense and is quite lame. Anyone else have a suggestion on how to go about this, there has to be some sort of a workaround or something for the time being
  • 2007年6月16日 23:16Matt Eland 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Well, what do you need a custom Window object for? Is it for styling, added methods, or what?

    If you really can't extend Windows and reference them as root nodes in XAML (which does seem pretty lousy to me), there might be another way to get the functionality you're looking for.
  • 2007年6月17日 22:16Prog-Bman 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    I basically need a window that will do some special control processing that I want all the windows in my application to inherit(formating, etc), so that I don't need some sort of external class, within each window of the application. I mean it is possible to do it that way but it just seems more easily done with some simple inheritance.
  • 2007年6月18日 15:41Carole Snyder - MSFT 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    It's not that you're defining and using the namespace in the same element.  The following is perfectly legal:  (spaces intentional to avoid emoticons)

     

    <Label Content="{x: Static sys: DateTime.Now}" xmlns: sys="clr-namespace: System;assembly=mscorlib"/>

     

    But it's not possible to inherit a from a custom window in XAML.  I forget the full reason why, but if I recall it has to do with how XAML is loaded.  What I've done in create a class that inherits from a panel such as grid or dock panel and then use that class as the root element for all of my windows.  I don't know if that will work for you though.  You could also try creating a style for Window in App.xaml.

  • 2007年6月20日 6:19Yi-Lun LuoMSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    Hello, Carole is right. You can’t inherit a Window if the parent Window is defined in XAML. But in your case, it seems that it’s not difficult to define your parent Window in pure code. So just use pure code to define your parent Window, and then you can inherit it in XAML.

  • 2008年1月30日 21:57the_magic_juan 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    I think MSFT really missed the boat on this one and I hope they fix it...to not be able to inherit xaml is a big miss.  I know somebody is going to say, "just use styles".  What we want to do, declaritively define a base xaml page with wpf controls including the excellent xceed datagrid with events and what not...that's not styles.  wpf is right on for the designer but for shops that are trying out wpf, to not be able to inherit a base xaml page, defined declaritively, defeats the purpose of oop.  I understand that you can create the page all in code and then have your wpf page inherit that.  That's great.  But then you loose the ease and power of the xaml.  I can dynamically generate a xaml page at run time but that does nothing for me in design time.

     

    Anyway, I hope MSFT addresses this.  For the time being we're going to have to go back to winforms and use the host object for presenting wpf - sort of a hybrid application.

     

    Thanks.