Thursday 12 April 2018

Window LifeTime Events in PowerShell


       In this post I’ll explain the lifetime of your window from the moment it is initialized and the moment it's destroyed. I hope this post will help you achieve easily what you seek and could be time-saving in some case.

What I’m going talk about here?

«To find it you must look beyond what you see» Rafiki
Everything has its lifecycle 😊. So does your window in PowerShell. It follows the same rule as in C# when you load it in PowerShell. It’s well explained here.





Why is it important?

Well, in some case you may want your script to do something at one of those events but doesn’t know if it is feasible or not.

For example, when you want your script to do something only when your window content is fully rendered, then you will call the task at “ContentRendered.” Another example: if you want your script to execute another script when your window is being closed then you will call it at “Closing”.



So let’s take a look at each one of these step and see how we call them in PowerShell.

Window Lifetime events:

- SourceInitialized:
Your window is not yet visible when this event is fired. It’s like a pre-initialization phase like “we’re good to go, shall we proceed?”



$Form.Add_SourceInitialized({
    Write-Host "0 - Window is Initialized"   
})

- Activated:
This is called every time when your window got the focus. I recommend not using a time-consuming function/script here …



$Form.Add_Activated({
    Write-Host "*** - Oh boy! I'm on the stage"
})

- Loaded:
Your entire window is ready. If you want to do something before elements are rendered do it here. For example, if I want to apply something specific to my window I will do it here.

$Form.Add_Loaded({
    Write-Host "1 - Only window is Loaded"
})

- ContentRendered:
There is nothing more to add here. It’s fired when all contents are rendered. Don’t do visual modifications from here such as moving content/window. Do it before this event occurs.

$Form.Add_ContentRendered({
    Write-Host "2 - Content is rendered"   
})

- Closing:
This is triggered when the user attempts to close the window via “X” button or close event from the script. This event can be canceled! I discovered this when I wrote this article ^_^ by setting CancelEventArgs.Cancel to true.

Why is it important you would say?If you want to prompt a message like: “Hey! Do you really want to close the app?"Or something like “Dude! You haven’t saved your work? You shouldn’t close for now! ”


$Form.Add_Closing({
    [System.Object]$sender = $args[0]
    [System.ComponentModel.CancelEventArgs]$e = $args[1]
    $message = "Do you really wish to close me :( ? "
    $result = [MahApps.Metro.Controls.Dialogs.DialogManager]::ShowModalMessageExternal(`
    $Form,"Window", $message ,$okAndCancel)

    If ($result -eq "Negative"){
        #handle with care otherwise you won't be able to close the window.
        $e.Cancel = $true
        Write-Host "3 - User canceled Closing."
    }
    Else{
        Write-Host "3 - Window is Closing."  
    }
})

Then the app will not be closed. Very useful!

- Deactivated:
This happens if window loses focus or the user switches to another application. The opposite of activated …
$Form.Add_Deactivated({
    Write-Host "*** - Mama! I've lost focus X)"
})

- Closed:
The window is closed but window elements are accessible in your script (if you haven't closed it too).



$Form.Add_Closed({
    Write-Host "4 - Form closed"
})

So that’s it! Thanks for reading!
The source code is available here.

Share:

0 commentaires:

Post a Comment

Popular Posts

Join us on Facebook

STATS

Contact Form

Name

Email *

Message *