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