简体   繁体   English

尝试在Windows Phone 8项目中使用MVVM Light的App.xaml中的错误

[英]Errors in App.xaml trying to use MVVM Light in Windows Phone 8 project

When I add the MVVM Light package via NuGet I get errors referencing the lines in the App.xaml file added during the install. 当我通过NuGet添加MVVM Light软件包时,出现错误,引用了在安装过程中添加的App.xaml文件中的行。 These errors only appear in Windows Phone 8 projects. 这些错误仅出现在Windows Phone 8项目中。 The exact same lines in a Windows Phone 7 project do not raise any errors. Windows Phone 7项目中完全相同的行不会引发任何错误。 The MVVM Light added lines are: MVVM Light添加的行是:

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

These lines are positioned just before the ending </Application.Resources> tag. 这些行位于</Application.Resources>标记的紧前面。 The errors reported in the Error List pane are: 在“错误列表”窗格中报告的错误为:

  • Each dictionary must have an associated key 每个字典必须具有关联的键
  • The name "ViewModelLocator" does not exist in the namespace "clr-namespace:sdkVoiceAlarmClockWP8CS.ViewModel" 名称空间“ clr-namespace:sdkVoiceAlarmClockWP8CS.ViewModel”中不存在名称“ ViewModelLocator”

This seems to make sense since the <ResourceDictionary> tag does not have a key attribute. 这似乎是有道理的,因为<ResourceDictionary>标记没有键属性。 However, if I try to move this block of lines outside the 但是,如果我尝试将此线段移到 block, I get a whole new set of errors. 块,我得到了一套全新的错误。

As far as the ViewModelLocator problem is concerned, I double-checked and the following namespace is added to the <Application> tag as an attribute and it is not flagged with any errors: 就ViewModelLocator问题而言,我进行了仔细检查,并将以下名称空间作为属性添加到<Application>标记中,并且未将其标记为任何错误:

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

Why does this exact same set of lines work fine in a Windows Phone 7 project and how can I fix the namespace problems I am having in my Windows Phone 8 project? 为什么这组完全相同的行在Windows Phone 7项目中可以正常工作,如何解决我在Windows Phone 8项目中遇到的名称空间问题?

Just in case this is due to a more complex problem here is the entire App.xaml file: 万一这是由于更复杂的问题引起的,下面是整个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>

I had this problem before. 我以前有这个问题。 To make it work in WP8 replace this: 要使其在WP8中工作,请替换为:

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

With just this: 有了这个:

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

But I haven't bothered as to why it works on WP7 but not WP8 但是我没有为它为什么可以在WP7上工作而不是在WP8上工作而烦恼

将所有内容插入具有所有必要名称空间的ResourceDictionary中,而不是插入ApplicationResources中,如本stackoverflow问题中所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM