[C++] Call function from another class

Eric Tian 211 Reputation points
2020-11-21T23:45:13.883+00:00

Hello,

I'm trying to create a program with XAML and C++. There is a settings frame, that contains a settings page on MainPage.xaml. And a "Back" button that collapses the frame upon being clicked (the function that handles click is on SettingsPage.xaml.cpp). However, my code (look below) does not call the function to collapse the frame (there are no errors, however, when testing, it does not work). Essentially, I'm trying to call the CollapseFrame method from the Button_Click method.

SettingsPage.xaml.cpp:

void SettingsPage::Button_Click(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e)
{
    MainPage mainPage;
    mainPage.CollapseFrame();
}

MainPage.xaml.cpp:

void MainPage::CollapseFrame()
{
    SettingsHolder->Visibility = ::Visibility::Collapsed;
}
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,543 questions
0 comments No comments
{count} votes

Accepted answer
  1. Eric Tian 211 Reputation points
    2020-11-25T05:15:29.027+00:00

    Thank you, @Yan Gu - MSFT . This is what you should do:
    SettingsPage.xaml.h:

    public:  
          property MainPage ^ ParentMainPage;  
    protected:  
           virtual void OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override;  
    

    SettingsPage.xaml.cpp:

     void AccessViolation::SettingsPage::OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e)  
     {  
            MainPage^ mainPage = (MainPage^)e->Parameter;  
            if (mainPage != nullptr)  
            {  
                   ParentMainPage = mainPage;  
            }  
     }  
      
     void SettingsPage::Button_Click(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e)  
     {  
          ParentMainPage->CollapseFrame();  
     }  
    
    
    
      
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 112.5K Reputation points
    2020-11-22T08:02:04.963+00:00

    If MainPage has a member or local variable that points to SettingsPage, then you can initialise a property or field of SettingsPage. Something like this:

    // in MainPage:
    SettingsPage ^ s = . . .
    s->ParentMainPage = this;
    

    where ParentMainPage is a new member of SettingsPage, of ‘MainPage ^’ type. It will point to MainPage.

    Then you can do this:

    void SettingsPage::Button_Click( . . . )
    {
         ParentMainPage->CollapseFrame();
    }
    

    Show some details about the usage of SettingsPage in MainPage.

    Or maybe you can use events: the MainPage will add a handler for Button_Click or for some custom event of SettingsPage. The code that collapses the frame will be written inside this handler.

    1 person found this answer helpful.