Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在我的代码中触发按钮点击?_C#_Windows 8 - Fatal编程技术网

C# 如何在我的代码中触发按钮点击?

C# 如何在我的代码中触发按钮点击?,c#,windows-8,C#,Windows 8,如何在代码中直接触发按钮单击 我有这样一个代码: namespace App1 { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class MainPage : Page { public MainPage

如何在代码中直接触发按钮单击

我有这样一个代码:

namespace App1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // fire a click ebent von the "Button" MyBtn
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // ...
        }
    }
}
名称空间App1
{
/// 
///可以单独使用或在框架内导航到的空页。
/// 
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
//点击“按钮”MyBtn
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
// ...
}
}
}
是否可以通过按钮
MyBtn
触发单击事件?如果是,怎么做

我知道我可以调用
按钮\u Click
方法,但我想通过按钮调用它


类似于:
MyBtn.FireClickEvent()

您可以删除指向事件的方法链接(单击):

MyBtn.Click -= new EventHandler(Button_Click);
MyBtn.PerformClick();
并添加其他方法:

MyBtn.Click += new EventHandler(FireClickEvent);
因此,当您单击按钮时,将调用方法“FireClickEvent”,而不是“button\u click”

单击代码:

MyBtn.Click -= new EventHandler(Button_Click);
MyBtn.PerformClick();

您可以从代码中触发
按钮\u单击
事件,如下所示:

MyBtn.PerformClick();
你也可以试试这个:

MyBtn.Click(new object(), new EventArgs());

我用这个小小的扩展从外面触发事件。 它是一种泛型(可以在任何类中引发任何事件),但我把它作为一种扩展方法,仅用于控制,因为这是一种不好的做法,我只在其他所有操作都失败时才使用它。玩得开心

public static class ControlExtensions
{

    public static void SimulateClick(this Control c)
    {
        c.RaiseEvent("Click", EventArgs.Empty);
    }

    public static void RaiseEvent(this Control c, string eventName, EventArgs e)
    {

        // TO simulate the delegate invocation we obtain it's invocation list
        // and walk it, invoking each item in the list
        Type t = c.GetType();
        FieldInfo fi = t.GetField(eventName, BindingFlags.NonPublic | BindingFlags.Instance);
        MulticastDelegate d = fi.GetValue(c) as MulticastDelegate;
        Delegate[] list = d.GetInvocationList();

        // It is important to cast each member to an appropriate delegate type
        // For example for the KeyDown event we would replace EventHandler
        // with KeyEvenHandler and new EventArgs() with new KeyHandlerEventArgs()
        foreach (EventHandler del in list)
        {
            del.Invoke(c, e);
        }

    }

}

我真的不明白这个要求,如果你需要执行button_click中的代码,请用一个单独的方法提取它,并从button_click事件处理程序调用它,无论你在哪里需要它,这都是一个很长的描述,我为什么需要这样做。如果不可能发布或告诉我怎么做。过度(和不必要的)复杂设计的一个明显迹象是当有人说抱歉时,太复杂了,无法解释我为什么需要做这件事…是的,你可以-你不应该。两个原因:(1)通过单击的内容指定应该发生的事情只会导致代码不太清晰,并且(稍微)更难更改UI。(2) PerformClick只会引发Click事件,因此是调用该方法的低效方法。Win Store应用程序中的按钮没有此方法。Win Store应用程序中的按钮没有此方法。@gurehbgui:尝试更新的代码。这行代码
MyBtn.Click(new object(),new EventArgs())在很多方面都是错误的。1.它不触发事件,只调用方法。2.不要创建新对象,发送者应该始终是对象本身(MyBtn)3。不要使用新的EventArgs,而是使用
EventArgs.Empty
4。如果您确实不想触发事件,但执行了button从代码中触发的相同方法,那么应该让click eventhandler调用另一个方法。从您自己的代码中只需调用相同的方法。