1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
5 namespace System.Activities.Presentation.View
7 using System.CodeDom.Compiler;
8 using System.Globalization;
12 using Microsoft.CSharp;
13 using Microsoft.VisualBasic;
16 /// The class is not only used for VB(Dev10), but also for C# (Dev11).
18 internal class VBIdentifierName : DependencyObject
20 public static readonly DependencyProperty IdentifierNameProperty =
21 DependencyProperty.Register("IdentifierName", typeof(string), typeof(VBIdentifierName), new UIPropertyMetadata(OnIdentifierNameChanged));
23 public static readonly DependencyProperty IsValidProperty =
24 DependencyProperty.Register("IsValid", typeof(bool), typeof(VBIdentifierName));
26 public static readonly DependencyProperty ErrorMessageProperty =
27 DependencyProperty.Register("ErrorMessage", typeof(string), typeof(VBIdentifierName));
29 static VBCodeProvider vbProvider;
30 static CSharpCodeProvider csProvider;
31 static XamlSchemaContext xamlContext = new XamlSchemaContext();
32 static XamlType xamlType = new XamlType(typeof(string), xamlContext);
34 bool checkAgainstXaml;
36 VBCodeProvider VBProvider
40 if (vbProvider == null)
42 vbProvider = CodeDomProvider.CreateProvider("VisualBasic") as VBCodeProvider;
48 CSharpCodeProvider CSProvider
52 if (csProvider == null)
54 csProvider = CodeDomProvider.CreateProvider("C#") as CSharpCodeProvider;
61 public string ErrorMessage
63 get { return (string)GetValue(ErrorMessageProperty); }
64 set { SetValue(ErrorMessageProperty, value); }
69 get { return (bool)GetValue(IsValidProperty); }
70 set { SetValue(IsValidProperty, value); }
73 public string IdentifierName
75 get { return (string)GetValue(IdentifierNameProperty); }
76 set { SetValue(IdentifierNameProperty, value); }
79 public bool CheckAgainstXaml
83 return this.checkAgainstXaml;
87 public VBIdentifierName()
89 this.checkAgainstXaml = false;
92 public VBIdentifierName(bool checkAgainstXaml)
94 this.checkAgainstXaml = checkAgainstXaml;
97 static void OnIdentifierNameChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
99 ((VBIdentifierName)sender).OnIdentifierNameChanged();
102 internal static bool IsValidXamlName(string name)
104 bool isValid = new XamlMember(name, xamlType, false).IsNameValid;
108 //Work around TFS bug #825815, in some cases, XamlMember.IsNameValid returns true but it's not valid Xml Name.
111 XmlConvert.VerifyName(name);
122 void OnIdentifierNameChanged()
124 string trimedName = this.IdentifierName;
125 if (this.CheckAgainstXaml && !VBIdentifierName.IsValidXamlName(trimedName))
127 this.IsValid = false;
128 this.ErrorMessage = string.Format(CultureInfo.CurrentUICulture, SR.InvalidXamlMemberName, trimedName);
130 else if (!this.VBProvider.IsValidIdentifier(trimedName) || !this.CSProvider.IsValidIdentifier(trimedName))
132 this.IsValid = false;
133 this.ErrorMessage = string.Format(CultureInfo.CurrentUICulture, SR.InvalidIdentifier, trimedName);
135 else if (trimedName.StartsWith("[", StringComparison.Ordinal) && trimedName.EndsWith("]", StringComparison.Ordinal))
137 this.IsValid = false;
138 this.ErrorMessage = string.Format(CultureInfo.CurrentUICulture, SR.InvalidIdentifier, trimedName);
143 this.ErrorMessage = string.Empty;