locked
TextBlock, set text from a user defined function

    Question

  • Hi everybody,

    I am coding in C++ and with SQLite3 and I'm trying to use the SQLite3_exec() function to invoke an SQL Statement which searches my DB. 

    Well I defined my callback function like this:

    int callback(void *NotUsed, int argc, char **argv, char **azColName){
    
    greetingOutput->Text = "Some Text";
    
    	};

    I get the error that "greetingOutput" is not defined. In other functions, like a Button_click callback, I can execute that with no errors. My callback function is gonna be executed from this part:

    rc = sqlite3_exec(db, s_SQLstatement.c_str(), callback, 0, 0);

    What I already have tried is to define my callback function in the header file, which at first worked fine(the greetingOutput is recognized) but then I get an error from sqlite3_exec(): "The argument of the type ""int (HelloWorld::MainPage::*)(void *NotUsed, int argc, char **argv, char **azColName)"" is not compatible with ""int (*)(void *, int, char **, char **)""

    I have translated the error messages from german, I am sorry for any mistakes.

    Has someone a solution for my problem? I thank you in advance...

    Sunday, November 03, 2013 1:05 PM

All replies

  • You cannot pass a class method as the call-back. The error that the types don't match is correct: the class method takes the this pointer as a hidden first argument, which the caller doesn't expect.

    However, greetingOutput is a member variable and requires the class object.

    You'll need to find the page's object from the static callback.

    If you have a singleton page you can expose a static Current function which returns that single item. The static call back can call MainPage::Current to get the object with the greetingText, or the call back can call a method on the Current object to do the actual work. A generally better way would be to set the greeting in a data model object rather than directly on the element and then bind the element to the data model.

    --Rob

    Sunday, November 03, 2013 2:46 PM
    Owner
  •  A generally better way would be to set the greeting in a data model object rather than directly on the element and then bind the element to the data model.

    Hi, thank you for your answer but can you please be more specific.

    I get the reason why it does not work but I do not know how I get the page's object from the static callback. I tried to call that MainPage::Current what u mentioned but "Current" does not seem to be a Member of "Mainpage". 

    And what do you mean with the part I quoted?

    ...a code snippet of a solution would be nice.

    --Peter

    Sunday, November 03, 2013 5:10 PM
  • can nobody help?  I am still having the same prob... :-(
    Monday, November 04, 2013 6:58 PM
  • You would need to define the Current function or field yourself. If you look at the C++ Xaml samples they demonstrate this as a field:

        internal: 
            static MainPage^ Current; 
    and set it in the class' constructor:
        MainPage::Current = this; 

    See Data binding for information on data binding and the wizard generated models (other than blank) for examples. In general you will be much better off separating your data (the model) from the UI (the view). Put the data in a object or objects with properties exposing the information. The controls then bind to the data that they should display or edit. This way the code doesn't need a dependency on specific UI controls and it can be updated much more easily and flexibly.

    Take a look at the XAML data binding sample  for help with both of these.

    --Rob


    Tuesday, November 05, 2013 4:44 AM
    Owner
  • Hey, thank you for your answers. I am making some progress. I understand the XAML data binding sample and I am trying to play with it but I am getting a few error messages that I do not understand. I am working on the problem for a week now.

    I added the Employee.h class in my Project, added the binding in the xaml file and such... every thing ok but:

    Although I added the Employee class completely unchanged to my project I get a few error on the part of: 

    void OnPropertyChanged(String^ propertyName)
    {
          if (_isPropertyChangedObserved)
          {
          PropertyChanged(this, ref new PropertyChangedEventArgs(propertyName));
          }
    }

    error C2061: Syntaxerror: identifier 'String' (App.xaml.cpp)
    error C2660: 'HelloWorld::Employee::OnPropertyChanged': Function does not accept 1 argument

    for "OnPropertyChanged(String^ propertyName)" and for "PropertyChanged()" the following:


    error C2061: Syntaxerror: identifier 'PropertyChangedEventArgs' 
    error C2660: 'Windows::UI::Xaml::Data::PropertyChangedEventHandler::NonVInvoke': Function does not accept 1 argument

    I really do not understand the last error. Well there are much more errors but I think they are basically the same. The strange thing is that the binding sample compiles with no errors.

    Can so please help me, I struggle with this for a week now... 

    I already coded with c++ and a few UI's and also other languages, but it is incedible how hard it is to code for Metro... really exhausting.

    But thank u in advance..

    Sunday, November 10, 2013 5:35 PM
  • Ah and maybe this in addition to the errors above:
                virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged
                {
                    virtual Windows::Foundation::EventRegistrationToken add(Windows::UI::Xaml::Data::PropertyChangedEventHandler^ e)
                    {
                        _isPropertyChangedObserved = true;
                        return _privatePropertyChanged += e;
                    }
                    virtual void remove(Windows::Foundation::EventRegistrationToken t)
                    {
                        _privatePropertyChanged -= t;
                    }
    
            protected:
                    virtual void raise(Object^ sender, Windows::UI::Xaml::Data::PropertyChangedEventArgs^ e)
                    {
                        if (_isPropertyChangedObserved)
                        {
                            _privatePropertyChanged(sender, e);
                        }
                    }
                }

    Monday, November 11, 2013 10:46 AM