在按钮的单击事件上,执行另一个类中的事件处理程序

2024-03-30

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

我相信创建一个绑定到另一个类中的事件处理程序方法的 ObjectDataProvider 对象,然后将所述对象绑定到 Click 事件就可以解决问题,但事实并非如此。

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

}

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

<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 方法不会返回任何可用于绑定的对象。

适合您的场景的最佳选择是使用 RelayCommand。您可以阅读有关如何实现这一目标的信息:http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute

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

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 https://www.nuget.org/packages/MvvmLight/图书馆。它已包含 RelayCommand 类的实现以及 WPF MVVM 应用程序的其他有用类。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在按钮的单击事件上,执行另一个类中的事件处理程序 的相关文章

随机推荐