簡體   English   中英

嘗試在Windows Phone 8項目中使用MVVM Light的App.xaml中的錯誤

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

當我通過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>標記沒有鍵屬性。 但是,如果我嘗試將此線段移到 塊,我得到了一套全新的錯誤。

就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>

我以前有這個問題。 要使其在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上工作而煩惱

將所有內容插入具有所有必要名稱空間的ResourceDictionary中,而不是插入ApplicationResources中,如本stackoverflow問題中所述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM