question

METHUSPHONPRASIT-6288 avatar image
1 Vote"
METHUSPHONPRASIT-6288 asked JessieZhang-2116 answered

How to get value on another page from firstpage binding context

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>

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered

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.


[1]: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center






5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.