Windows store apps 可移植类库-Windows应用商店应用程序:“;无法加载文件或程序集Microsoft.Threading.Task“;
我有一个MVVM轻型基础设施,它包含在一个可移植类库中,目标是.Net 4、SL5、Win 8、WP 8.1、WPSL 8、Xamarin.Android和Xamarin.iOS。这对WPF客户端非常有效,但是,当我尝试在Windows应用商店App/Win 8环境中使用它时,我遇到了一些阻力。第一个问题在App.xaml中找到:Windows store apps 可移植类库-Windows应用商店应用程序:“;无法加载文件或程序集Microsoft.Threading.Task“;,windows-store-apps,mvvm-light,portable-class-library,Windows Store Apps,Mvvm Light,Portable Class Library,我有一个MVVM轻型基础设施,它包含在一个可移植类库中,目标是.Net 4、SL5、Win 8、WP 8.1、WPSL 8、Xamarin.Android和Xamarin.iOS。这对WPF客户端非常有效,但是,当我尝试在Windows应用商店App/Win 8环境中使用它时,我遇到了一些阻力。第一个问题在App.xaml中找到: <Application x:Class="Win8Client.App" xmlns="http://schemas.microsoft.com/winfx/
<Application
x:Class="Win8Client.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:INPS.Vision.Appointments.Shared.ViewModels"
xmlns:local="using:Win8Client">
<Application.Resources>
<vm:ViewModelLocator x:Key="Locator" />
</Application.Resources>
</Application>
我在slots属性的setter中得到以下异常,它利用了ViewModelBase的MVVM Lights Set方法。Slots属性尚未绑定到任何UI
例外情况:
“应用程序调用了为其他线程封送的接口。(HRESULT的异常:0x8001010E(RPC_E_错误的_线程))”
插槽属性:
public List<Slot> Slots
{
get { return _slots; }
set
{
Set(SlotsPropertyName, ref _slots, value); // <-- Exception thrown here
}
}
公共列表插槽
{
获取{return\u slots;}
设置
{
Set(SlotsPropertyName,ref _slots,value);//第一个问题“无法加载文件或程序集'Microsoft.Threading.Tasks',version=1.0.12.0…”我还没有计算出来,但有时我确实看到了设计数据。看起来非常喜怒无常
第二个问题——我之所以这么想是因为它在WPF中“刚刚工作”,我认为它在Windows应用商店应用程序中也会工作。错。看起来Windows应用商店应用程序处理异步/等待线程的方式不同——这是我的猜测
修复:使用单个方法声明在PCL中创建IDispatcherHelper接口:
void RunOnUIThread(Action action);
然后在每个实现IDispatcherHelper的平台特定项目(WPF/Windows 8.1)中创建一个具体的DispatcherHelper类。每个实现只调用MVVM Lights:
DispatcherHelper.CheckBeginInvokeOnUI(action);
在WPF和Windows 8.1中的App.xaml.cs中,我只是使用IDispatcherHelper作为句柄,使用MVVM Lights SimpleIoc注册了具体的实现。然后在视图模型中,我通过接口使用特定于平台的实现:
var slots = await _appointmentsDataProvider.GetAppointments(SelectedDate);
IDispatcherHelper dispatcherHelper = SimpleIoc.Default.GetInstance<IDispatcherHelper>();
dispatcherHelper.RunOnUIThread(() =>
{
Slots = slots;
});
var slots=wait\u appointsdataprovider.getappoints(SelectedDate);
IDispatcherHelper dispatcherHelper=SimpleIoc.Default.GetInstance();
dispatcherHelper.RunOnUIThread(()=>
{
槽=槽;
});
喜欢抽象!发生跨线程异常是因为您正在从后台线程更新插槽
,因此它与异步
/等待
无关。WPF确实有特殊的逻辑来允许这一点,这在其他平台中是不允许的。总有比使用dispatcher更好的解决方案:通常ode>await
或Progress
能够更优雅地解决问题。我在原始代码中使用了await,但运气不佳。我将查看进度。谢谢。对于程序集加载错误,请尝试以下操作:卸载所有Microsoft.Bcl
NuGet软件包,从app.config
中删除所有程序集重定向,然后安装这些软件包的最新版本。
var slots = await _appointmentsDataProvider.GetAppointments(SelectedDate);
IDispatcherHelper dispatcherHelper = SimpleIoc.Default.GetInstance<IDispatcherHelper>();
dispatcherHelper.RunOnUIThread(() =>
{
Slots = slots;
});