han*_*ali 1 c# xaml win-universal-app
我尝试在后面的代码中为我的按钮应用样式,如下所示:
使用Windows.UI.Xaml.Markup;
MenuButton2.Style = ButtonStyle();
private Style ButtonStyle()
{
string xaml =
"<Style " +
"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' " +
"TargetType='Button'>" +
"<Setter Property='Foreground' Value='#e6e6e6'/>" +
" < Setter Property = 'Template' >" +
" < Setter.Value >" +
"< ControlTemplate TargetType = 'Button' >" +
"< Grid >" +
"< VisualStateManager.VisualStateGroups >" +
"< VisualStateGroup x: Name = 'CommonStates' >" +
"< VisualState x: Name = 'Normal' />" +
"< VisualState x: Name = 'PointerOver' >" +
" < Storyboard >" +
"< ColorAnimation Duration = '0' To = '#e6e6e6' Storyboard.TargetProperty = '(Rectangle.Fill).(SolidColorBrush.Color)' Storyboard.TargetName = 'Button' />" +
"</ Storyboard >" +
"</ VisualState >" +
"< VisualState x: Name = 'Pressed' >" +
" < Storyboard >" +
" < ColorAnimation Duration = '0' To = '#e6e6e6' Storyboard.TargetProperty = '(Rectangle.Fill).(SolidColorBrush.Color)' Storyboard.TargetName = 'Button' />" +
"< ColorAnimation Duration = '0' To = '#e6e6e6' Storyboard.TargetProperty = '(TextBlock.Foreground).(SolidColorBrush.Color)' Storyboard.TargetName = 'Content' />" +
"</ Storyboard > " +
" </ VisualState >" +
" </ VisualStateGroup >" +
" </ VisualStateManager.VisualStateGroups >" +
"< Grid >" +
" < Rectangle x: Name = 'Button' Stroke = 'Transparent' Fill = 'Transparent' Margin = '0' />" +
" < ContentPresenter x: Name = 'Content' />" +
"</ Grid >" +
" </ Grid >" +
"</ ControlTemplate >" +
" </ Setter.Value >" +
" </ Setter >" +
"</Style>";
return (Style)Windows.UI.Xaml.Markup.XamlReader.Load(xaml);
}
Run Code Online (Sandbox Code Playgroud)
但是当我执行我的应用程序时遇到问题,这是我得到的错误:
App.exe中出现"Windows.UI.Xaml.Markup.XamlParseException"类型的异常但未在用户代码中处理WinRT信息:非法限定名称字符[行:1位置:262]其他信息:与此关联的文本找不到错误代码.
感谢帮助
您是否有特别的原因试图这样做而不是创建资源字典?我从未见过这种方法.
在资源字典中定义样式并给它一个 x:Key="MyStyleName"
在app.xaml中加载字典
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Common/StandardStyles.xaml"/>
<ResourceDictionary Source="Common/MyStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
Run Code Online (Sandbox Code Playgroud)
在任何地方引用您的风格:
myStyle = (Style)App.Current.Resource["MyStyleName"];
Run Code Online (Sandbox Code Playgroud)
使用它像: MenuButton2.Style = myStyle;
这将是在您的控件上使用syle的更传统的方法.
| 归档时间: |
|
| 查看次数: |
3193 次 |
| 最近记录: |