共享数据并添加到 ObservableCollection 时出现 InvalidCastException
InvalidCastException when sharing data and adding to ObservableCollection
我正在尝试使用共享合同将数据从 Edge 发送到我的 UWP 应用程序。它就像一个魅力,除了当我尝试将以这种方式收到的数据添加到 ObservableCollection<T>
时,我得到这个异常:
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.*
代码如下:
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));
}
当然,集合绑定到页面上的元素。
事件触发时我似乎在不同的线程上(这并不奇怪),但是将操作包装在
await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, ...)
没有改变任何东西,我仍然得到那个例外。有人知道这里发生了什么吗?
编辑:
为了它的价值,我尝试使用 .NET 本机工具链进行编译并发现了这个警告:
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.
是的,我开始怀疑这里存在一些版本冲突。我安装了最新的 Windows 10 SDK (10.0.26624.0
),所以我不确定如何按照上述警告中的建议更新我的 SDK。
每个 window(您的主应用程序和您的共享 window)都有唯一的调度程序。
您不能将 UI 或调度程序从一个 window 直接引用到另一个 window。
可以调用后台线程,然后使用其他 windows 的调度程序。
我的建议是摆脱调度程序的所有全局获取器并使用页面的调度程序 (this.Dispatcher)。最佳做法是使用要在其上显示内容的相应控件或页面的调度程序。
我正在尝试使用共享合同将数据从 Edge 发送到我的 UWP 应用程序。它就像一个魅力,除了当我尝试将以这种方式收到的数据添加到 ObservableCollection<T>
时,我得到这个异常:
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.*
代码如下:
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));
}
当然,集合绑定到页面上的元素。
事件触发时我似乎在不同的线程上(这并不奇怪),但是将操作包装在
await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, ...)
没有改变任何东西,我仍然得到那个例外。有人知道这里发生了什么吗?
编辑:
为了它的价值,我尝试使用 .NET 本机工具链进行编译并发现了这个警告:
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.
是的,我开始怀疑这里存在一些版本冲突。我安装了最新的 Windows 10 SDK (10.0.26624.0
),所以我不确定如何按照上述警告中的建议更新我的 SDK。
每个 window(您的主应用程序和您的共享 window)都有唯一的调度程序。
您不能将 UI 或调度程序从一个 window 直接引用到另一个 window。
可以调用后台线程,然后使用其他 windows 的调度程序。
我的建议是摆脱调度程序的所有全局获取器并使用页面的调度程序 (this.Dispatcher)。最佳做法是使用要在其上显示内容的相应控件或页面的调度程序。