Caliburn.Micro 中的 WinRT 8.1 设置
本文关键字:设置 WinRT Micro 中的 Caliburn | 更新日期: 2023-09-27 18:34:02
我正在尝试使用 VS2013 RC 在 Caliburn.Micro WinRT 8.1 应用程序中打开设置视图,但在打开它时不断收到未经处理的异常,并显示以下消息:
值不能为空。参数名称:无法分析应用清单中的可视元素。
我可以通过以下步骤重现这些问题:
- 使用空白应用模板从 VS2013 RC 创建新的 Windows 应用商店应用。
- 通过NuGet添加Caliburn.Micro。
- 在 App.xaml 中,将基类更改为 caliburn:CaliburnApplication(命名空间声明为 xmlns:caliburn="using:Caliburn.Micro"(。
- 在 App.xaml.cs 中,像这样更改类(对于我遵循的基于 CM 的设置 http://compiledexperience.com/blog/posts/settings-caliburn(
代码如下:
public sealed partial class App
{
private WinRTContainer _container;
public App()
{
InitializeComponent();
}
protected override void Configure()
{
_container = new WinRTContainer();
_container.RegisterWinRTServices();
_container.PerRequest<MainViewModel>();
_container.PerRequest<SettingsViewModel>();
ISettingsService settings = _container.RegisterSettingsService();
settings.RegisterCommand<SettingsViewModel>("Test settings");
}
protected override object GetInstance(Type service, string key)
{
var instance = _container.GetInstance(service, key);
if (instance != null) return instance;
throw new Exception("Could not locate any instances.");
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return _container.GetAllInstances(service);
}
protected override void BuildUp(object instance)
{
_container.BuildUp(instance);
}
protected override void PrepareViewFirst(Frame rootFrame)
{
_container.RegisterNavigationService(rootFrame);
}
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
DisplayRootView<MainView>();
}
}
- 最后,在解决方案中为视图和视图模型创建文件夹,向其添加所需的项:主视图模型、设置视图模型、主视图、设置视图。视图只包含一个带有一些文本的文本块。MainViewModel 源自 Screen,而 SettingsViewModel 源自 PropertyChangedBase。其中任何一个都没有相关的代码。
可以看到主视图;然后我打开超级按钮栏并单击设置,我找到指向我的应用程序设置的标签;当我单击它时,我得到上面引用的异常。有什么提示吗?
您可以在此处找到完整的重现解决方案:http://sdrv.ms/18GIMvB 。
如果尚未准备好迁移到 CM 的 alpha 版本,可以通过 NuGet 将 Callisto 更新到 1.4.0。 这为我解决了错误。
似乎新的 CM 版本(alpha 2(解决了这个问题,所以我在这里添加更多信息来帮助像我这样的其他新手。这是我现在正在做的事情:
在应用程序的Configure
中,我有一些引导代码,例如:
...
ResourceLoader loader = ResourceLoader.GetForViewIndependentUse("Resources");
ISettingsService settings = _container.RegisterSettingsService();
settings.RegisterFlyoutCommand<ContentSettingsViewModel>(loader.GetString("SettingsContent"));
ContentSettingsViewModel
是用于筛选某些内容的视图模型。从资源获取的字符串是将显示在设置浮出控件中的标签(请确保此字符串有一个条目,因为传递空字符串或 null 字符串会触发异常(。此 VM 派生自 CM Screen
,因为我将覆盖OnActivate
,并在用户打开或关闭设置页面时OnDeactivate
加载和保存设置。