在UWP app后面的代码中定义按钮的样式

时间:2016-01-02 00:19:29

标签: 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);
            }

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

类型&#39; Windows.UI.Xaml.Markup.XamlParseException&#39;的例外情况发生在App.exe中但未在用户代码中处理  WinRT信息:非法合格姓名[行:1职位:262]  附加信息:找不到与此错误代码关联的文本。

感谢您的帮助

1 个答案:

答案 0 :(得分:7)

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

在资源字典中定义样式并为其指定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的更常规方法。