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>