locked
Is is possible to derive from a ref class defined in an outside module

    Question

  • I'd like to encapsulate the RaisePropertyChanged logic into the ref class  and use it as a base class like this:

     namespace ViewModels
     {
      [Windows::Foundation::Metadata::WebHostHidden]
      public ref class ViewModelBase : Windows::UI::Xaml::DependencyObject, INotifyPropertyChanged
      {
      public:
       //ViewModelBase();
       virtual event PropertyChangedEventHandler^ PropertyChanged;
       void RaisePropertyChanged(String^ propertyName);
      };
     }

    void ViewModelBase::RaisePropertyChanged(String^ property)
    {
     CoreDispatcher^ dispatcher = CoreApplication::MainView->CoreWindow->Dispatcher;
     dispatcher->RunAsync(CoreDispatcherPriority::Normal,
            ref new Windows::UI::Core::DispatchedHandler([=] () -> void
           {
           PropertyChanged(this, ref new PropertyChangedEventArgs(property));
           }));
    }

    But I cannot seem to derive from it in a different project. The ctor() is automatically generated as internal, so this base type is not activatable (NARC - NARC – Non-Activatable-Runtime-Class).  Does this mean that this use case is not supported by WinRT?

    Sunday, December 30, 2012 4:37 PM

All replies

  • Yes, But ALL the components and the interface must be in the SAME namespace.

    For more information, please check
    http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/cb06c393-bcf2-4302-96bb-1ca1f06709d3/

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jesse Jiang Friday, January 04, 2013 1:44 AM
    • Unproposed as answer by Feinberg, Alex Sunday, January 06, 2013 4:17 PM
    Wednesday, January 02, 2013 6:09 AM
  • Yes, But ALL the components and the interface must be in the SAME namespace.

    For more information, please check
    http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/cb06c393-bcf2-4302-96bb-1ca1f06709d3/

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Does not work.  Even if I keep the namespace same as the base class it does not compile:

    error C2248: 'RSSParser::ViewModels::ViewModelBase::ViewModelBase' : cannot access private member declared in class 'RSSParser::ViewModels::ViewModelBase

    Perhaps I was not clear on what I was trying to do:

    DLL A:

    namespace RSSParser
    {
    	namespace ViewModels
    	{
    		[Windows::Foundation::Metadata::WebHostHidden]
    		public ref class ViewModelBase : Windows::UI::Xaml::DependencyObject, INotifyPropertyChanged
    		{
    		public:
    			//ViewModelBase();
    			virtual event PropertyChangedEventHandler^ PropertyChanged;
    			void RaisePropertyChanged(String^ propertyName);
    		};
    	}
    }

    Dll B:

    namespace NewsReader
    {
    	namespace ViewModels
    	{
    		[Bindable]
    		public ref class FeedViewModel sealed : public RSSParser::ViewModels::ViewModelBase
    		{

    This gives me a compilation error above.  If I try to make an explicit ctor in the base class, I get this error:

    error C2456: 'NewsReader::ViewModels::ViewModelBase' an unsealed ref class cannot have a public visible constructor. Seal the class, or specify constructor as 'internal', 'protected private' or 'private' instead.

    So I cannot create a public constructor and I cannot derive from a class with an internal one. That means that I cannot derive from a ref Class in defined in another dll.

    Right?


    Sunday, January 06, 2013 4:16 PM