WPF笔记一

笔记内容:

  1. BUG、WPF运行窗体时调用Hide()方法,然后再Show()异常的解决方案
  2. WPF 窗体设置为无边框
  3. 选择本地文件
  4. 选择文件夹
  5. WPF实现右下角弹出消息窗口
  6. WPF 显示 HTTP 网络图片
  7. 获得当前应用软件的版本
  8. 获取匿名对象(object)的属性值
  9. WPF *.exe给另一个*.exe传值
  10. C# zip压缩与zip解压,下载地址:点击此处DotNetZip
  11. C#通过文件路径获取文件名
  12. C#中 is判断类型 和 as强制转换
  13. WPF 获取程序路径的一些方法,根据程序路径获取程序集信息 

1BUG、WPF运行窗体时调用Hide()方法,然后再Show()异常的解决方案

XAML页面:
Closed="Window_Closed"

CS后台:

private void Window_Closed(object sender, EventArgs e)
{
this.Hide();
}

  

当再次显示Show()的时候出现异常;

noti.Click += delegate
{
this.Show();
};

  

解决方案;重写OnClosing事件 解决窗口隐藏后不能再开的bug。

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
this.Hide();
e.Cancel = true;	//不关闭窗口
}

  

关闭事件Closing 和Closed的区别
在窗口真正关闭之前,先触发Closing,然后才会触发 Closed。

2、WPF 窗体设置为无边框

方案1:
<Window Height="0" Width="0" WindowStyle="None">

3、选择本地文件

private void OpenFile_Click(object sender, RoutedEventArgs e)
{
    // 实例化OpenFileDialog
    var openFileDialog = new Microsoft.Win32.OpenFileDialog()
    {
        Filter = "*.xml|*.XML",	    //默认扩展名
        Title = "选择文件",	    //将显示在对话框标题栏中的标题
	InitialDirectory="c:\",    //默认选择文件的路径
	RestoreDirectory=true	    //记录上次选择的路径
    };
    var result = openFileDialog.ShowDialog();
    if (result == true)
    {                
	//取得文件路径
        string filename = openFileDialog.FileName;
    }
}

4、选择文件夹
选择文件夹使用的是System.Windows.Forms的密封类FolderBrowserDialog
所有需要添加引用:using System.Windows.Forms;

private void dialogFolder_Click(object sender, RoutedEventArgs e)
{
    var dialog = new FolderBrowserDialog()
    {
        SelectedPath = @"f:",               //默认选项的文件夹路径
        Description = "请选择一个文件夹",   //将显示在对话框标题栏中的标题
        ShowNewFolderButton = true          //是否显示对话框左下角 新建文件夹 按钮,默认为 true
    };
    
    var result = dialog.ShowDialog();
    if (result == System.Windows.Forms.DialogResult.Cancel)
    {
        //取消选
        return;
    }
    //取得文件夹路径
    string foldername = dialog.SelectedPath.Trim();
    //处理CODE
    //处理完成打开指定的文件夹
    System.Diagnostics.Process.Start("explorer.exe ", this.txtFolder.Text);
}

5、WPF实现右下角弹出消息窗口

1) 引用
using System.Windows.Threading;

2)实现

public MainWindow()
{
    InitializeComponent();
        timer = new DispatcherTimer();
    timer.Interval = TimeSpan.FromMilliseconds(300);
    timer.Tick += timer_Tick;
    this.Left = SystemParameters.WorkArea.Width - this.Width;
    this.EndTop = SystemParameters.WorkArea.Height-this.Height;
    this.Top = SystemParameters.WorkArea.Height;
}
private DispatcherTimer timer;
public double EndTop { get; set; }

void timer_Tick(object sender, EventArgs e)
{
    while (this.Top > EndTop)
    {
        this.Top -= 5;
    }
}

private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
    timer.Start();
}

6、WPF 显示 HTTP 网络图片

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http//网络图片地址");
WebResponse response = request.GetResponse();
Image img = Image.FromStream(response.GetResponseStream());

7、获得当前应用软件的版本

string version = Version(System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetEntryAssembly().Location).ProductVersion);

8、获取匿名对象(object)的属性值

Type type = obj.GetType();
PropertyInfo[] ps = type.GetProperties();
foreach (PropertyInfo p in ps)
{            
    var n = p.Name;                 //属性名
    var v = p.GetValue(obj, null);  //属性值
    var t = p.PropertyType;         //属性类型
}

9、WPF *.exe给另一个*.exe传值

第一步:在接收的*.exe的App.xaml.cs设置程序运行时触发的事件

protected override void OnStartup(StartupEventArgs e)
{
    if (e.Args != null && e.Args.Length > 0)
    {
        this.Properties["args"] = e.Args[0];
    }
    base.OnStartup(e);
}

第二步:取值

if (Application.Current.Properties["args"] != null)
{
    string args = Application.Current.Properties["args"].ToString();
}

第三步、给*.exe传值

System.Diagnostics.ProcessStartInfo exe = new System.Diagnostics.ProcessStartInfo(@"路径*.exe", "args参数");
System.Diagnostics.Process.Start(a);

10、C# zip压缩与zip解压
网络上这方面的插件比较多,如(ICSharpCode.SharpZipLib.dll),本人推荐使用DotNetZip。
1)asp.net 网络下载应用
用途:根据URL参数选择服务器的文件,供用户下载,服务器不需要保存zip压缩文件。

2)C#压缩本地文件,然后保存到指定的地址

3)根据zip文件路径解压文件

4)可以删除zip文件里指定的某个文件

5)可以跟现有的zip压缩文件添加文件

具体使用方法,网上搜索DotNetZip使用,或者是Ionic.Zip


下载地址:点击此处DotNetZip

11、C#通过文件路径获取文件名
以前用获取文件后缀名或文件名的时候,总是使用LastIndexOf、Replace等,其实不需要;
使用System.IO.Path可以完全不需要那样。
示例:

string fullPath = @"siteimage.gif";
string filename = System.IO.Path.GetFileName(fullPath);		   //文件名 image.gif
string extension = System.IO.Path.GetExtension(fullPath);	   //扩展名 .gif
string name = System.IO.Path.GetFileNameWithoutExtension(fullPath);// 没有扩展名的文件名 image
string directoryname = System.IO.Path.GetDirectoryName(fullPath);  //文件所在的目录

12、C#中 is判断类型 和 as强制转换

使用as操作符转换,
使用传统C风格的强制转型
使用is来做一个转换测试,然后再使用as操作符或者强制转
示例一:

object o = *;     
try
{
    MyType t = (MyType) o;
    if (t != null)
    {
        ////转换成功 
    }
    else
    {
        ////转换失敗 
    } 

}
catch
{
    ////异常处理
}

示例二:

if(T is System.String)

示例三:

public static T Haha<T>()
{
    Type tp = typeof(T);
    if (tp.FullName == "System.String")
    { 
	//如果String 类型
    }
    else if (tp.FullName == "System.Data.DataTable")
    {
    	//如果DataTable类型
    }
    return tp.ToString();
}

  

原文地址:https://www.cnblogs.com/sntetwt/p/5401817.html