|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Reflection; |
| 5 | +using System.Windows; |
| 6 | +using System.Windows.Data; |
| 7 | +using System.Xml; |
| 8 | +using Microsoft.VisualStudio.PlatformUI; |
| 9 | + |
| 10 | +namespace AphidVSShell.AboutBoxPackage |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Interaction logic for AboutBox.xaml |
| 14 | + /// </summary> |
| 15 | + public partial class AboutBox : DialogWindow |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Default constructor. |
| 19 | + /// </summary> |
| 20 | + public AboutBox() |
| 21 | + { |
| 22 | + InitializeComponent(); |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Handles click navigation on the hyperlink in the About dialog. |
| 27 | + /// </summary> |
| 28 | + /// <param name="sender">Object that sent the event.</param> |
| 29 | + /// <param name="e">Navigation events arguments.</param> |
| 30 | + private void hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e) |
| 31 | + { |
| 32 | + if (e.Uri != null && string.IsNullOrEmpty(e.Uri.OriginalString) == false) |
| 33 | + { |
| 34 | + string uri = e.Uri.AbsoluteUri; |
| 35 | + Process.Start(new ProcessStartInfo(uri)); |
| 36 | + e.Handled = true; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + #region AboutData Provider |
| 41 | + #region Member data |
| 42 | + private XmlDocument xmlDoc = null; |
| 43 | + |
| 44 | + private const string propertyNameTitle = "Title"; |
| 45 | + private const string propertyNameDescription = "Description"; |
| 46 | + private const string propertyNameProduct = "Product"; |
| 47 | + private const string propertyNameCopyright = "Copyright"; |
| 48 | + private const string propertyNameCompany = "Company"; |
| 49 | + private const string xPathRoot = "ApplicationInfo/"; |
| 50 | + private const string xPathTitle = xPathRoot + propertyNameTitle; |
| 51 | + private const string xPathVersion = xPathRoot + "Version"; |
| 52 | + private const string xPathDescription = xPathRoot + propertyNameDescription; |
| 53 | + private const string xPathProduct = xPathRoot + propertyNameProduct; |
| 54 | + private const string xPathCopyright = xPathRoot + propertyNameCopyright; |
| 55 | + private const string xPathCompany = xPathRoot + propertyNameCompany; |
| 56 | + private const string xPathLink = xPathRoot + "Link"; |
| 57 | + private const string xPathLinkUri = xPathRoot + "Link/@Uri"; |
| 58 | + #endregion |
| 59 | + |
| 60 | + #region Properties |
| 61 | + /// <summary> |
| 62 | + /// Gets the title property, which is display in the About dialogs window title. |
| 63 | + /// </summary> |
| 64 | + public string ProductTitle |
| 65 | + { |
| 66 | + get |
| 67 | + { |
| 68 | + string result = CalculatePropertyValue<AssemblyTitleAttribute>(propertyNameTitle, xPathTitle); |
| 69 | + if (string.IsNullOrEmpty(result)) |
| 70 | + { |
| 71 | + // otherwise, just get the name of the assembly itself. |
| 72 | + result = Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase); |
| 73 | + } |
| 74 | + return result; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Gets the application's version information to show. |
| 80 | + /// </summary> |
| 81 | + public string Version |
| 82 | + { |
| 83 | + get |
| 84 | + { |
| 85 | + string result = string.Empty; |
| 86 | + // first, try to get the version string from the assembly. |
| 87 | + Version version = Assembly.GetExecutingAssembly().GetName().Version; |
| 88 | + if (version != null) |
| 89 | + { |
| 90 | + result = version.ToString(); |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + // if that fails, try to get the version from a resource in the Application. |
| 95 | + result = GetLogicalResourceString(xPathVersion); |
| 96 | + } |
| 97 | + return result; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Gets the description about the application. |
| 103 | + /// </summary> |
| 104 | + public string Description |
| 105 | + { |
| 106 | + get { return CalculatePropertyValue<AssemblyDescriptionAttribute>(propertyNameDescription, xPathDescription); } |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Gets the product's full name. |
| 111 | + /// </summary> |
| 112 | + public string Product |
| 113 | + { |
| 114 | + get { return CalculatePropertyValue<AssemblyProductAttribute>(propertyNameProduct, xPathProduct); } |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Gets the copyright information for the product. |
| 119 | + /// </summary> |
| 120 | + public string Copyright |
| 121 | + { |
| 122 | + get { return CalculatePropertyValue<AssemblyCopyrightAttribute>(propertyNameCopyright, xPathCopyright); } |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Gets the product's company name. |
| 127 | + /// </summary> |
| 128 | + public string Company |
| 129 | + { |
| 130 | + get { return CalculatePropertyValue<AssemblyCompanyAttribute>(propertyNameCompany, xPathCompany); } |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Gets the link text to display in the About dialog. |
| 135 | + /// </summary> |
| 136 | + public string LinkText |
| 137 | + { |
| 138 | + get { return GetLogicalResourceString(xPathLink); } |
| 139 | + } |
| 140 | + |
| 141 | + /// <summary> |
| 142 | + /// Gets the link uri that is the navigation target of the link. |
| 143 | + /// </summary> |
| 144 | + public string LinkUri |
| 145 | + { |
| 146 | + get { return GetLogicalResourceString(xPathLinkUri); } |
| 147 | + } |
| 148 | + #endregion |
| 149 | + |
| 150 | + #region Resource location methods |
| 151 | + /// <summary> |
| 152 | + /// Gets the specified property value either from a specific attribute, or from a resource dictionary. |
| 153 | + /// </summary> |
| 154 | + /// <typeparam name="T">Attribute type that we're trying to retrieve.</typeparam> |
| 155 | + /// <param name="propertyName">Property name to use on the attribute.</param> |
| 156 | + /// <param name="xpathQuery">XPath to the element in the XML data resource.</param> |
| 157 | + /// <returns>The resulting string to use for a property. |
| 158 | + /// Returns null if no data could be retrieved.</returns> |
| 159 | + private string CalculatePropertyValue<T>(string propertyName, string xpathQuery) |
| 160 | + { |
| 161 | + string result = string.Empty; |
| 162 | + // first, try to get the property value from an attribute. |
| 163 | + object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(T), false); |
| 164 | + if (attributes.Length > 0) |
| 165 | + { |
| 166 | + T attrib = (T)attributes[0]; |
| 167 | + PropertyInfo property = attrib.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance); |
| 168 | + if (property != null) |
| 169 | + { |
| 170 | + result = property.GetValue(attributes[0], null) as string; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + // if the attribute wasn't found or it did not have a value, then look in an xml resource. |
| 175 | + if (result == string.Empty) |
| 176 | + { |
| 177 | + // if that fails, try to get it from a resource. |
| 178 | + result = GetLogicalResourceString(xpathQuery); |
| 179 | + } |
| 180 | + return result; |
| 181 | + } |
| 182 | + |
| 183 | + /// <summary> |
| 184 | + /// Gets the XmlDataProvider's document from the resource dictionary. |
| 185 | + /// </summary> |
| 186 | + protected virtual XmlDocument ResourceXmlDocument |
| 187 | + { |
| 188 | + get |
| 189 | + { |
| 190 | + if (xmlDoc == null) |
| 191 | + { |
| 192 | + // if we haven't already found the resource XmlDocument, then try to find it. |
| 193 | + XmlDataProvider provider = this.TryFindResource("aboutProvider") as XmlDataProvider; |
| 194 | + if (provider != null) |
| 195 | + { |
| 196 | + // save away the XmlDocument, so we don't have to get it multiple times. |
| 197 | + xmlDoc = provider.Document; |
| 198 | + } |
| 199 | + } |
| 200 | + return xmlDoc; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + /// <summary> |
| 205 | + /// Gets the specified data element from the XmlDataProvider in the resource dictionary. |
| 206 | + /// </summary> |
| 207 | + /// <param name="xpathQuery">An XPath query to the XML element to retrieve.</param> |
| 208 | + /// <returns>The resulting string value for the specified XML element. |
| 209 | + /// Returns empty string if resource element couldn't be found.</returns> |
| 210 | + protected virtual string GetLogicalResourceString(string xpathQuery) |
| 211 | + { |
| 212 | + string result = string.Empty; |
| 213 | + // get the About xml information from the resources. |
| 214 | + XmlDocument doc = this.ResourceXmlDocument; |
| 215 | + if (doc != null) |
| 216 | + { |
| 217 | + // if we found the XmlDocument, then look for the specified data. |
| 218 | + XmlNode node = doc.SelectSingleNode(xpathQuery); |
| 219 | + if (node != null) |
| 220 | + { |
| 221 | + if (node is XmlAttribute) |
| 222 | + { |
| 223 | + // only an XmlAttribute has a Value set. |
| 224 | + result = node.Value; |
| 225 | + } |
| 226 | + else |
| 227 | + { |
| 228 | + // otherwise, need to just return the inner text. |
| 229 | + result = node.InnerText; |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + return result; |
| 234 | + } |
| 235 | + #endregion |
| 236 | + #endregion |
| 237 | + } |
| 238 | +} |
0 commit comments