在我的WPF MVVM项目中,我得到这个错误:InvalidCastException的:无法投型“Mocks.Class _...”的对象为“类”
InvalidCastException: Unable to cast object of type 'Mocks.DDD_AutoRadio_General_Audio_AudioFile_130_13063066' to type 'DDD.AutoRadio.General.Audio.AudioFile'.
的WPF主窗口有一个自定义UserControl
。
简化类AudioFile
:
namespace DDD.AutoRadio.General.Audio
{
[DataContract]
public class AudioFile : INotifyPropertyChanged
{
private string name;
/// <summary>
/// Name of the Mp3
/// </summary>
[DataMember]
public string FilePath
{
get { return name; }
set
{
var oldvalue = name;
if (oldvalue != value)
{
name = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("FilePath"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
但我不认为这是有关这一类。
我认为这更多的是自定义UserControl
:
public partial class EditField : UserControl
{
public static DependencyProperty selectedList =
DependencyProperty.Register("DataGridSelectedItems", typeof(OC<AudioFile>), typeof(EditField));
public OC<AudioFile> DataGridSelectedItems
{
get { return (OC<AudioFile>)GetValue(selectedList); }
set { SetValue(selectedList, value); }
}
public EditField()
{
InitializeComponent();
}
private void Collection_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
OC<AudioFile> l = new OC<AudioFile>();
foreach (AudioFile i in Collection.SelectedItems) //'Collection' is a DataGrid
l.Add(i);
DataGridSelectedItems = l;
}
}
注:OC是一款基于ObservableColletion<>
类
public class OC<T> : ObservableCollection<T> where T: INotifyPropertyChanged
这里一类是堆栈跟踪
at DDD.AutoRadio.Database.Editor.View.EditField.Collection_SelectionChanged(Object sender, SelectionChangedEventArgs e)
at System.Windows.Controls.SelectionChangedEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Controls.DataGrid.OnSelectionChanged(SelectionChangedEventArgs e)
at System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List`1 unselectedInfos, List`1 selectedInfos)
at System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
at System.Windows.Controls.Primitives.Selector.SelectionChanger.SelectJustThisItem(ItemInfo info, Boolean assumeInItemsCollection)
at System.Windows.Controls.Primitives.Selector.SetSelectedToCurrent()
at System.Windows.Controls.Primitives.Selector.SetSynchronizationWithCurrentItem()
at System.Windows.Controls.DataGrid.OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
at System.Windows.Controls.ItemsControl.OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.Activate(Object item)
at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Run(Object arg)
at MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e)
at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
at System.Windows.ContextLayoutManager.UpdateLayout()
at System.Windows.UIElement.UpdateLayout()
例外具有堆栈跟踪,告诉你(和我们,如果我们将帮助你)_where_该异常长大。你能 –
@RenéVogt我用StackTrace编辑了我的问题 –
DDD.AutoRadio.Database.Editor.View.EditField.Overzicht_SelectionChanged是错误的地方,这不是你问题中的任何地方。该方法?为什么对象是模拟的? – Will