Hi all,
We are working on sample application with Xamarin.Forms MVVM Pattern Logic. In sample, we are struggling to show the validation alert when clicking on button (using command property).
Kindly suggest on how to show alert dialog/MessagingCenter dialog from View Model.
My Sample Code XAML :
`<?xml version="1.0" encoding="utf-8" ?>
<ContentPage.Resources>
</ContentPage.Resources>
<Grid.RowDefinitions>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- Row 0 -->
<Label Text="Value One :" Grid.Row="0" Grid.Column="0" Font="Large" XAlign="End" YAlign="Center" />
<Entry Grid.Row="0" Grid.Column="1" Keyboard="Numeric" Placeholder="Value One"
Text ="{Binding Value1, Mode=TwoWay, Converter={StaticResource stringConverter}, ConverterParameter=0.5}}" />
<!-- Row 1 -->
<Label Text="Value Two :" Grid.Row="1" Grid.Column="0" Font="Large" XAlign="End" YAlign="Center" />
<Entry Grid.Row="1" Grid.Column="1" Keyboard="Numeric" Placeholder="Value Two" Text="{Binding Value2,
Converter={StaticResource stringConverter}, ConverterParameter=0.5}}" />
<Grid Grid.Row="2" Grid.ColumnSpan="2">
<Grid.RowDefinitions> <RowDefinition Height="*" /> </Grid.RowDefinitions>
<Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions>
<Button Text="Sum" Command="{Binding AddCommand}" Grid.Row="0" Grid.Column="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
<Button Text="Subtract" Command="{Binding SubCommand}" Grid.Row="0" Grid.Column="1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
</Grid>
<!-- Row 4 -->
<Label Text="Total :" Grid.Row="3" Grid.Column="0" Font="Large" XAlign="End" YAlign="Center" />
<ContentView BackgroundColor="#40808080" Grid.Row="3" Grid.Column="1" Padding="10, 10, 40, 10">
<Label Text="{Binding Total, StringFormat='{0:00}'}" Font="Large" XAlign="End" />
</ContentView>
`
View Model :
` class TipCalcModel : INotifyPropertyChanged
{
double value1, value2, tipPercent, total;
public event PropertyChangedEventHandler PropertyChanged;
public ICommand AddCommand
{
get
{
return new Command(() =>
{
// We need to show the alert dialog.
//MessagingCenter.Subscribe<TipCalcPage, string>(this, "Hi", (sender, arg) =>
//{
//});
this.Total = Math.Round(this.Value1 + this.Value2, 2);
});
}
}
public ICommand SubCommand
{
get
{
return new Command(() =>
{
this.Total = Math.Round(this.Value1 - this.Value2, 2);
});
}
}
public double Value1
{
set
{
if (value1 != value)
{
value1 = value;
OnPropertyChanged("Value1");
}
}
get{return value1;}
}
public double Value2
{
set
{
if (value2 != value)
{
value2 = value;
OnPropertyChanged("Value2");
}
}
get {return value2;}
}
public double TipPercent
{
set
{
if (tipPercent != value)
{
tipPercent = value;
OnPropertyChanged("TipPercent");
}
}
get{return tipPercent;}
}
public double Total
{
set
{
if (total != value)
{
total = value;
OnPropertyChanged("Total");
}
}
get {return total; }
}
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}`