日期:2014-05-20  浏览次数:21220 次

WPF如何每隔一段时间自动执行某个BUTTON?
在C#中有PerformClick();而在WPF中没有。
C# code

public void Load(object sender, System.EventArgs e)
         {
             System.Timers.Timer t = new System.Timers.Timer(1000*60);
             t.Elapsed += new System.Timers.ElapsedEventHandler(Timer_TimesUp);
             t.AutoReset = true;
             t.Enabled = true;
         }
public void Timer_TimesUp(object sender, System.Timers.ElapsedEventArgs e)
         {
             //这里怎么写才能自动执行button?    
         }



我在网上找了一段代码,但是调用这个方法不起作用。
C# code

 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();
        }



------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
using System.Windows.Forms;
using System.Timers;

namespace ConsoleApplication4
{
class Program
{
static Button button;
static System.Timers.Timer t;
static void Main(string[] args)
{
button = new Button();
button.Click += new EventHandler(button_Click);

t = new System.Timers.Timer(10000);
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
t.Interval = 2000;
t.Enabled = true;
Console.ReadLine();
}

public static void PerformClick(ButtonBase button)
{
var method = button.GetType().GetMethod("OnClick",
BindingFlags.NonPublic | BindingFlags.Instance);

if (method != null)
{
method.Invoke(button, new object[] { null });
}
button.Focus();
}
static void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
PerformClick(button);
}

static void button_Click(object sender, EventArgs e)
{
MessageBox.Show("aaa");
}
}
}