标签: wpf

  • WPF 手动触发按钮事件

    在WPF的开发中,有时需要在代码中触发按钮的点击事件,可以使用下面的代码来实现该功能:

    FirstButton.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Button.ClickEvent));

  • 反射实现 WPF 按钮的  PerformClick 

    反射实现 WPF 按钮的  PerformClick:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Controls.Primitives;
    namespace cmbc
    {
        static public class  ButtonEx
        {
            public static void PerformClick(this ButtonBase button)
            {
                var method = button.GetType().GetMethod("OnClick",
                    BindingFlags.NonPublic | BindingFlags.Instance);
                if (method != null)
                {
                    method.Invoke(button, null);
                }
                //button.Focus();
            }
        }
    }
  • WPF对某个元素进行淡入淡出

    WPF对某个元素进行淡入淡出,例如对 ImagePage 元素进行淡入淡出,代码如下:

    淡入

    daV = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(1)));
    this.ImagePage.BeginAnimation(UIElement.OpacityProperty, daV);

    淡出

    DoubleAnimation daV = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(1)));
    this.ImagePage.BeginAnimation(UIElement.OpacityProperty, daV);
  • C#在线程中使用Invoke来调用UI线程里的控件

    C#在线程中使用Invoke来调用UI线程里的控件

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace TextTool
    {
        public partial class Form1 : Form
        {
            private Thread workThread = null; //工作线程
            private List <string> fileList = new List <string>();
            public Form1()
            {
                InitializeComponent();
            }
    
            // 导入 可多选
            private void button1_Click(object sender, EventArgs e)
            {
                Stream mystream;
                OpenFileDialog openfiledialog1 = new OpenFileDialog();
                openfiledialog1.Multiselect = true;//允许同时选择多个文件
                openfiledialog1.Filter = "txt files(*.txt)|*.txt|All files(*.*)|*.*" ;
                openfiledialog1.FilterIndex = 1;
                openfiledialog1.RestoreDirectory = true;
                if (openfiledialog1.ShowDialog() == DialogResult.OK)
                {
                    if ((mystream = openfiledialog1.OpenFile()) != null)
                    {
                        fileList.Clear();
                        for (int fi = 0; fi < openfiledialog1.FileNames.Length; fi++)
                        {
                            fileList.Add(openfiledialog1.FileNames[fi]);
                        }
                        mystream.Close();
                    }
    
                    listView1.Items.Clear();
                    foreach (var filePath in fileList)
                    {
                        ListViewItem lvi = new ListViewItem(filePath);
                        listView1.Items.Add(lvi);
                    }
                }
            }
    
            // 合并处理并输出
            private void button2_Click(object sender, EventArgs e)
            {
                if (fileList.Count == 0)
                {
                    MessageBox.Show("请先添加要处理的文件" );
                    return;
                }
    
                SaveFileDialog savefiledialog1 = new SaveFileDialog();
                savefiledialog1.Filter = "txt files(*.txt)|*.txt" ;
                if (savefiledialog1.ShowDialog() == DialogResult.OK)
                {
                    button2.Text = "处理中..." ;
                    button2.Enabled = false;
    
                    workThread = new Thread (WorkThread);
                    workThread.Start(savefiledialog1.FileName);
                }
            }
    
            private int MyCompareString(string x, string y)
            {
                int pos1 = x.IndexOf("(" );
                int pos2 = x.IndexOf(")" );
                int cnt1 = Convert .ToInt32(x.Substring(pos1 + 1, pos2-pos1 -1));
    
                pos1 = y.IndexOf( "(");
                pos2 = y.IndexOf( ")");
                int cnt2 = Convert .ToInt32(y.Substring(pos1 + 1, pos2 - pos1 - 1));
     
                if (cnt1 > cnt2)
                {
                    return -1;
                }
                else if (cnt1< cnt2)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
    
            public void WorkThread(object savePath)
            {
                List<string > allLines = new List< string>();
                foreach (var filePath in fileList)
                {
                    allLines.AddRange( File.ReadAllLines(filePath));
                }
    
                int totalCount = 0;
                List<string > outputList = new List< string>();
                foreach (var line in (from t in allLines where t.Trim() != "" select t).Distinct())
                {
                    int count = (from c in allLines where c == line select c).Count();
                    totalCount += count;
                    outputList.Add( $"{line} ( {count})");
                }
                outputList.Sort(MyCompareString);
                outputList.Add( $"总数:{totalCount}" );
                File.WriteAllLines(savePath.ToString(), outputList);
    
                Invoke( new MethodInvoker (delegate ()
                {
                    button2.Text = "合并处理输出" ;
                    button2.Enabled = true;
                    MessageBox.Show("处理完成!" );
                }));
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (workThread != null && workThread.IsAlive)
                {
                    workThread.Abort();
                }
            }
        }
    }
    

    如果是WPF程序,则在线程中像下面这样操作控件

    this.Dispatcher.Invoke(new Action( delegate
    {
        lblState.Content = "正在检测域名" + fullUrl;
    }));