In this tutorial, you will learn how to navigate the user to another page when the user clicks the Start button. The second page will show the user's step counts.
Creating a Second Page
Take the following steps to create the second page:
In the Visual Studio Solution Explorer, right-click on your project(MySteps), click Add > New Item.
When the Add New Item window appears, select Xamarin.Forms from the left menu as highlighted in the image below, and select Content Page.
Type in the file name for StepCountPage in the Name edit box, and click Add.
In the StepCountPage.xaml file, change the font size and text of Label as follows:
In MySteps, we will use the NavigationPage class, which keeps a navigation history and provides a way for users to move back to the pages where they came from. Use the NavigationPage by assigning its instance, which itself initializes with a start-up page—an instance of MainPage. The finished code of App.xaml.cs is shown below:
public App()
{
InitializeComponent();
//MainPage = new MySteps.MainPage();
//MainPage = new MySteps.StepCountPage();
MainPage = new NavigationPage(new MainPage());
}
Next, in the MainPage.cs file, create a logic that moves from MainPage to StepCountPage when the the user clicks the Start button.
In the MainPage.xaml file, set the Clicked event to an event handler named Button_Clicked that is located in the code-behind file, MainPage.xaml.cs.
<StackLayout>
<Label Text="Welcome to My Steps!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"/>
<Button Text="Start"
Clicked="Button_Clicked" />
</StackLayout>
In the MainPage.xaml.cs, implement the Button_Clicked event handler method:
public partial class MainPage : ContentPage
{
...
// Invoked when the button is clicked
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new StepCountPage());
}
...
}
Now when a user taps the button, the Button_Clicked method executes. The sender argument represents a Button object responsible for the event. You can use this to access the source of the event, or to distinguish between multiple button objects sharing the same event handler.
In order to prevent blocking the UI thread, in the Button_Clicked method, you should use the async modifier and await operator to call the NavigationPage.PushAsync() method which asynchronously pushes a page onto the navigation stack.
Run MySteps. Don't be alarmed by the text being slightly pushed downwards. Focus that you can now navigate from MainPage to StepCountPage using the Start button. You can return to MainPage by pressing the Back button of the emulator.
When you build and run MySteps, you can see the text being slightly pushed downwards because there is some empty space occupied for the navigation bar. By using NavigationPage.SetHasNavigationBar(BindableObject, Boolean) method, you can hide it as follows:
public MainPage()
{
NavigationPage.SetHasNavigationBar(this, false);
InitializeComponent();
}
public StepCountPage()
{
NavigationPage.SetHasNavigationBar(this, false);
InitializeComponent();
}
Alternatively, you can set this in XAML by setting the property attribute NavigationPage.HasNavigationBar="false" as follows:
Run MySteps again. Now, you can see the text is in the center of the screen.
In the next tutorial, you will learn how to request the step count data from the Galaxy Watch's device sensor and display it on the screen.
Manage Your Cookies
We use cookies to improve your experience on our website and to show you relevant
advertising. Manage you settings for our cookies below.
Essential Cookies
These cookies are essential as they enable you to move around the website. This
category cannot be disabled.
Company
Domain
Samsung Electronics
.samsungdeveloperconference.com
Analytical/Performance Cookies
These cookies collect information about how you use our website. for example which
pages you visit most often. All information these cookies collect is used to improve
how the website works.
Company
Domain
LinkedIn
.linkedin.com
Meta (formerly Facebook)
.samsungdeveloperconference.com
Google Inc.
.samsungdeveloperconference.com
Functionality Cookies
These cookies allow our website to remember choices you make (such as your user name, language or the region your are in) and
tailor the website to provide enhanced features and content for you.
Company
Domain
LinkedIn
.ads.linkedin.com, .linkedin.com
Advertising Cookies
These cookies gather information about your browser habits. They remember that
you've visited our website and share this information with other organizations such
as advertisers.
Company
Domain
LinkedIn
.linkedin.com
Meta (formerly Facebook)
.samsungdeveloperconference.com
Google Inc.
.samsungdeveloperconference.com
Preferences Submitted
You have successfully updated your cookie preferences.