【发布时间】:2014-03-25 11:09:32
【问题描述】:
我正在开发 windows phone 8 应用程序。
我尝试使用 Xaml Styles 为我的 WP 8 应用程序添加一个通用样式表。
我尝试以下步骤-:
第 1 步:添加一个新的文本文件并保存扩展名为 .xaml [Stylessheet.xaml]
Stylessheet.xamlTextBox 样式的代码
<ResourceDictionary
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"
mc:Ignorable="d">
<Style x:Name="Textblockstyle" TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"></Setter>
<Setter Property="FontWeight" Value="ExtraBold"></Setter>
</Style>
</ResourceDictionary>
Stpe2:在App.xaml 页面中,我添加了stylessheet.xaml 资源
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Stylessheet.xaml"/>
</ResourceDictionary.MergedDictionaries>
<local:LocalizedStrings xmlns:local="clr-namespace:PhoneApp1" x:Key="LocalizedStrings"/>
</ResourceDictionary>
</Application.Resources>
第 3 步: mainpage.xaml 为 textBlock 应用样式
<TextBlock Name="txtblock" Style="{StaticResource Textblockstyle}">Hi this is for sample</TextBlock>
在设计窗口屏幕中应用此代码后,文本颜色变为红色和粗体。
但我尝试运行此代码时,我在App.xaml.cs 页面中的InitializeComponent() 行中出现错误;
错误是“发生 XamlParseException”
System.Windows.ni.dll 中发生了“System.Windows.Markup.XamlParseException”类型的第一次机会异常
我尝试了一些堆栈溢出解决方案,但对我不起作用。 请说明为什么会出现此错误以及如何解决。
我的文件路径结构
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>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Stylessheet.xaml"/>
</ResourceDictionary.MergedDictionaries>
<local:LocalizedStrings xmlns:local="clr-namespace:PhoneApp1" x:Key="LocalizedStrings"/>
</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>
由于这条线出现问题
这段代码有什么问题
<TextBlock Name="txtblock2" Margin="0,50,0,0">This is second sample</TextBlock>
<TextBlock Name="txtblock3" Margin="0,100,0,0," Style="{StaticResource Textblockstyle}">Final sample Text</TextBlock>
存在 Margin="0,100,0,0" 时发生错误。
去掉这个边距之后。代码运行正确。这段代码有什么问题。
【问题讨论】:
-
Source="/Stylessheet.xaml" 是正确的文件路径??
-
但我删除了它显示的“/”查找资源字典“Stylessheet.xaml”时出错
-
JaiHind 现在我添加我的文件结构图片供您参考。
-
@Gurunathan 我已经根据您的代码构建了一个简单的示例,它运行良好。也许您在 App.xaml 的其他地方有错误?
-
@Gurunathan 问题已解决,将答案标记为已接受。我认为 Romasz 应该添加他的答案,让您将其标记为已接受。
标签: c# xaml c#-4.0 windows-phone-8