NavigatePage.xaml :
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>
<Button x:Name="AddToCartButton" BackgroundColor="#FB9B63" CornerRadius="10" HeightRequest="60" FontSize="20" HorizontalOptions="Center"
Text="Entrer" TextColor="White" VerticalOptions="Center" Clicked="AddToCartButton_Clicked" />
</Grid>
NavigatePage.xaml.cs :
private void AddToCartButton_Clicked(object sender, EventArgs e)
{
var btn = (Button)sender;
var item = (ProductViewModel)btn.BindingContext;
ContentPage page=null;
switch (item.Name)
{
case "QR_Manager":
page = new Views.Report();
break;
}
Navigation.PushAsync(page);
MainViewModel.cs :
namespace UpManager.Dashboard.ViewModels
{
public class MainViewModel : BaseViewModel
{
public IList<ProductViewModel> Products { get; set; }
private ProductViewModel _selectedProduct;
public ProductViewModel SelectedProduct
{
get { return _selectedProduct; }
set { SetProperty(ref _selectedProduct, value); }
}
public ShoppingCartViewModel ShoppingCart { get; set; }
public ICommand RemoveItemCommand { private set; get; }
public MainViewModel()
{
Products = new ObservableRangeCollection<ProductViewModel>()
{
new ProductViewModel()
{
HeroColor = "#95C9F7",
Name="QR_Manager",
ImageUrl = "QRM",
IsFeatured = true,
Description = "Contained in a glass polygonal florarium",
},
new ProductViewModel()
{
HeroColor = "#FFCA81",
Name="Yellow Sun",
ImageUrl = "yellow_moss",
IsFeatured = true,
Description = "Contained in a yellow glass polygonal florarium",
},
new ProductViewModel()
{
HeroColor = "#A2BAD3",
Name="Grey Blue",
ImageUrl = "grey_moss",
IsFeatured = true,
Description = "Contained in a glass polygonal florarium",
},
new ProductViewModel()
{
HeroColor = "#F796DD",
Name="Pink",
ImageUrl = "pink_moss",
IsFeatured = false,
Description = "Contained in a glass polygonal florarium",
},
new ProductViewModel()
{
HeroColor = "#95C9F7",
Name="Sky Blue",
ImageUrl = "blue_moss",
IsFeatured = false,
Description = "Contained in a glass polygonal florarium",
},
new ProductViewModel()
{
HeroColor = "#D69EFC",
Name="Lavender",
ImageUrl = "lavender_moss",
IsFeatured = false,
Description = "Contained in a glass polygonal florarium",
},
new ProductViewModel()
{
HeroColor = "#74D69E",
Name="Green Life",
ImageUrl = "green_moss",
IsFeatured = true,
Description = "Contained in a glass polygonal florarium",
},
new ProductViewModel()
{
HeroColor = "#FB8183",
Name="Red",
ImageUrl = "red_moss",
IsFeatured = false,
Description = "Contained in a glass polygonal florarium",
},
new ProductViewModel()
{
HeroColor = "#FB9B64",
Name="Orange",
ImageUrl = "orange_moss",
IsFeatured = false,
Description = "Contained in a glass polygonal florarium",
},
new ProductViewModel()
{
HeroColor = "#D69EFC",
Name="Lavender",
ImageUrl = "lavender_moss",
IsFeatured = false,
Description = "Contained in a glass polygonal florarium",
},
};
i have this exception ` System.InvalidCastException: 'Specified cast is not valid.' for the line :
var item = (ProductViewModel)btn.BindingContext;
i want for every new ProductViewModel() in MainViewModel() a button make me go to page for example the new ProductViewModel() whose name is QR_Manager i want go to ManagerPage and for new ProductViewModel() whose name is Yellow Sun i want go to the page YellowSunPage.
See Question&Answers more detail:
os