Share via


Part 3: Add the code

[ This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation ]

In this step, add code to take action when the user interacts with your app.

Add the code

The final step before testing your app is to add the code that implements the Go button.

To add the code

  1. In the designer, double-click the Go button control that you added. This creates an empty event handler for the button’s Click event. You will see the empty event handler in a page of C# code on the MainPage.xaml.cs tab, or in a page of Visual Basic code on the MainPage.xaml.vb tab.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    
    // The Blank Page item template is documented at https://go.microsoft.com/fwlink/p/?LinkID=234238
    
    namespace MiniBrowser
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            /// <summary>
            /// Invoked when this page is about to be displayed in a Frame.
            /// </summary>
            /// <param name="e">Event data that describes how this page was reached.  The Parameter
            /// property is typically used to configure the page.</param>
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
            }
    
            private void Go_Click(object sender, RoutedEventArgs e)
            {
    
            }
        }
    }
    
    Imports System.Runtime.InteropServices.WindowsRuntime
    
    ' The Blank Page item template is documented at https://go.microsoft.com/fwlink/p/?LinkID=234238
    
    ''' <summary>
    ''' An empty page that can be used on its own or navigated to within a Frame.
    ''' </summary>
    Public NotInheritable Class MainPage
        Inherits Page
    
        ''' <summary>
        ''' Invoked when this page is about to be displayed in a Frame.
        ''' </summary>
        ''' <param name="e">Event data that describes how this page was reached.  The Parameter
        ''' property is typically used to configure the page.</param>
        Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)
    
        End Sub
    
        Private Sub Go_Click(sender As Object, e As RoutedEventArgs) Handles Go.Click
    
        End Sub
    End Class
    

    When you double-click the Go button, Visual Studio also updates the XAML in MainPage.xaml to connect the button’s Click event to the Go_Click event handler.

    <Button x:Name="Go" Content="Go" ... Click="Go_Click"/>
    
  2. In MainPage.xaml.cs or MainPage.xaml.vb, add the two lines of code shown below inside the empty Go_Click event handler. This code takes the URL that the user enters in the text box and navigates to that URL in the WebView control named MiniBrowser.

            private void Go_Click(object sender, RoutedEventArgs e)
            {
                string site = URL.Text;
                MiniBrowser.Navigate(new Uri(site, UriKind.Absolute));
            }
    
        Private Sub Go_Click(sender As Object, e As RoutedEventArgs) Handles Go.Click
    
            Dim site As String = URL.Text
            MiniBrowser.Navigate(New Uri(site, UriKind.Absolute))
    
        End Sub
    

Summary

Congratulations, you're done with the third step of building your first Windows Phone Store app! You learned how to add code to take action when the user interacts with your app.

Next step

In the next step of this tutorial, you run your app on your development computer in Windows Phone emulator. Go to Part 4: Run the app.