VS Developer Express for Windows Phone新项目显示XamlParseException

时间:2013-01-16 13:10:04

标签: windows-phone-7 visual-studio-2012 windows-phone-7.1 xamlparseexception

我最近安装了Windows 8和Visual Studio Express 2012 for Windows Phone。但是,如果我启动一个新的Windows Phone App项目(7.1,虽然8具有相同的问题)并打开MainPage.xaml文件,设计器窗口中会出现如下错误:

XamlParseException: Cannot find a Resource with the Name/Key PhoneBackgroundBrush [Line: 47 Position: 111]

MainPage.xaml文件如下:

<phone:PhoneApplicationPage 
    x:Class="PhoneApp1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>
    </Grid>

</phone:PhoneApplicationPage>

App.xaml文件包含:

<Application 
    x:Class="PhoneApp1.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"       
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">

    <!--Application Resources-->
    <Application.Resources>
    </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>

解决方案如下所示:

Project solution

在任何地方似乎没有任何其他与样式相关的文件。任何人都可以对此有所了解吗?它让我疯了。

2 个答案:

答案 0 :(得分:1)

唷!修正如下:

右键单击右侧解决方案资源管理器面板中的项目 选择添加 - &gt;现有项目 在文件浏览器中选择C:\ Program Files(x86)\ Microsoft SDKs \ Windows Phone \ v7.1 \ Design \ ThemeResources.xaml 点击“添加”按钮 在App.xaml文件中,添加以下内容替换以下部分:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ThemeResources.Xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

在MainPage.xaml打开设计器 - 现在应该可以正常工作。希望这能节省一些时间!

答案 1 :(得分:0)

根据您自己的答案,而不是复制原始的ThemesResources.xaml,我会创建一个新的并将其与原始版本合并。

创建一个新的资源字典(例如FixedDictionaryResources.xaml)。在Visual Studio中,您需要创建Windows Phone页面或使用任何其他创建XAML文件的模板。在Blend中,您可以直接创建资源字典。在VS中,您需要删除所有内容并输入以下代码。在Blend中添加

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!-- Resource dictionary entries should be defined here. -->
    <SolidColorBrush x:Key="PhoneBackgroundBrush" Color="{StaticResource PhoneBackgroundColor}"/>
</ResourceDictionary>

打开App.xaml并添加以下代码以将新字典与默认资源字典合并。

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/FixingResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

重新打开仍然显示PhoneBackgroundBrush缺失问题的页面,现在一切都应该正常工作。