【问题标题】:Why am I getting an invalid cast exception ('Specified cast is not valid.')?为什么我得到一个无效的转换异常(\'Specified cast is not valid.\')?
【发布时间】:2022-09-27 11:17:15
【问题描述】:

选中复选框时,我试图从列表中删除一个项目。我正在使用 input.kit 作为复选框。我已经能够使用图像按钮将它们删除,但不能使用此复选框。这是 xaml:

<ListView ItemsSource=\"{Binding TodoListItems}\" x:Name=\"todoList\">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <FlexLayout JustifyContent=\"SpaceBetween\" Padding=\"20,0\">
                            <ContentView>
                                <FlexLayout AlignItems=\"Center\" >
                                    <input:CheckBox IsChecked=\"{Binding Complete}\" 
                                                    CheckChangedCommand=\"{Binding Path=BindingContext.CompleteTodoCommand, Source={x:Reference todoList}}\"
                                                    CommandParameter=\"{Binding .}\"
                                                    />
                                    <Label Text=\"{Binding TodoText}\" Padding=\"10,0,0,0\" FontSize=\"Large\"/>
                                </FlexLayout>
                            </ContentView>
                            
                            <ImageButton Source=\"trash_icon.png\" 
                                         Command=\"{Binding Path=BindingContext.RemoveTodoCommand, Source={x:Reference todoList}}\"
                                         CommandParameter=\"{Binding .}\"
                                         Scale=\"1.2\" BackgroundColor=\"White\"
                                         />
                            
                        </FlexLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
            
        </ListView>

这是 ViewModel:

    {
        private ObservableCollection<TodoItem> todoListItems;

        
        public ObservableCollection<TodoItem> TodoListItems
        {
            get { return todoListItems; }
            set { todoListItems = value; }
        }

        private ObservableCollection<TodoItem> completedTodoItems;

        public ObservableCollection<TodoItem> CompletedTodoItems
        {
            get { return completedTodoItems; }
            set { completedTodoItems = value; }
        }



        public TodoListViewModel()
        {
            todoListItems = new ObservableCollection<TodoItem>();
            TodoListItems.Add(new TodoItem(\"Walk the duggo\",false));
            TodoListItems.Add(new TodoItem(\"Do the washing\",false));
            TodoListItems.Add(new TodoItem(\"Brush off Cheeto dust\",false));

            CompletedTodoItems = new ObservableCollection<TodoItem>();
        }
        public ICommand AddTodoCommand => new Command(AddTodoItem);
        public string NewTodoInputValue { get; set; }
        void AddTodoItem() 
        {
            TodoListItems.Add(new TodoItem(NewTodoInputValue));
        }

        public ICommand RemoveTodoCommand => new Command(RemoveTodoItem);
        
        void RemoveTodoItem(object o)
        {
            TodoItem todoItemBeingRemoved = o as TodoItem;
            TodoListItems.Remove(todoItemBeingRemoved);
        }

        public ICommand CompleteTodoCommand => new Command(CompleteTodo);

        void CompleteTodo(object o) 
        {
            
            TodoItem todoItemCompleted = o as TodoItem;
            todoListItems.Remove(todoItemCompleted);

            CompletedTodoItems.Add(todoItemCompleted);
        }

    }

全视图模型。 我已经检查了 observable 集合是否已经初始化等等。此外,AddToDo 和 RemoveTodo 命令也有效。

0x1C in TodoAppXamarin.ViewModels.TodoListViewModel.CompleteTodo at C:\\Users\\johns\\source\\repos\\TodoAppXamarin\\TodoAppXamarin\\TodoAppXamarin\\ViewModels\\TodoListViewModel.cs:64,4
at C:\\Users\\johns\\source\\repos\\TodoAppXamarin\\TodoAppXamarin\\TodoAppXamarin\\ViewModels\\TodoListViewModel.cs(64)

那堆栈跟踪?

  • 你能包括整个 ViewModel 吗? InvalidCasException 表示类型彼此不兼容,但是根据您提供的内容,我们无法知道 CompletedTodoItems 是什么。
  • CompletedTodoItems 的类型是什么?
  • 现在就在那里。 CompletedTodoItems 的类型是 ObservableCollection<TodoItem>
  • 在不告诉我们异常提供的堆栈跟踪的情况下,您给我们留下了一个谜语来推测您的代码中的哪一行可能会引发此异常。不是我的那种演出,我不得不承认......
  • 我们不知道“第 64 行”是什么

标签: c# xamarin.forms mvvm


【解决方案1】:

CompletedTodoItems.Add 不接受类型 TodoItem。尝试隐式转换为正确的类型,但 TodoItem 不兼容。

【讨论】:

    【解决方案2】:

    根据您的代码,我创建了一个演示,但我无法重现此问题。

    由于看不到 TodoItem 的代码,所以我创建了一个类 TodoItem.cs 如下:

    public class TodoItem 
        {
            public string TodoText { get; set; }
    
            public bool Complete { get; set; }
            public TodoItem(string text,bool ischecked) { 
              this.TodoText = text;    
              this.Complete = ischecked;
            }
        }
    

    此外,因为TodoItem 类没有单个参数构造函数,所以我替换了您的代码:

        void AddTodoItem() 
        {
            TodoListItems.Add(new TodoItem(NewTodoInputValue));
        }
    

    void AddTodoItem()
        {   // add a value for the second parameter 
            TodoListItems.Add(new TodoItem(NewTodoInputValue,false));
        }
    

    TodoListViewModel.cs 的整个代码是:

       public class TodoListViewModel 
    {
        private ObservableCollection<TodoItem> todoListItems;
    
    
        public ObservableCollection<TodoItem> TodoListItems
        {
            get { return todoListItems; }
            set { todoListItems = value; }
        }
    
        private ObservableCollection<TodoItem> completedTodoItems;
    
        public ObservableCollection<TodoItem> CompletedTodoItems
        {
            get { return completedTodoItems; }
            set { completedTodoItems = value; }
        }
    
    
    
        public TodoListViewModel()
        {
            todoListItems = new ObservableCollection<TodoItem>();
            TodoListItems.Add(new TodoItem("Walk the duggo", false));
            TodoListItems.Add(new TodoItem("Do the washing", false));
            TodoListItems.Add(new TodoItem("Brush off Cheeto dust", false));
    
            CompletedTodoItems = new ObservableCollection<TodoItem>();
        }
        public ICommand AddTodoCommand => new Command(AddTodoItem);
        public string NewTodoInputValue { get; set; }
        void AddTodoItem()
        { // add a value for the second parameter 
            TodoListItems.Add(new TodoItem(NewTodoInputValue,false));
        }
    
        public ICommand RemoveTodoCommand => new Command(RemoveTodoItem);
    
        void RemoveTodoItem(object o)
        {
            TodoItem todoItemBeingRemoved = o as TodoItem;
            TodoListItems.Remove(todoItemBeingRemoved);
        }
    
        public ICommand CompleteTodoCommand => new Command(CompleteTodo);
    
        void CompleteTodo(object o)
        {
    
            TodoItem todoItemCompleted = o as TodoItem;
            todoListItems.Remove(todoItemCompleted);
    
            CompletedTodoItems.Add(todoItemCompleted);
        }
    }
    
    
    
    
     
    

    【讨论】:

      猜你喜欢
      • 2012-09-15
      • 2011-04-26
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 2023-02-15
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      相关资源
      最近更新 更多