While using User Controls in Windows Phone, we often get into a situation where we need to pass some kind of data from the user control to the parent page, when any operation is performed on the user control. Delegates can be used to pass data from the User Control to the Parent page. We reference C# Corner’s article to apply the same logic in Windows Phone 8.1
Step 1: Create a User Control
Firstly, we create an User Control with a button in it.
Step 2: Declare a delegate on the User Control
Create a delegate on the user control, that can encapsulate a method that takes no input and returns a DateTime type.
Step 3: Create an event on the User Control
Declare an event on the user control, that is of the delegate type that we created.
Step 4: Using User Control in the Parent Page
Next, we use the User Control in our MainPage.xaml.
Step 5: Subscribe to the User Control event in the Parent Page
Our main page will subscribe to the event that we created on the user control, to get notification of when the event is fired on the user control.
Step 6: Execute the Event Explicitly in the User Control
Next, when the button click functionality is done, we explicitly execute the event by calling the GetDataFromChild
event, passing the required data.
Run the application and click the button. On the clicking of the button, when we make the explicit call to the event from the user control, the parent page receives the notification through the UChild_GetDataFromChild
method.
Recent Comments