0

In my UWP application i have three xaml pages.In first xaml page i have a button click event. I want to access this button click event from the third page. Is it possible ?

Visnu
  • 87
  • 8

2 Answers2

0

Generally, you can pass parameters between pages like that:

Frame.Navigate(typeof(TargetPage), 19);

On the TargetPage override the OnNavigatedTo method like this:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (e.Parameter != null)
    {
        var myAge = e.Parameter + 3;
    }
}

In this case the value of myAgevariable should be 19 + 3 = 22; However, you have to check if e.Parameter is not null when your navigation model provides navigation from multiple pages (which do not pass any parameters) to the current one.

Also, best practices from documentation:

The parameter value can have a complex type if you do not use GetNavigationState(). However, you should still use only basic types in order to avoid excess memory usage caused by the frame’s navigation stack holding a reference to the parameter. A preferred approach is to not pass the actual object, but instead pass an identifier that you can use to look up the object in the target landing page. For example, instead of passing a Customer object, pass a reference to the CustomerID, then look up the Customer after the navigation is complete.

0

If you're familiar with MVVM pattern. You could call methods from other ViewModels.

For example like the following:

public class FirstViewModel:ViewModelBase
{
    public RelayCommand buttonClickCmd { get; set; }

    public FirstViewModel()
    {
        buttonClickCmd = new RelayCommand(Click);
    }

    public void Click()
    {
        Debug.WriteLine("Clicked");
    }
}

public class ThirdViewModel:ViewModelBase
{
    public RelayCommand ClickFromFirstCmd { get; set; }

    public ThirdViewModel()
    {
        ClickFromFirstCmd = new RelayCommand(Click);
    }

    public void Click()
    {
        FirstViewModel firstviewmodel = new FirstViewModel();
        firstviewmodel.Click();
    }
}

You could check How to call functions in a main view model from other view models? for more details.

Community
  • 1
  • 1
Timo
  • 106
  • 8