` System.InvalidCastException: '指定的转换无效.' [英] ` System.InvalidCastException: 'Specified cast is not valid.'

查看:31
本文介绍了` System.InvalidCastException: '指定的转换无效.'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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",

                },

            };

我有这个异常 ` System.InvalidCastException: '指定的转换无效.'对于行:

var item = (ProductViewModel)btn.BindingContext;

我希望 MainViewModel() 中的每个新 ProductViewModel() 都有一个按钮让我转到页面,例如名称为 QR_Manager 的新 ProductViewModel() 我想转到 ManagerPage 和名称为 Yellow Sun 我的新 ProductViewModel()想去页面 YellowSunPage.

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.

推荐答案

错误是你绑定在Button上的BindingContext类型不对应,也就是说绑定路径不正确正确.

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 .

解决问题

  1. 将按钮放在 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>

  • 在视图模型中将 ItemsSourceProducts 绑定.

    <ListView ItemsSource="{Binding Products}" >
    

  • page.xaml.cs中将viewmodel设置为BindingContext.

  • Set the viewmodel as BindingContext in page.xaml.cs.

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

  • 这篇关于` System.InvalidCastException: '指定的转换无效.'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆