6

当我通过 NuGet 添加 MVVM Light 包时,引用安装期间添加的 App.xaml 文件中的行时出现错误。这些错误仅出现在 Windows Phone 8 项目中。Windows Phone 7 项目中完全相同的行不会引发任何错误。MVVM Light 添加的行是:

<ResourceDictionary>
    <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
     <ResourceDictionary.MergedDictionaries></ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

这些行位于结束</Application.Resources>标记之前。错误列表窗格中报告的错误是:

  • 每个字典必须有一个关联的键
  • 命名空间“clr-namespace:sdkVoiceAlarmClockWP8CS.ViewModel”中不存在名称“ViewModelLocator”

这似乎是有道理的,因为<ResourceDictionary>标记没有 key 属性。但是,如果我尝试将这行代码块移到块,我得到了一组全新的错误。

就 ViewModelLocator 问题而言,我仔细检查了以下命名空间作为属性添加到<Application>标记中,并且没有标记任何错误:

xmlns:vm="clr-namespace:sdkVoiceAlarmClockWP8CS.ViewModel" mc:Ignorable="d"

为什么这组完全相同的行在 Windows Phone 7 项目中可以正常工作?如何解决我在 Windows Phone 8 项目中遇到的命名空间问题?

以防万一这是由于更复杂的问题,这里是整个App.xaml文件:

<?xml version="1.0" encoding="utf-8"?>
<!-- 
    Copyright (c) 2012 Microsoft Corporation.  All rights reserved.
    Use of this sample source code is subject to the terms of the Microsoft license 
    agreement under which you licensed this sample source code and is provided AS-IS.
    If you did not accept the terms of the license agreement, you are not authorized 
    to use this sample source code.  For the terms of the license, please see the 
    license agreement between you and Microsoft.

    To see all Code Samples for Windows Phone, visit http://go.microsoft.com/fwlink/?LinkID=219604
-->
<Application x:Class="AlarmClockWithVoice.App" 
             xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
             xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
             xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" 
             xmlns:telerikPrimitives="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Primitives" 
             xmlns:p1="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:vm="clr-namespace:sdkVoiceAlarmClockWP8CS.ViewModel" mc:Ignorable="d"
             >

    <!--Application Resources-->
    <Application.Resources>
        <Style x:Key="TransitionPageStyle" TargetType="phone:PhoneApplicationPage">
            <Setter Property="toolkit:TransitionService.NavigationInTransition">
                <Setter.Value>
                    <toolkit:NavigationInTransition>
                        <toolkit:NavigationInTransition.Backward>
                            <toolkit:TurnstileTransition Mode="BackwardIn" />
                        </toolkit:NavigationInTransition.Backward>
                        <toolkit:NavigationInTransition.Forward>
                            <toolkit:TurnstileTransition Mode="ForwardIn" />
                        </toolkit:NavigationInTransition.Forward>
                    </toolkit:NavigationInTransition>
                </Setter.Value>
            </Setter>
            <Setter Property="toolkit:TransitionService.NavigationOutTransition">
                <Setter.Value>
                    <toolkit:NavigationOutTransition>
                        <toolkit:NavigationOutTransition.Backward>
                            <toolkit:TurnstileTransition Mode="BackwardOut" />
                        </toolkit:NavigationOutTransition.Backward>
                        <toolkit:NavigationOutTransition.Forward>
                            <toolkit:TurnstileTransition Mode="ForwardOut" />
                        </toolkit:NavigationOutTransition.Forward>
                    </toolkit:NavigationOutTransition>
                </Setter.Value>
            </Setter>
        </Style>
        <ResourceDictionary>
            <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
            <ResourceDictionary.MergedDictionaries></ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

    <Application.ApplicationLifetimeObjects>
        <!--Required object that handles lifetime events for the application-->
        <shell:PhoneApplicationService Launching="Application_Launching" Closing="Application_Closing" Activated="Application_Activated" Deactivated="Application_Deactivated" />
    </Application.ApplicationLifetimeObjects>

</Application>
4

2 回答 2

2

我以前遇到过这个问题。要使其在 WP8 中工作,请替换:

<ResourceDictionary>
    <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
    <ResourceDictionary.MergedDictionaries></ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

就这样:

<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />

但我没有在意为什么它适用于 WP7 而不是 WP8

于 2013-05-13T02:59:10.760 回答
0

将所有内容插入 ResourceDictionary 而不是 ApplicationResources 与所有必要的命名空间,正如在这个stackoverflow question中已经提到的那样。

于 2014-06-12T06:26:09.040 回答