Create custom wpf event

…衆ロ難τιáo~ 提交于 2019-12-04 10:11:55

问题


i've created an UserControl for Database connection where user input Username and Password for a connection. This UserControl is in a MainWindow.xaml

Now, in code behind of my UserControl i create a MSSQL connection. If login Successfully, i want to Raise a custom event to expose in MainWindow.

For example in MyUserControl.xaml.cs

try
{

    using (SqlConnection sqlConn = new SqlConnection(connection))
    {
        sqlConn.Open();
        MessageBox.Show("Connessione Riuscita!", "Connessione a " + TextIP.Text, MessageBoxButton.OK, MessageBoxImage.Information);
        RaiseMyEvent();
        sqlConn.Close();                 
    }
}
catch (SqlException ex)
{
    MessageBox.Show("Connessione Fallita: " + ex.Message, "Connessione a " + TextIP.Text, MessageBoxButton.OK, MessageBoxImage.Error);
}

and In MainWindow.xaml i want to use mypersonalized event:

<Window x:Class="XLogin.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:XLogin" WindowStartupLocation="CenterScreen">
    <Grid>
        <local:DBLogin x:Name="DBLoginFrame" MyPersonalizedUCEvent="DBLoginFrame_MyPersonalizedUCEvent"/>
    </Grid>
</Window>

I need this for multiple type connection (MSSQL, Oracle, MySql etc).

How to get this?


回答1:


First you should define a delegate and then use that delegate to define that event.

In your MyUserControl.xaml.cs file add the following

Option 1

    public delegate void MyPersonalizedUCEventHandler(string sampleParam);

    public event MyPersonalizedUCEventHandler MyPersonalizedUCEvent;

    public void RaiseMyEvent()
    {
        // Your logic
        if (MyPersonalizedUCEvent != null)
        {
            MyPersonalizedUCEvent("sample parameter");
        }
    }

And that's it. You have defined your event.

Option 2

    public event Action<String> MyPersonalizedUCEvent;

    public void RaiseMyEvent()
    {
        // Your logic
        if (MyPersonalizedUCEvent != null)
        {
            MyPersonalizedUCEvent("sample parameter");
        }
    }

More about the Action delegate can be found in this link.

Note:

In many cases if events are not used properly they can cause memory leaks. Just make sure that you have written code to remove the registered event handlers as shown below.

        MyPersonalizedUCEvent -= MyPersonalizedUCEventHandler;



回答2:


First create a public event in your class :

public event EventHandler<MyEventArgs> SomethingChanged;

NB MyEventArgs is the type of object that will be passed with the event to subscribers. For this example it could be like this :

public class MyEventArgs{
    public String Prop1 {get; set;}
}

Next fire it as-is in your class :

SomethingChanged?.Invoke(this, new MyEventArgs() { Prop1="test" });

Finnally handle it like this :

private void OnSomethingChanged(object sender, MyEventArgs e)
{
    //TODO
}

NB You need to subscribe to the event in order to enter in the OnSometingChanged method. Subscribe like this :

myClass.SomethingChanged+=OnSomethingChanged;

Where myClass is an instance of the class where you define SomethingChanged



来源:https://stackoverflow.com/questions/14500559/create-custom-wpf-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!