Задайте вопросЗадайте вопрос
 

ОтвеченоDeriving A Window From Another Custom Window

  • 16 июня 2007 г. 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.

Ответы

  • 18 июня 2007 г. 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.

  • 20 июня 2007 г. 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.

Все ответы

  • 16 июня 2007 г. 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.
  • 16 июня 2007 г. 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
  • 16 июня 2007 г. 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.
  • 17 июня 2007 г. 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.
  • 18 июня 2007 г. 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.

  • 20 июня 2007 г. 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.

  • 30 января 2008 г. 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.