3

I'm trying to send data from Edge to my UWP app using the share contract. It works like a charm, except when I try to add the data received in this way into an ObservableCollection<T> I get this exception:

InvalidCastException: 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.*

Code looks as follows:

App.xaml.cs:

protected override void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
  {
     if (this.SharingService != null)
        await this.SharingService.OnShareTargetActivated(args);
  }

SharingService.cs:

public delegate void UriAddedEventHandler(object sender, UriAddedEventArgs args);
public event UriAddedEventHandler UriAdded;

public async Task OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
   var shareOperation = args.ShareOperation;

   if (shareOperation.Data.Contains(StandardDataFormats.WebLink))
   {
      var uri = await shareOperation.Data.GetWebLinkAsync();

      this.UriAdded?
         .Invoke(this, new UriAddedEventArgs { Uri = uri });
   }
}

ViewModel.cs:

public ViewModel(ISharingService sharingService)
{
   sharingService.UriAdded += OnUriAdded;
}

public ObservableCollection<Uri> collection = new ObservableCollection<Uri>();

private async void OnUriAdded(object sender, UriAddedEventArgs args)
{
   this.collection.Add(args.Uri));
}

The collection is bound to an element on the page, of course.

It appears I'm on a different thread when the event fires (no surprise there), but wrapping the operation in a

await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, ...)

doesn't change anything, I still get that exception. Has anybody got a clue what is going on here?

Edit:

For what it's worth, I tried to compile with the .NET native tool chain and spotted this warning:

Warning : DEP0810 : This app references Microsoft.NET.Native.Runtime.1.1, version 1.1.23231.0, found in your SDK, but you have a higher version of Microsoft.NET.Native.Runtime.1.1 installed on the target machine, 1.1.23406.0. If you continue to run this application, it will run against the currently installed version, Microsoft.NET.Native.Runtime.1.1, version 1.1.23406.0. Consider updating your SDK to match the version of Microsoft.NET.Native.Runtime.1.1 that is installed.

So yeah, I'm beginning to suspect some version conflict here. I have the latest Windows 10 SDK installed (10.0.26624.0) so I'm unsure how to update my SDK as suggested in the above warning.

3
  • 1
    this link may help
    – Oguz Ozgul
    Nov 6, 2015 at 23:50
  • Ok, so it seems that Edge uses a different version of the DLL in question than does my app. Question is, how do I fix this? Nov 7, 2015 at 7:18
  • 1
    I confirmed with MSFT that your .net native warning is expected. Nothing to worry about here.
    – Quincy
    Nov 10, 2015 at 21:40

1 Answer 1

5

Each window (your main application and your share window) have unique dispatchers.

You can not reference UI or dispatchers from one window to another window directly.

It is possible to invoke background thread and then use the dispatchers of other windows.

My advice is to get rid of all global getters of the dispatcher and use the dispatcher of the page (this.Dispatcher). The best practice is to use the dispatcher of the respective control or page that your going to display the content on.

1
  • Thanks for confirming what I had been suspecting for some time now. I'm going to work around this issue by saving the URI to a file (I need to do that anyway) and then watching the file from within the app thread; when the file is updated I'll read the contents and trigger the UI update from there. Nov 10, 2015 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.