C# WPF InvalidCastException in RelayCommand<T> [closed]

你说的曾经没有我的故事 提交于 2019-12-13 10:40:06

问题


I would like to have a method like ShowImageCommand = new RelayCommand<Image, Audio>(ShowImageMethod);, but it's not possible, so I have created

public class AudioAndImage 
   { 
     public Image Picture { get; set; } 
     public Audio Sound { get; set; } 
   }

I have a new object of this type in ViewModel and set properties when Audiofile and Image are selected. But in the method which opens a new window

public void AnnotateImageMethod(AudioAndImage obj)
   {
     AnnotateImageWindow win = new AnnotateImageWindow(obj.Picture.Name, obj.Sound.AudioName);
   }

I have thrown an exception System.InvalidCastException: 'Unable to cast object of type 'x.Model.Image' to type 'x.ViewModel.AudioAndImage'.', I double-checked all code and probably is about some converter or something about binding because I haven't change anything in XAML.

My logic is the following: I already have methods to handle opening audiofile and image selection. So I can create a new object and set his properties at those methods. Please, explain to me where I'm wrong?

Questions realated to the same app: ONE,TWO

来源:https://stackoverflow.com/questions/59172519/c-sharp-wpf-invalidcastexception-in-relaycommandt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!