0

I want to extract common code from a few WinRT components to one base class so I don't need to copy&past it. I have the following base class:

[Windows::Foundation::Metadata::WebHostHidden]
ref class ExpandableView : public Windows::UI::Xaml::DependencyObject
{
public:
    static void onIsExpandedChanged(Windows::UI::Xaml::DependencyObject^ object, 
        Windows::UI::Xaml::DependencyPropertyChangedEventArgs^ arguments);
public:
    property bool IsExpanded
    {
        bool get(){return (bool)GetValue(IsExpandedProperty);}
        void set(bool value){SetValue(IsExpandedProperty, value);}
    }
    static property Windows::UI::Xaml::DependencyProperty^ IsExpandedProperty
    {
        Windows::UI::Xaml::DependencyProperty^ get(){return  _IsExpandedProperty;}
    }
protected:
    ExpandableView();
    virtual void viewExpanded();
    virtual void viewCollapsed();
private:
    void _expand();
    void _collapse();
private:
    static Windows::UI::Xaml::DependencyProperty^ _IsExpandedProperty;
};

And I create a few User Controls which should be somehow inherited from this base class. And it is not possible to do it the way I want because winrt class can inherit only one ref class and other should be interfaces. But I need this very class which has dependency property which has some logic when it is set and I don't want to copy&past this property across all my classes. So the question is: how to achieve it with WinRT?

ixSci
  • 13,100
  • 5
  • 45
  • 79

1 Answers1

1

Have you tried using a template and inheritance of the specific class needed:

template<typename BaseClass>
ref class ExpandableView : public BaseClass;

Now the subclasses reusing ExpandableView can inherit whatever they need, not only Windows::UI::Xaml::DependencyObject.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • But ExpandableView does need DependencyObject since it has dependency property; besides the problem is not in ExpandableView base class but in ExpandableView **as** base class. – ixSci Jan 28 '13 at 05:18
  • @ixSci: You didn't give a full example in the question, so I couldn't give a full example in the answer. Describe the derived class better, please. What additional base class(es) does it need? Is the other base class derived from `DependencyObject`? – Ben Voigt Jan 28 '13 at 14:36
  • Ben Voigt, it is User Control. so it has xaml file, 2 code behind files and one generated file. And in the generated file the partial class inherits ::Windows::UI::Xaml::Controls::UserControl, public ::Windows::UI::Xaml::Markup::IComponentConnector – ixSci Jan 28 '13 at 17:09
  • @ixSci: So the inheritance chain you need is Object, DependencyObject, UIElement, FrameworkElement, Control, UserControl, ExpandableView, MyUserControl – Ben Voigt Jan 28 '13 at 19:38
  • And the next one could be Object, DependencyObject, UIElement, FrameworkElement, Control, ContentControl, ButtonBase, Button, ExpandableView – Ben Voigt Jan 28 '13 at 19:40
  • I see your point but I believe this way is not available. At least I don't see how I can change parent in the partial class since it is generated by tool and re-genereated each time I change xaml. – ixSci Jan 29 '13 at 04:01
  • @ixSci: Surely XAML allows you to specify that. – Ben Voigt Jan 29 '13 at 14:28
  • http://stackoverflow.com/questions/887519/how-can-a-wpf-usercontrol-inherit-a-wpf-usercontrol – Ben Voigt Jan 29 '13 at 14:29
  • It works! Thank you. Note, however I didn't use templated class which seems can't be a solution here due to visibility restriction of the public WinRT classes. And I have to use fancy MS keyword for the constructor: it should have internal visibility, so instead of using pirvate you should use internal. – ixSci Jan 30 '13 at 06:08
  • I have the exact same issue, but when I replace the Page tag with my BaseClass, like local:BaseClass, it say that it cannot find the BaseClass in local namespace. Have you a full example of BaseClass, SubClass and following XAML ? – Christophe Gigax Sep 16 '15 at 15:39