How to go to another screen from one screen in Xamarin cross-platform?

13,628

Solution 1

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

Try this:

void OnbtnSecondXaml(object sender, EventArgs args)
  {
   // Write code here to move on second Xaml 

     Navigation.PushModalAsync(new SecondXaml());
  }

Solution 2

This is what NavigationPage is for. You need to wrap FirstXAML inside of a NavigationPage, then you can use the Navigation property to navigate to other pages.

Navigation.PushAsync(page2);

Also, you do not need to use FindByName to assign local variables for controls in your xaml. Any control with a x:name property will automatically be assigned a local variable.

Share:
13,628
Ashish Tiwari
Author by

Ashish Tiwari

I am cool casual person working as Android Application Developer.

Updated on June 07, 2022

Comments

  • Ashish Tiwari
    Ashish Tiwari about 2 years

    I am new in Xamarin and I want to go to another screen from one screen. I have a button in first screen and I want to open another screen after clicking on that button. How can I do this?

    Here is the code I have 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 of (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 
    
      }
    
    
    
    }
    }
    
  • Ashish Tiwari
    Ashish Tiwari over 9 years
    I am writhing this Navigation.PushAsync(page2); on FirstXML.cs but SecondXAML that is page2 is not accessible.
  • Jason
    Jason over 9 years
    You have to create a new instance of SecondXAML before you can push it onto the stack.
  • Ashish Tiwari
    Ashish Tiwari over 9 years
    Okay, Thank you Jason.