I wanted to create an abstract base class for all of my view models, but I’m running into accessibility issues and can’t seem to navigate through the errors. Here’s an example of my code:
public ref class ViewModelBase {
...
}
public ref class OtherViewModel : ViewModelBase {
...
}
When I define my code as state above, I get the following error:
error C4585: ‘MyNamespace::ViewModelBase’ : A WinRT ‘public ref class’ must either be sealed or derive from an existing unsealed class
If, instead, I make ViewModelBase private, I get this error:
error C3213: base class ‘MyNamespace::ViewModelBase’ is less accessible than ‘OtherViewModel’
This seems like it should be incredibly simple. What am I doing wrong?
What you are attempting is not strictly possible in C++/CX, as in VS 2012 C++/CX does not support public class inheritance for general-purpose scenarios. It is only supported enough to have the XAML scenarios work; the only possible public unsealed types are those found under the
Windows::UI::Xamlnamespace, and users are not able to author their own public unsealed types.If you want to have a base class for all your view models, your only options to make all your classes private or to inherit from a base class from
Windows::UI::Xaml(such asDependencyObject).Each of these approaches has drawbacks:
[Bindable]attribute to do databinding, so you would need to have a private databinding implementation. (Your private class would need to implementICustomPropertyProviderand related interfaces.)