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

define a style for a button in code behind UWP 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);
            }

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

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

感谢帮助

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

在资源字典中定义您的样式并给它一个 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 的更传统的方法。