How to switch to another screen from one screen in the Xamarin cross platform?

I am new to Xamarin and I want to switch to another screen from one screen. I have a button on the first screen, and I want to open another screen after clicking on this button. How can i do this?

Here is the code I've tried so far:

XAML Layout (FirstXAML.xaml)

<?xml version="1.0" encoding="UTF-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="AllControlsDemo.FirstXaml"> <StackLayout> <Slider x:Name="sldr" VerticalOptions="CenterAndExpand" ValueChanged="OnSliderValueChanged" /> <Label x:Name="lblValue" Text="A simple Label" Font="Large" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" /> <Button x:Name="btnClickme" Text="Click Me!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Clicked="OnbtnClickme" /> <Button x:Name="btnSecondXaml" Text="Second Xaml!" HorizontalOptions="Center" VerticalOptions="StartAndExpand" Clicked="OnbtnSecondXaml" /> </StackLayout> </ContentPage> 

Code (FirstXAML.xaml.cs)

 using System; using System.Collections.Generic; using Xamarin.Forms; namespace AllControlsDemo { public partial class FirstXaml : ContentPage { private Label valueLabel; float count = 0.050f; private Slider slider; public FirstXaml () { InitializeComponent (); valueLabel = this.FindByName<Label>("lblValue"); slider = this.FindByName<Slider> ("sldr"); } void OnSliderValueChanged(object sender, ValueChangedEventArgs args) { valueLabel.Text = ((Slider)sender).Value.ToString("F3"); count = float.Parse(valueLabel.Text); } void OnbtnClickme(object sender, EventArgs args) { count += 0.050f; slider.Value = count; } void OnbtnSecondXaml(object sender, EventArgs args) { // Write code here to move on second Xaml } } } 
+5
source share
2 answers

I am also new to Xamarin. I copied your code and solved your problem.

Try the following:

 void OnbtnSecondXaml(object sender, EventArgs args) { // Write code here to move on second Xaml Navigation.PushModalAsync(new SecondXaml()); } 
+9
source

This is what for NavigationPage . You need to migrate FirstXAML inside the NavigationPage, then you can use the Navigation property to go to other pages.

 Navigation.PushAsync(page2); 

In addition, you do not need to use FindByName to assign local variables to controls in your xaml. Any control with an x:name attribute is automatically assigned a local variable.

+4
source

Source: https://habr.com/ru/post/1207881/


All Articles