我正在开发windows phone 8应用程序。
我尝试使用Xaml Styles为我的WP 8应用添加一个通用样式表。
我尝试以下步骤 - :
第1步:添加新的文本文件,并以扩展名.xaml [Stylessheet.xaml
]
Stylessheet.xaml
TextBox
样式代码
<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”存在时发生错误。
删除此保证金后。代码运行正常。这段代码有什么问题。
答案 0 :(得分:2)
您的样式表工作正常,问题在下面一行
<TextBlock Name="txtblock3" Margin="0,100,0,0," Style="{StaticResource Textblockstyle}">Final sample Text</TextBlock>*
保证金需要额外的逗号
<TextBlock Name="txtblock3" Margin="0,100,0,0" Style="{StaticResource Textblockstyle}">Final sample Text</TextBlock>
答案 1 :(得分:1)
经过一番讨论后发现问题是由Margin
中的额外逗号引起的:
<TextBlock Name="txtblock3" Margin="0,100,0,0," Style="{StaticResource Textblockstyle}">Final sample Text</TextBlock>
答案 2 :(得分:0)
从MergedDictionary的ResourceDictionary中的Source属性中删除正斜杠:
...来源= <强>&#34; Stylessheet.xaml&#34; 强> ...
而不是
...来源= <强>&#34; /Stylessheet.xaml" 强> ...