How to avoid Null Values using Mvvm in Uwp Projecct?

Nov 2 2016 2:12 AM
Model: Coffee.cs
 
public class Coffee:BindableBase
{
private int coffeeId;
private string coffeeName;
public int CoffeeId
{
get
{
return coffeeId;
}
set
{
coffeeId = value;
RaisePropertyChanged("CoffeeId");
}
}
public string CoffeeName
{
get
{
return coffeeName;
}
set
{
coffeeName = value;
RaisePropertyChanged("CoffeeName");
}
}
}
 
 
View: CoffeeAdd.xaml
 
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Coffee Id"/>
<TextBox Width="120" Height="30" Margin="50 0 0 0" Text="{Binding CoffeeId}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0 20 0 0">
<TextBlock Text="Coffee Name"/>
<TextBox Width="120" Height="30" Margin="20 0 0 0" Text="{Binding CoffeeName}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0 20 0 0">
<Button Content="Add" Width="120" Height="30" Command="{Binding AddCommand}"/>
<Button Content="View" Width="120" Height="30" Margin="20 0 0 0"/>
</StackPanel>
</StackPanel>
 
View Model:CoffeeAddViewModel
 
public class CoffeeAddViewModel:BindableBase
{
private ICoffeeDataService coffeedataservice;
public CoffeeAddViewModel(ICoffeeDataService dataservice)
{
coffeedataservice = dataservice;
LoadCommand();
}
private int _coffeeId;
private string _coffeeName;
public int CoffeeId
{
get
{
return _coffeeId;
}
set
{
_coffeeId = value;
RaisePropertyChanged("CoffeeId");
}
}
public string CoffeeName
{
get
{
return _coffeeName;
}
set
{
_coffeeName = value;
RaisePropertyChanged("CoffeeName");
}
}
public ICommand AddCommand { get; set; }
private void LoadCommand()
{
AddCommand = new CustomCommand(add, canadd);
}
private async void add(object obj)
{
coffeedataservice.AddCoffee(new Model.Coffee { CoffeeId = _coffeeId, CoffeeName = _coffeeName });
var dialog = new MessageDialog("Successfully Added");
await dialog.ShowAsync();
}
private bool canadd(object obj)
{
return true;
}
}
 
 I want to add new coffee details,when i add using Addcommand (custom command) but when add new details is null, so how i avoid this issue?
 
 
 
 
 

Answers (1)