【问题标题】:Parse XAML styles in code behind and load custom control在代码后面解析 XAML 样式并加载自定义控件
【发布时间】:2018-05-09 19:05:39
【问题描述】:

我有一个使用 Roslyn 解析 C# 代码的 c++ 程序。 我需要将我的样式和自定义控件转换为“代码隐藏”。

例如我有一个简单的自定义控件包含一个按钮。

XAML 样式:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CUSTOM_LIBRARY_PARSE">
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Button Background="#FF487DF0" >
                            <Label VerticalContentAlignment="Center" HorizontalContentAlignment="Center" OpacityMask="#FFC3C3C3" Content="{Binding text_of_button_Value, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:CustomControl1}}}" />
                        </Button>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

控制背后的代码:

using System;
using System.Windows;
using System.Windows.Controls;
namespace CUSTOM_LIBRARY_PARSE
{
    public class CustomControl1 : Control
    {
        public static readonly DependencyProperty text_of_button
= DependencyProperty.Register(
"text_of_button_Value",
typeof(string),
typeof(CustomControl1),
new PropertyMetadata(Environment.UserName)
);
        public string text_of_button_Value
        {
            get { return (string)GetValue(text_of_button); }
            set { SetValue(text_of_button, value); }
        }
        static CustomControl1()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
        }
    }
}

现在,我需要知道如何将 xaml 代码作为字符串嵌入到后面的代码中:

  string code_xaml = "<ResourceDictionary\n    xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"\n    xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"\n    xmlns:local=\"clr-namespace:CUSTOM_LIBRARY_PARSE\">\n    <Style TargetType=\"{x:Type local:CustomControl1}\">\n        <Setter Property=\"Template\">\n            <Setter.Value>\n                <ControlTemplate TargetType=\"{x:Type local:CustomControl1}\">\n                    <Border Background=\"{TemplateBinding Background}\"\n                            BorderBrush=\"{TemplateBinding BorderBrush}\"\n                            BorderThickness=\"{TemplateBinding BorderThickness}\">\n                        <Button Background=\"#FF487DF0\" >\n                            <Label VerticalContentAlignment=\"Center\" HorizontalContentAlignment=\"Center\" OpacityMask=\"#FFC3C3C3\" Content=\"{Binding text_of_button_Value, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:CustomControl1}}}\" />\n                        </Button>\n                    </Border>\n                </ControlTemplate>\n            </Setter.Value>\n        </Setter>\n    </Style>\n</ResourceDictionary>\n";

然后使用 XamlParser 对其进行解析并将其加载到 customcontrol1

这可能吗? 谢谢

【问题讨论】:

    标签: c# wpf xaml roslyn


    【解决方案1】:

    答案是:

    1. 创建自定义控件的公共版本:

      public CustomControl1() { }

    2. 创建自定义控件的公共版本:

      public CustomControl1()
      {
          ResourceDictionary Parse_Resource = XamlReader.Parse(code_xaml) as ResourceDictionary;
          this.Resources = Parse_Resource;
      }
      

    已解决:D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      • 1970-01-01
      • 1970-01-01
      • 2016-02-22
      • 2013-06-26
      相关资源
      最近更新 更多