Skip to content

Commit

Permalink
Handled x:ClassModifier attribute to set the visibility of rootType (d…
Browse files Browse the repository at this point in the history
  • Loading branch information
egvijayanand authored Feb 24, 2023
1 parent 53f6e39 commit 33240ea
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Controls/src/SourceGen/CodeBehindGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ static void GenerateXamlCodeBehind(ProjectItem projItem, Compilation compilation
return;
var uid = Crc64.ComputeHashString($"{compilation.AssemblyName}.{itemName}");

if (!TryParseXaml(text, uid, compilation, xmlnsDefinitionCache, context.CancellationToken, out var rootType, out var rootClrNamespace, out var generateDefaultCtor, out var addXamlCompilationAttribute, out var hideFromIntellisense, out var XamlResourceIdOnly, out var baseType, out var namedFields, out var parseException))
if (!TryParseXaml(text, uid, compilation, xmlnsDefinitionCache, context.CancellationToken, out var accessModifier, out var rootType, out var rootClrNamespace, out var generateDefaultCtor, out var addXamlCompilationAttribute, out var hideFromIntellisense, out var XamlResourceIdOnly, out var baseType, out var namedFields, out var parseException))
{
if (parseException != null)
context.ReportDiagnostic(Diagnostic.Create(Descriptors.XamlParserError, null, parseException.Message));
Expand Down Expand Up @@ -154,7 +154,7 @@ static void GenerateXamlCodeBehind(ProjectItem projItem, Compilation compilation
if (hideFromIntellisense)
sb.AppendLine($"\t[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]");

sb.AppendLine($"\tpublic partial class {rootType} : {baseType}");
sb.AppendLine($"\t{accessModifier} partial class {rootType} : {baseType}");
sb.AppendLine("\t{");

//optional default ctor
Expand Down Expand Up @@ -208,10 +208,11 @@ static void GenerateXamlCodeBehind(ProjectItem projItem, Compilation compilation
context.AddSource(hintName, SourceText.From(sb.ToString(), Encoding.UTF8));
}

static bool TryParseXaml(SourceText text, string uid, Compilation compilation, IList<XmlnsDefinitionAttribute> xmlnsDefinitionCache, CancellationToken cancellationToken, out string? rootType, out string? rootClrNamespace, out bool generateDefaultCtor, out bool addXamlCompilationAttribute, out bool hideFromIntellisense, out bool xamlResourceIdOnly, out string? baseType, out IEnumerable<(string, string, string)>? namedFields, out Exception? exception)
static bool TryParseXaml(SourceText text, string uid, Compilation compilation, IList<XmlnsDefinitionAttribute> xmlnsDefinitionCache, CancellationToken cancellationToken, out string? accessModifier, out string? rootType, out string? rootClrNamespace, out bool generateDefaultCtor, out bool addXamlCompilationAttribute, out bool hideFromIntellisense, out bool xamlResourceIdOnly, out string? baseType, out IEnumerable<(string, string, string)>? namedFields, out Exception? exception)
{
cancellationToken.ThrowIfCancellationRequested();

accessModifier = null;
rootType = null;
rootClrNamespace = null;
generateDefaultCtor = false;
Expand Down Expand Up @@ -283,6 +284,10 @@ static bool TryParseXaml(SourceText text, string uid, Compilation compilation, I
var typeArguments = GetAttributeValue(root, "TypeArguments", XamlParser.X2006Uri, XamlParser.X2009Uri);
baseType = GetTypeName(new XmlType(root.NamespaceURI, root.LocalName, typeArguments != null ? TypeArgumentsParser.ParseExpression(typeArguments, nsmgr, null) : null), compilation, xmlnsDefinitionCache);

// x:ClassModifier attribute
var classModifier = GetAttributeValue(root, "ClassModifier", XamlParser.X2006Uri, XamlParser.X2009Uri);
accessModifier = classModifier?.ToLowerInvariant().Replace("notpublic", "internal") ?? "public"; // notpublic is WPF for internal

return true;
}

Expand Down

0 comments on commit 33240ea

Please sign in to comment.