Sunday, February 23, 2020

RegisterPropertyChangedCallback VB Example

I had a need to follow changes to an item's Visibility property although the property is not included in UWP Routed Events as defined here.

I could not find examples for RegisterPropertyChangedCallback written in VB anywhere. So here is Code, Page Code, Use and Notes.

Code First:

Public NotInheritable Class MainPage
    Inherits Page

    Private RegisterNum As Integer

    Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click

        If Border1.Visibility = Visibility.Visible Then
            Border1.Visibility = Visibility.Collapsed
        Else
            Border1.Visibility = Visibility.Visible
        End If

    End Sub

    Private Sub Border1VisibilityChanged(sender As DependencyObject, dp As DependencyProperty)
        Dim borderA As Border = sender
        Dim dpm As Visibility = borderA.Visibility

        Select Case dpm
            Case Visibility.Visible
                TextBlock1.Text = "Box is Visible"
            Case Visibility.Collapsed
                TextBlock1.Text = "Box is Collapsed"
        End Select

    End Sub

    Private Sub MainPage_Loading(sender As FrameworkElement, args As Object) Handles Me.Loading

        RegisterNum = Border1.RegisterPropertyChangedCallback(VisibilityProperty, New DependencyPropertyChangedCallback(AddressOf Border1VisibilityChanged))

    End Sub

    Private Sub Button2_Click(sender As Object, e As RoutedEventArgs) Handles Button2.Click

        Border1.UnregisterPropertyChangedCallback(VisibilityProperty, RegisterNum)

    End Sub
End Class

Page: 
<Page
    x:Class="SimpleVisibilityExample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SimpleVisibilityExample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" RequestedTheme="Dark">

    <Grid>
        <Button x:Name="Button1" Content="Show/Hide Box" Margin="50,75,0,0" VerticalAlignment="Top" Width="200"/>
        <Button x:Name="Button2" Content="Unregister" Margin="50,150,0,0" VerticalAlignment="Top" Width="200"/>
        <Border x:Name="Border1" BorderThickness="1" BorderBrush="Black" HorizontalAlignment="Left" Height="200" Margin="50,225,0,0" VerticalAlignment="Top" Width="200" Background="#FF8BD608"/>
        <TextBlock x:Name="TextBlock1" HorizontalAlignment="Left" Margin="50,450,0,0" Text="Box is Visible" TextWrapping="Wrap" VerticalAlignment="Top"/>
    </Grid>
</Page>

Use:

Put it together and run. When the "Show/Hide Box" button is pressed, the box will turn on and off. (Change visibility) Also the text block will show text saying whether the box is visible or collapsed.

Now press the "unregister" button. After that is pressed, the box still blinks on-off when the Show/Hide button is pressed, but the text box no longer indicates the state of visibility, except what was written before the code was unhooked.

Notes:

C# examples are missing the syntax necessary to make the code work in Visual Basic, because of the implicitness of C#. You can put it together like the C# example alright, but it throws weird errors like asking for parameters for something that should be simply a sub's name.

One thing not used in C# is explicitly calling the delegate DependencyPropertyChangedCallback. If you look it up online here, and it gives the C# example that never mentions the delegate in code, despite the page is being devoted to DependencyPropertyChangedCallback. Honestly ...

Another thing not used in the C# example was the AddressOf operator, of course.

This is a case where there is a necessity of alternative code examples in documentation, especially seeing how usage of the object being described by that page is necessary with those other code examples. It's kind of ridiculous. Luckily, after some searching on Google, I came across a C++ example of that usage which set me to succeeding. 

Well, time to get the stuff done that I needed this for. I put this up because of lack of docs first. I am sure I am not the only person who needs to do this.