realesmedia
Topic Author
Posts: 85
Joined: 18 May 2016, 10:26

[C++] property update

14 Feb 2017, 14:21

I want to update two properties, but my version doesn't work. Can you explain the solution?
<Canvas DataContext="{StaticResource ViewModel_dc_cursor}">
  <Canvas.Resources>
    <ViewModel_cursor x:Name="ViewModel_dc_cursor"/>
  </Canvas.Resources>
  <Image Source="..\icons\cursor_arrow.png" Width="16" Height="25"
              Canvas.Left="{Binding pos_x, Mode=TwoWay}" Canvas.Top="{Binding pos_y, Mode=TwoWay}"/>
</Canvas>
class ViewModel_cursor : public Canvas {
public:
    ViewModel_cursor();
    virtual void update(NsInt x, NsInt y) {
        pos_x = x;
        pos_y = y;
    };
private:
    NsInt pos_x, pos_y;
    NS_IMPLEMENT_INLINE_REFLECTION(ViewModel_cursor, Canvas) {
        NsMeta<TypeId>("ViewModel_cursor");
        NsProp("pos_x", &ViewModel_cursor::pos_x);
        NsProp("pos_y", &ViewModel_cursor::pos_y);
    }
};
extern "C" NS_DLL_EXPORT void NsRegisterReflection(Noesis::ComponentFactory* factory, NsBool registerComponents) {
    NS_REGISTER_COMPONENT(ViewModel_cursor);
} 
 
Ziriax
Posts: 60
Joined: 10 Dec 2015, 17:59
Location: Belgium
Contact:

Re: [C++] property update

14 Feb 2017, 14:42

I have no experience with the C++ version of Noesis, but I note the following:

Your ViewModel_cursor class derives from Canvas.

In the MVVM pattern, ViewModels should only contain state (data). The idea of a the view-model pattern is that you can use any other UI technology to render it. But Canvas is a UI element, so a "View".

Secondly, your update method is just poking the pos_x and pos_y data-members, so the view cannot possible know that these have changed. For that, you need to fire a property changed event.

To see how to implement such a view-model using Noesis technology, I recommend you read this documentation:

http://noesisengine.com/docs/Gui.Core.D ... orial.html
Peter Verswyvelen,
Strongly Typed Solutions
 
realesmedia
Topic Author
Posts: 85
Joined: 18 May 2016, 10:26

Re: [C++] property update

14 Feb 2017, 15:17

I've got error with macro NSS()
"__sSymbolpos_x" undeclaired identifier
class Cursor : public BaseComponent, public INotifyPropertyChanged {
public:
    Cursor();
    PropertyChangedEventHandler& PropertyChanged() {
        return _propertyChanged;
    }
    void set_x(NsInt x) {
        pos_x = x;
        _propertyChanged(this, NSS(pos_x));
    }
    NsInt get_x() {
        return pos_x;
    }
    void set_y(NsInt y) {
        pos_y = y;
        _propertyChanged(this, NSS(pos_y));
    }
    NsInt get_y() {
        return pos_y;
    }

private:
    NsInt pos_x, pos_y;
    PropertyChangedEventHandler  _propertyChanged;
    NS_IMPLEMENT_INLINE_REFLECTION(Cursor, BaseComponent) {
        NsMeta<TypeId>("Cursor");
        NsImpl<INotifyPropertyChanged>();
        NsProp("pos_x", &Cursor::get_x, &Cursor::set_x);
        NsProp("pos_y", &Cursor::get_y, &Cursor::set_y);
    }
};
 
realesmedia
Topic Author
Posts: 85
Joined: 18 May 2016, 10:26

Re: [C++] property update

15 Feb 2017, 09:57

Could anyone help me with error?
 
Ziriax
Posts: 60
Joined: 10 Dec 2015, 17:59
Location: Belgium
Contact:

Re: [C++] property update

15 Feb 2017, 10:31

Would it be possible to share your project? What version of Noesis are you using?
Peter Verswyvelen,
Strongly Typed Solutions
 
realesmedia
Topic Author
Posts: 85
Joined: 18 May 2016, 10:26

Re: [C++] property update

15 Feb 2017, 11:13

Would it be possible to share your project? What version of Noesis are you using?
Version 1.3.0b4
Open DX Integration sample from sdk and just paste code listed below (class Cursor).
 
Ziriax
Posts: 60
Joined: 10 Dec 2015, 17:59
Location: Belgium
Contact:

Re: [C++] property update

15 Feb 2017, 12:17

I had to change two things for your code to compile

(1) I had to declare the symbols in some CPP file
NS_DECLARE_SYMBOL(pos_x);
NS_DECLARE_SYMBOL(pos_y);
(2) The getters must be const
   
NsInt get_x() const {
      return pos_x;
}
NsInt get_y() const {
        return pos_y;
}
Peter Verswyvelen,
Strongly Typed Solutions
 
User avatar
sfernandez
Site Admin
Posts: 3013
Joined: 22 Dec 2011, 19:20

Re: [C++] property update

15 Feb 2017, 20:39

First of all thanks to Ziriax for your help here :)

Are you able to make it work with Ziriax suggestions?
 
realesmedia
Topic Author
Posts: 85
Joined: 18 May 2016, 10:26

Re: [C++] property update

17 Feb 2017, 14:20

Sorry for my slowpok mode enabled. Yes, thanks a lot for sharing solution. I'm able to compile code, and later I check solution for the first sample.
 
User avatar
jsantos
Site Admin
Posts: 3939
Joined: 20 Jan 2012, 17:18
Contact:

Re: [C++] property update

17 Feb 2017, 21:48

Cool! Thanks for reporting back.

Who is online

Users browsing this forum: No registered users and 2 guests