【发布时间】:2025-12-11 08:10:01
【问题描述】:
我正在使用 MVVM Light,一切都很好,除了将我的 Windows Phone 8.1 WinRT 应用程序作为共享目标启动。
当我尝试分配 MainViewModel viewModel = ServiceLocator.Current.GetInstance<MainViewModel>(); 时,ServiceLocator.Current 出现异常。
异常消息:必须设置 ServiceLocationProvider。
我是否需要在 App.xaml.cs OnShareTargetActivated 事件中做一些额外的事情来确保定位器正在运行?
更新:
需要将 ShareTarget 页面视为您的应用程序的一个小扩展。似乎并非所有应用程序的资源都已加载(包括 App.xaml 中的应用程序范围的资源)。所以我只是在共享页面的构造函数中创建了一个新的 MainViewModel 实例,只加载了完成共享所需的内容,保存信息并调用ShareOperation.ReportCompleted。这会将用户返回到正在共享的应用程序。
我仍然没有找到在我的 ViewModel 中获取其他资源的好解决方案,但目前可行。
【问题讨论】:
标签: windows-runtime winrt-xaml windows-phone-8.1 mvvm-light