gpt4 book ai didi

c# - 在 UWP 应用程序背后的代码中为按钮定义样式

转载 作者:太空宇宙 更新时间:2023-11-03 18:25:59 25 4
gpt4 key购买 nike

我试图在后面的代码中为我的按钮应用样式,如下所示:

使用 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);
}

但是我在执行我的应用程序时遇到了问题,这是我得到的错误:

“Windows.UI.Xaml.Markup.XamlParseException”类型的异常发生在 App.exe 中,但未在用户代码中处理 WinRT 信息:非法限定名称字符 [行:1 位置:262] 附加信息:找不到与此错误代码关联的文本。

感谢帮助

最佳答案

与创建资源字典相比,您尝试这样做是否有特殊原因?我从未见过这种方法。

在资源字典中定义你的 Style 并给它一个 x:Key="MyStyleName"

在 app.xaml 中加载字典

<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Common/StandardStyles.xaml"/>
<ResourceDictionary Source="Common/MyStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>

在任何地方引用你的风格:

myStyle = (Style)App.Current.Resource["MyStyleName"];

像这样使用:MenuButton2.Style = myStyle;

这是在控件上使用 syle 的更传统的方法。

关于c# - 在 UWP 应用程序背后的代码中为按钮定义样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34561416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com