질문하기질문하기
 

답변됨Inheriting from inherited user control

  • 2006년 7월 6일 목요일 오전 4:22leovernazza 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    I'm having a problem with a user control generated code, when it inherits from other than UserControl.

    Steps:
    1. Add new User control
    2. Replace in the inheritance declaration UserControl for MyBaseUserControl
    3. Compile

    The generated class part is still inheriting from UserControl.
    You can change the ".g.cs" file, and sometimes you can get it succesfully compiled, but the error cames back again and again :)

    There is another way to do that?
    Thanks in advance...

답변

모든 응답

  • 2006년 7월 6일 목요일 오후 12:36Marc Laroche 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    Quick question:

    Have you changed Root XAML node of the MyBaseUserControl.xaml file to MyBaseUserControl as well?

    Let me know!!!


  • 2006년 7월 6일 목요일 오후 10:11leovernazza 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Yes, it was the first problem. :S

    But then I get: "Error 7 'MyBaseUserControl' cannot be the root of a XAML file because it was defined using XAML."

    I can get it working if I convert it to a CustomControl (no xaml definition), but I loose the declarative inheritance.... mmm... I suppose I still can use templates and styles to reach that.

    Is this a XAML "design decision"?
  • 2007년 10월 2일 화요일 오후 5:44Ed Noepel 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    This seems to be a very important question for which I cannot find an answer.  Can a component defined in XAML be subclassed? This blog post asserts it can be done, but I have not found a way to get it to work.  Like everyone else, I get the following compile-time error when I try to subclass a Window I defined in XAML.

    error MC6017: 'MyCustomWindow' cannot be the root of a XAML file because it was defined using XAML.

    If we cannot subclass Windows defined in XAML, what are the best practices for coupling a base window style with common window functionality throughout an application?
  • 2007년 10월 2일 화요일 오후 9:31SerialSeb- 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    As the error message indicates, you can inherit from a class that inherits from Window, not from a Window you've created that has a .xaml and a .cs file. The former will be perfectly acceptable as a base class, the second will not.

     

    If you want to define a base window style, the way to go is to create a ControlTemplate for Window, and symply apply it through a style.

     

    Code Block

    <Window x:Class="CaffeineIT.Blog.WindowTemplate.Window1"

      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

      Title="Window1" Height="300" Width="300">

        <Window.Template>

            <ControlTemplate TargetType="{x:Type Window}">

                <Border Background="Beige" Padding="20" Margin="20">

                    <ContentPresenter />

                </Border>

            </ControlTemplate>

        </Window.Template>

        <TextBlock>Oh, I'm the content!</TextBlock>

    </Window>

     

     

  • 2007년 10월 4일 목요일 오전 1:19Ed Noepel 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Agreed.  The ctor of my BaseWindow currently sets its own Template property to a ControlTemplate (stored in a ResourceDictionary) defining the look-and-feel.  This is because I do not want to force all Windows deriving from BaseWindow to grab the ControlTemplate from the ResourceDictionary and set it explicitly.  It is important that classes deriving from my BaseWindow use the same ControlTemplate because some code in the BaseWindow class is needed to define the look (doing things not possible in XAML alone).

    The following post makes some good arguments about where inheritance should be used: markup subclassing. In my case, I am using inheritance for more than just polymorphism.  I am doing so because I must be fascist about the look of the derived Window.