Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
172 views
Welcome To Ask or Share your Answers For Others

1 Answer

The error is caused that the type of BindingContext you bind on Button is not corresponding , in other word, the binding path is not correct .

To solve the problem

  1. Place button inside ListView .

    <ListView >
         <ListView.ItemTemplate>
             <DataTemplate>
                 <ViewCell>
                     <Button x:Name="AddToCartButton" 
                             BackgroundColor="#FB9B63" CornerRadius="10" HeightRequest="60" FontSize="20" HorizontalOptions="Center"
                             Text="Entrer" TextColor="White" VerticalOptions="Center" Clicked="AddToCartButton_Clicked" />
                 </ViewCell>
             </DataTemplate>
         </ListView.ItemTemplate>
    </ListView>
    
  2. Bind ItemsSource with Products in viewmodel .

    <ListView ItemsSource="{Binding Products}" >
    
  3. Set the viewmodel as BindingContext in page.xaml.cs.

    public NavigatePage()
    {
    
       InitializeComponent();
    
       MainViewModel model = new MainViewModel();
       this.BindingContext = model;
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...