C# 在代码隐藏中解析XAML样式并加载自定义控件 我有一个C++程序,用Roslyn解析C代码。 我需要将我的样式和自定义控件转换为“代码隐藏”

C# 在代码隐藏中解析XAML样式并加载自定义控件 我有一个C++程序,用Roslyn解析C代码。 我需要将我的样式和自定义控件转换为“代码隐藏”,c#,wpf,xaml,roslyn,C#,Wpf,Xaml,Roslyn,例如,我有一个简单的自定义控件,其中包含一个按钮 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"> <

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

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>
现在,我需要知道如何将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";
string code\u xaml=“\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n”;
然后使用XamlParser对其进行解析,并将其加载到customcontrol1

可能吗? 谢谢

答案是:

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

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

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

    public CustomControl1()
    {
        ResourceDictionary Parse_Resource = XamlReader.Parse(code_xaml) as ResourceDictionary;
        this.Resources = Parse_Resource;
    }
    
  • 答案:D