User control events handling in page Windows 8.1
- Create a Windows Store blank app.
- Add a user control in app,
- Add a button in user control within grid.
- <Grid>
- <Button Content="click me" Click="Button_Click" />
- </Grid>
- In code behind of user control create a event handler.
- public event RoutedEventHandler ButtonClicked;
- Add user control to your main page,
- <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
- <local:MyUserControl1 HorizontalAlignment="Center" VerticalAlignment="Center" />
- </Grid>
- Rebuild you solution and add new event for button clicked event that was created in user control.
- <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
- <local:MyUserControl1 HorizontalAlignment="Center" VerticalAlignment="Center" ButtonClicked="MyUserControl1_ButtonClicked" />
- </Grid>
- Adding message dialog to verify button click event in main page.
- public MainPage()
- {
- this.InitializeComponent();
- }
-
- private async void MyUserControl1_ButtonClicked(object sender, RoutedEventArgs e)
- {
- MessageDialog md = new MessageDialog("Button clicked");
- await md.ShowAsync();
- }
- Run application and check that a message is shown.