在按钮的click事件上,在另一个类中执行事件处理程序 [英] On a button's click event, execute event handler in another class

查看:116
本文介绍了在按钮的click事件上,在另一个类中执行事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击按钮后,我希望它执行一个事件处理程序方法,该方法是除窗口类之外的另一个类。

On a button's click, i want it to execute an event handler method that is another class apart from the window class.

我相信创建一个ObjectDataProvider对象,该对象是绑定到另一个类中的事件处理程序方法,然后将所述对象绑定到Click事件可以解决问题,但是没有成功。

I believe creating a ObjectDataProvider object which is binded to the event handler method in the other class, then binding said object to the Click event would do the trick, but it didn't.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 System.Windows.Media.Animation;
using System.Windows.Threading;
using System.Data.SqlClient;

namespace LoginNS
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class LoginWindow : Window
{

    public LoginWindow()
    {
        InitializeComponent();

    }

}

public class SQLServerClass
{
    public void ConnectSQLServer(object sender, RoutedEventArgs e)
    {
        try
        {
            SqlConnection conn = new SqlConnection("Data Source=tcp:172.16.1.71;Initial Catalog=Sample;User ID=sa;Password=hbkrko");
            conn.Open();
            MessageBox.Show("success");
        }
        catch
        {
            MessageBox.Show("db error");
        }
    }
}

}

这里是资源以及我如何使用它,这是不正确的,因为我收到一条错误消息:

Here is the resource and how i'm using it which is incorrect because i get an error message:

<ObjectDataProvider x:Key="loginFunction" ObjectType="{x:Type local:SQLServerClass}" MethodName="ConnectSQLServer"/>

<Grid DataContext="{Binding Path=LoginNS}" Width="400" Height="200">
    <Button x:Name="LoginButton" Style="{StaticResource LoginButton}" Click="{Binding Source={StaticResource loginFunction}}"/>
</Grid>

立即运行时错误:

Additional information: 'Provide value on 'System.Windows.Data.Binding' threw an exception.' Line number '24' and line position '75'.


推荐答案

ObjectDataProvider用于创建可以使用的对象实例作为绑定源。在您的情况下,ConnectSQLServer方法不会返回任何可用于绑定的对象。

ObjectDataProvider is used to create object instances that can be used as binding source. In your case ConnectSQLServer method does not return any object that can be used for binding.

对于您的方案而言,最好的选择是使用RelayCommand。您可以在 http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute

The best option for your scenario is to use RelayCommand. You can read about how to achieve this at http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute

在您的情况下,使用RelayCommand,您的SQLServerClass将是这样的

In your case, with RelayCommand your SQLServerClass will be something like this

public class SQLServerClass
{
    public SQLServerClass()
    {
        LoginCommand = new RelayCommand<object>(LoginCommandExecute, LoginCommandCanExecute);
    }
    public void ConnectSQLServer(object sender, RoutedEventArgs e)
    {
        try
        {
            SqlConnection conn = new SqlConnection("Data Source=tcp:172.16.1.71;Initial Catalog=Sample;User ID=sa;Password=hbkrko");
            conn.Open();
            MessageBox.Show("success");
        }
        catch
        {
            MessageBox.Show("db error");
        }
    }

    public ICommand LoginCommand { get; set; }

    private void LoginCommandExecute(object arg)
    {
        ConnectSQLServer(this, new RoutedEventArgs());
    }

    private bool LoginCommandCanExecute(object arg)
    {
        return true;
    }
}

您的XAML

<Window.Resources>
    <ObjectDataProvider x:Key="loginFunction" ObjectType="{x:Type local:SQLServerClass}"/>
</Window.Resources>
<Grid>


    <Grid  Width="400" Height="200">
        <Button x:Name="LoginButton"  Command="{Binding Path=LoginCommand, Source={StaticResource loginFunction}}"/>
    </Grid>
</Grid>

请注意,您可以使用 MvvmLight 库。它已经包含了RelayCommand类的实现以及对WPF MVVM应用程序的其他有用的类。

Note that you can use the MvvmLight library. It already contains an implementation of the RelayCommand class and other useful classes for WPF MVVM application.

这篇关于在按钮的click事件上,在另一个类中执行事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆