【问题标题】:define a style for a button in code behind UWP app在 UWP 应用程序后面的代码中为按钮定义样式
【发布时间】:2016-04-06 07:27:09
【问题描述】:

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

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

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

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

感谢帮助

【问题讨论】:

    标签: c# xaml win-universal-app


    【解决方案1】:

    与创建资源字典相比,您尝试这样做有什么特别的原因吗?我从未见过这种方法。

    在资源字典中定义你的样式并给它一个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;

    这将是在控件上使用样式的更传统方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-19
      • 1970-01-01
      • 2019-07-09
      • 2017-02-15
      • 1970-01-01
      • 2020-07-04
      相关资源
      最近更新 更多