I'm experimenting with Windows 8 "Metro Styled Apps", MVVM Light and want to create a share target - so far so good. But if I'm in the OnShareTargetActivated method and want to add an item to an ObservableCollection I catch an InvalidCastException between class type and COM Object.
Das COM-Objekt des Typs "System.Collections.Specialized.NotifyCollectionChangedEventHandler" kann nicht in den Klassentyp "System.Collections.Specialized.NotifyCollectionChangedEventHandler" umgewandelt werden. Instanzen von Typen, die COM-Komponenten repräsentieren, können nicht in andere Typen umgewandelt werden, die keine COM-Komponenten repräsentieren. Eine Umwandlung in Schnittstellen ist jedoch möglich, sofern die zugrunde liegende COM-Komponente QueryInterface-Aufrufe für die IID der Schnittstelle unterstützt.
English version:
Unable to cast COM object of type 'System.Collections.Specialized.NotifyCollectionChangedEventHandler' to class type 'System.Collections.Specialized.NotifyCollectionChangedEventHandler'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
Now Im a Little bit confused and don't know how to handle this behavior properly.
MainViewModel main1 = new ViewModelLocator().Main;
MainViewModel main2 = new MainViewModel();
var conversation = new ConversationViewModel();
conversation.Messages.Add(new MessageViewModel { Image = img, Text = "Share" });
main1.Conversations.Add(conversation); // error InvalidCastException
main2.Conversations.Add(conversation); // no error
Where img
is a newly created BitmapImage
ViewModelLocator
public class ViewModelLocator
{
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<UserViewModel>();
SimpleIoc.Default.Register<UriViewModel>();
}
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
//...
}
Stack Trace:
at System.StubHelpers.StubHelpers.GetCOMIPFromRCW_WinRTDelegate(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget)
at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
at System.Collections.ObjectModel.ObservableCollection1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
1.InsertItem(Int32 index, T item) at System.Collections.ObjectModel.Collection`1.Add(T item)
at System.Collections.ObjectModel.ObservableCollection
Instead of explicit casting use the "as" conversion, it sounds like the instance returned by the service locator is not a MainViewModel
object, change that line to
return ServiceLocator.Current.GetInstance() as MainViewModel;
It may behave differently, if the instance is not a MainviewModel
then it will return null
, this will help you debug why the instance returned null
from the service locator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With