I am working on a quick practice test page for an upcoming exam, and am noticing some odd behavior. I created a variable $type and set it to 0. I then create a card, and within that card create a radio button group and a button. For the radiogroup -OnChange, I have a switch statement that sets the $type variable based on the $Body value. Within the radiogroup -OnChange, a UDToast shows what I would expect; the $type variable 1, 2, or 3 depending on the radio selected. However, if I do a button element directly after the radio button group (same card), the $type variable goes back to 0. I have declared the $type variable at the page level, as well as trying it at the card level, but it appears that not to be passing between elements. I read through the docs on variables, and don’t see anything regarding scoping that explains this. Am I missing something stupid simple?
New-UDRadioGroup -Id "radioGroup" -Label "radioGroup" -Content {
New-UDRadio -Label "Show all answers" -Value 'all'
New-UDRadio -Label "Show incorrect answers only" -Value 'incorrect'
New-UDRadio -Label "Practice Exam, show no answers" -Value 'practice'
} -OnChange {
Switch ($Body) {
{$_ -match "all"} {$type = 1}
{$_ -match "incorrect"} {$type = 2}
{$_ -match "practice"} {$type = 3}
}
Show-UDToast -Message $type -Duration 2000
}
New-UDButton -Id "btnBegin" -Text "Begin" -OnClick {
Show-UDToast -Message $type -Duration 2000
}
Edit: I also noticed some differences in documentation. The help docs show using $Body, whereas the API docs on github show the exact same example but using $EventData. Not sure if one is ‘better’ than the other, but I confirmed both elicit the same behavior.
I did find I could do this, as sort of a kludge, so I guess it is a workaround. Still do not understand why $type doesn’t pass through.
New-UDButton -Id "btnBegin" -Text "Begin" -OnClick {
$Element = (Get-UDElement -Id "radioGroup")
Show-UDToast -Message $Element.Value -Duration 2000
}
Product: PowerShell Universal
Version: 3.9.4