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 ?
-
5Can you share what you've done so far? – mindOfAi Feb 28 '17 at 08:52
-
There are solutions but more important: You should not need or want this, – H H Mar 01 '17 at 10:16
2 Answers
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 myAge
variable 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 theCustomerID
, then look up theCustomer
after the navigation is complete.

- 43
- 7
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.