How to get value on another page from firstpage binding context

METHUS PHONPRASIT 6 Reputation points
2021-05-26T12:34:28.08+00:00

Hello how to get value from BmiPage to HomePage here is my code.

BmiViewModel

public class BmiViewModel : INotifyPropertyChanged
{
public double height = 160;
public double weight = 60;

    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public double Height
    {
        get => height;
        set
        {
            height = value;
            RaisePropertyChanged(nameof(Bmi));
            RaisePropertyChanged(nameof(Classification));
        }
    }
    public double Weight
    {
        get => weight;
        set
        {
            weight = value;
            RaisePropertyChanged(nameof(Bmi));
            RaisePropertyChanged(nameof(Classification));
        }
    }


    public double Bmi

    => Math.Round(Weight / Math.Pow(Height /100, 2), 2);

     public string Classification
    {
        get
        {

            if (Bmi < 18.5)
                return "Underweight";
            else if (Bmi < 25)
                return "Normal";
            else if (Bmi < 30)
                return "Overweight";
            else return "Obese";
        }
    }
}

HomePage.xaml
<Label FontSize="22" Text="{Binding Bmi}" TextColor="Black" />

BmiPage.xaml
<StackLayout>
<Grid Margin="20,0,20,0" ColumnSpacing="2">
<Grid.RowDefinitions >
<RowDefinition Height="AUTO"/>
<RowDefinition Height="AUTO"/>
<RowDefinition Height="AUTO"/>
<RowDefinition Height="AUTO"/>
<RowDefinition Height="AUTO"/>
<RowDefinition Height="AUTO"/>
<RowDefinition Height="AUTO"/>
</Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />

                </Grid.ColumnDefinitions>


                <Label Text="BMI Calculator" Margin="20" TextColor="Black"  FontSize="30" HorizontalTextAlignment="Center" Grid.Row="0" Grid.ColumnSpan="2" />
                <Label Text="weight" Margin="20" TextColor="Black"  FontSize="30" HorizontalTextAlignment="Center" Grid.Row="1" Grid.ColumnSpan="2" />
                <Entry Placeholder="your weight" Text="{Binding Weight}" x:Name="txtWeigh"  Grid.Row="2" Grid.ColumnSpan="2" />
                <Label Text="hieght" Margin="20" TextColor="Black"  FontSize="30" HorizontalTextAlignment="Center" Grid.Row="3" Grid.ColumnSpan="2" />
                <Entry Placeholder="your height" Text="{Binding Height}" x:Name="txtHeigh" Grid.Row="4" Grid.ColumnSpan="2"/>
                <Label Text="Your bmi" FontSize="22"  TextColor="Black" Grid.Row="5" Grid.Column="0"/>
                <Label  FontSize="22"  Text="{Binding Bmi, Mode=TwoWay}" TextColor="Black" x:Name="txtSum"  Grid.Row="5" Grid.Column="1" />
                <Label  FontSize="22"  Text="Overweight or not" TextColor="Black"   Grid.Row="6" Grid.Column="0" />

                <Label  FontSize="22"  Text="{Binding Classification}" TextColor="Black"  Grid.Row="6" Grid.Column="1" />

            </Grid>
        </StackLayout>
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,706 Reputation points Microsoft Vendor
    2021-05-27T13:08:22.373+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    There are several methods to achieve this based on your requirement. I list two ways below:

    1.pass data by constructor of the next page. Please refer to the following code:

    BmiPage.xaml.cs

    public partial class BmiPage : ContentPage  
    {  
        BmiViewModel viewModel;  
    
        public BmiPage()  
        {  
            InitializeComponent();  
    
            viewModel = new BmiViewModel();  
            BindingContext = viewModel;  
        }  
    
        private async void Button_Clicked(object sender, EventArgs e)  
        {  
            await Navigation.PushModalAsync(new HomePage(viewModel.Bmi));  
        }  
    
    }  
    

    HomePage.xaml.cs

    public partial class HomePage : ContentPage  
    {  
        public double Bmi { get; set; }  
        public HomePage(double bmi )  
        {  
            InitializeComponent();  
    
            Bmi = bmi;  
    
            BindingContext = this;  
        }  
    }  
    

    2.create a new Viewmodel for HomePage and set BindingContext for it.

    create Viewmodel HomeViewModel.cs for HomePage:

    public class HomeViewModel: INotifyPropertyChanged  
    {  
        double bmi;  
    
        public double Bmi  
        {  
            set { SetProperty(ref bmi, value); }  
    
            get { return bmi; }  
        }  
    
        public HomeViewModel(double bmi) {  
    
            Bmi = bmi;  
        }  
    
        bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)  
        {  
            if (Object.Equals(storage, value))  
                return false;  
    
            storage = value;  
            OnPropertyChanged(propertyName);  
            return true;  
        }  
    
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)  
        {  
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
        }  
    
        public event PropertyChangedEventHandler PropertyChanged;  
    }  
    

    BmiPage.xaml.cs

    public partial class BmiPage : ContentPage  
    {  
        BmiViewModel viewModel;  
    
        public BmiPage()  
        {  
            InitializeComponent();  
    
            viewModel = new BmiViewModel();  
            BindingContext = viewModel;  
        }  
    
        private async void Button_Clicked(object sender, EventArgs e)  
        {  
            //await Navigation.PushModalAsync(new HomePage(viewModel.Bmi));  
            await Navigation.PushModalAsync(new HomePage {  
                BindingContext = new HomeViewModel(viewModel.Bmi)  
            });  
        }  
    }  
    

    Best Regards,

    Jessie Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments