windows-runtimemidlwinrt-componentc++-winrt

What is a composable runtime class?


I'm experimenting with creating a simple xaml appilcation using C++/WinRT.

I come from a WPF background where it is pretty common to have a base class that implements INotifyPropertyChanged and have other classes inherit from it.

When I try to do the same with C++-WinRT I fail with an error

error MIDL4006: [msg]A runtime class can derive only from a composable runtime class.

Here is the rest of the relevant code for reference:

ObservableObject.idl:

namespace Example
{
    runtimeclass ObservableObject : Windows.UI.Xaml.DependencyObject, Windows.UI.Xaml.Data.INotifyPropertyChanged
    {
        ObservableObject();
    }
}

MainViewModel.idl:

import "ObservableObject.idl";

namespace Example
{
    runtimeclass MainViewModel : ObservableObject
    {
        MainViewModel();
        Int32 MyProperty;
    }
}

So what exactly is a composable runtime class? Is there a way to achieve this with C++/WinRT? I really don't want to repeat the INotifyPropertyChanged implementation each time I define a new bindable class.


Solution

  • If you inherit from DependencyObject, you actually needn't implement INotifyPropertyChanged, and vice versa (even in WPF).

    Inheriting custom runtimeclass is not recommended in WinRT types, because JavaScript doesn't know inheritance, thus App Cert Kit will fail in that case.

    If you don't want to write the same logic each time, declare a C++ native class and implement the interfaces (DON'T inherit winrt::implement). Let the implementation classes inherit from it.