Invoke_event - lucyberryhub/WPF.Tutorial GitHub Wiki

πŸ’πŸ“ Invoke Event to Close the Cherry Popup! πŸ“πŸ’

Welcome, sweet cherry berry! πŸ’πŸ“ Let’s have fun today and learn how to close a cute popup using an event called CloseRequested πŸ’. I’ll walk you through everything step by step, so you can make your popup super cute and super functional like Lucy Berry! 🌸

Let’s start the sweet journey! πŸ“πŸ’


πŸ’ Step 1: Set Up Your Cherry Popup Window πŸ“

First, let’s prepare your adorable CherryPopup πŸ₯° where everything is going to happen! We need to create an event that will help us close the popup when it’s requested. This event is called CloseRequested! πŸ’

Here’s how you do it in the CherryPopup window class πŸ“:

public class CherryPopup
{
    // πŸ’ Event declaration for the CloseRequested event πŸ“
    public event EventHandler CloseRequested;

    // πŸ“ Method to trigger the CloseRequested event πŸ’
    public void RequestClose()
    {
        // πŸ’ Invoke the event to notify the parent that the popup wants to close! πŸ“
        CloseRequested?.Invoke(this, EventArgs.Empty);
    }
}

πŸ“ Step 2: Create the CherryParent Class πŸ’

Next, we have to listen for this CloseRequested event in the CherryParent class πŸ’ (the window or control that has the popup). Here’s how you can do it:

public class CherryParent
{
    // πŸ“ The cherry popup that we are going to close πŸ’
    private CherryPopup _cherryPopup = new CherryPopup();

    // πŸ’ Subscribe to the CloseRequested event πŸ“
    public CherryParent()
    {
        _cherryPopup.CloseRequested += (s, e) =>
        {
            // πŸ’ When the CloseRequested event is triggered, we close the popup! πŸ“
            CloseCherryPopup();
        };
    }

    // πŸ“ Method to close the popup window πŸ’
    private void CloseCherryPopup()
    {
        // πŸ’ Here’s where the magic happens! We close the cherry popup! πŸ“
        _cherryPopup.IsOpen = false; // πŸ“ Close the popup by setting IsOpen to false πŸ’
        Console.WriteLine("πŸ’ Pop, the cherry popup has been closed! πŸ“");
    }
}

πŸ’ Step 3: Trigger the Event to Close the Popup πŸ“

Now, when you want to close the CherryPopup from anywhere, just call the RequestClose method! πŸ’ It’s like sending a close me! message πŸ“. Here’s how you can do it:

public class SomeOtherClass
{
    public void DoSomething()
    {
        // πŸ’ Trigger the close request! πŸ“
        _cherryPopup.RequestClose(); // πŸ’ This will trigger the CloseRequested event! πŸ“
    }
}

πŸ“ Step 4: Adding Some Sweetness – Display the Cherry Popup πŸ’

We don’t just want to close the popup; let’s make it sweet and pretty! Here’s how you can visualize and open the popup πŸ’πŸ“:

public class CherryPopup
{
    public bool IsOpen { get; set; } = true; // πŸ’ Whether the popup is open or not πŸ“

    public void Open()
    {
        // πŸ’ Display the popup to the user πŸ“
        Console.WriteLine("πŸ“ The cherry popup is now open! πŸ’");
    }

    public void Close()
    {
        // πŸ’ Close the popup and say goodbye πŸ“
        IsOpen = false;
        Console.WriteLine("πŸ’ The cherry popup is now closed! πŸ“");
    }
}

πŸ“ Step 5: The Sweet Cherry Closing in Action πŸ’

Now, when you call RequestClose() in the CherryPopup, it will notify the CherryParent to close it! πŸ’

Here’s what the whole flow will look like when you run it πŸ“:

public class CherryPopupExample
{
    public void RunExample()
    {
        // πŸ’ Create a new CherryParent and CherryPopup πŸ“
        var cherryParent = new CherryParent();
        var cherryPopup = new CherryPopup();

        // πŸ’ Open the popup and show a sweet message! πŸ“
        cherryPopup.Open();

        // πŸ“ Now, let’s trigger the close request from somewhere πŸ’
        cherryPopup.RequestClose(); // πŸ’ This will close the popup because of the event! πŸ“
    }
}

πŸ’ Step 6: Test it Out – It's Cherry Time! πŸ“

Finally, let’s test everything together and see the magic happen! When you call RequestClose(), the event will be triggered and the popup will close. πŸ’


πŸ“ Summary – Sweet, Cute, and Functional! πŸ’

Yay! Now you know how to close the cherry popup using the CloseRequested event πŸ’. Here’s the recap of the steps:

  1. πŸ’ CherryPopup has the CloseRequested event that’s triggered when we want to close it.
  2. πŸ“ The CherryParent listens for the CloseRequested event and closes the popup.
  3. πŸ’ When you want to close the popup, call the RequestClose method to trigger the event.

And there you go! πŸŽ€ Your popup is as cute as a cherry πŸ’ and works just the way you want! πŸ“