写在前面的话
无论什么系统,都会有一个“关于”页面,用来显示系统、开发人员以及版权等信息。在Windows Form中,VS2013已经给出了现成的“关于”框模板,但是不知道为什么WPF中并没有给出,不过我们可以参照Windows Form的“关于”框来打造自己的。
Windows Form
- 在建立窗体之前,先来完善自己系统的程序集信息
- 打开自己项目的“属性“,选择应用程序中的程序集信息
- 填写自己的程序集信息
- 信息完成后,来设计自己的窗体,右击自己的项目,选择”添加““新建项”
- 窗体如下所示:
- 到这里工作就完成了,运行既可。
WPF
- 完善程序集信息参照前面的Windows Form,现在需要做的是设计一个WPF窗体
- 推荐大家使用Blend,首先完成页面布局,然后添加我们所需要的lable,button等控件,左面可以放自己系统的Logo,右面是一些系统信息,自由发挥。
- 页面设计好之后,还需要后台代码来获取程序集信息,复制Windows Form的后台代码就可以,这里把代码贴出来,方便大家参考。
- 注意:需要使用反射
using System.Reflection;
- 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using System.Reflection;
namespace JFUI
{
/// <summary>
/// frmAbout.xaml 的交互逻辑
/// </summary>
public partial class frmAbout : MetroWindow
{
private static frmAbout instance;
public frmAbout()
{
InitializeComponent();
this.Title = String.Format("关于 {0}", AssemblyTitle);
this.lblProductName.Content = AssemblyProduct;
this.lblVersion.Content = String.Format("版本 {0}", AssemblyVersion);
this.lblCopyright.Content = AssemblyCopyright;
this.lblCompanyName.Content = AssemblyCompany;
this.txtDetailDescription.Text = AssemblyDescription;
btnOK.Focus();
}
public static frmAbout GetInstance()
{
if (instance == null)
{
instance = new frmAbout();
}
return instance;
}
private void MetroWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
instance = null;
}
private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
{
this.Title = String.Format("关于 {0}", AssemblyTitle);
this.lblProductName.Content = AssemblyProduct;
this.lblVersion.Content = String.Format("版本 {0}", AssemblyVersion);
this.lblCopyright.Content = AssemblyCopyright;
this.lblCompanyName.Content = AssemblyCompany;
this.txtDetailDescription.Text = AssemblyDescription;
}
#region 程序集特性访问器
public string AssemblyTitle
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
if (attributes.Length > 0)
{
AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
if (titleAttribute.Title != "")
{
return titleAttribute.Title;
}
}
return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
}
}
public string AssemblyVersion
{
get
{
return Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
}
public string AssemblyDescription
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return ((AssemblyDescriptionAttribute)attributes[0]).Description;
}
}
public string AssemblyProduct
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return ((AssemblyProductAttribute)attributes[0]).Product;
}
}
public string AssemblyCopyright
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
}
}
public string AssemblyCompany
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return ((AssemblyCompanyAttribute)attributes[0]).Company;
}
}
#endregion
private void btnOK_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
- 运行后: