Events of UserControls embeded in a Page not received

oligo92 1 Reputation point
2022-06-20T12:33:34.607+00:00

Hello. I'm newbie ,coming from VB.NET, in WPF and here is my question:

I created a UserControl. It is just a pictured button and a Text beneath. Picture and Text are binded:

<UserControl x:Class="oRadio"  
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:musicplayerwpf="clr-namespace:MusicPlayerWPF"   
             mc:Ignorable="d"             
             d:DesignHeight="100" d:DesignWidth="100">  
    <Button>  
        <Grid>  
            <Grid.RowDefinitions>  
                <RowDefinition />  
                <RowDefinition Height="15"/>  
            </Grid.RowDefinitions>  
            <Image Grid.Row="0" MouseUp="RadioClicked" Stretch="Fill" Source="{Binding Logo}"/>  
            <TextBlock Grid.Row="1" Text="{Binding Titre}" TextAlignment="Center" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>  
        </Grid>  
    </Button>                 
</UserControl>  

In the code behind:

Public Class oRadio
Public Event Play(Station As oRadio, e As RoutedEventArgs)
Public Sub New()
InitializeComponent()
DataContext = Me
End Sub
Public Property Titre As String
Public Property Url As String
Public Property Genre As String
Public Property Logo As ImageSource
Public WriteOnly Property LogoLink As String
Set(value As String)
Dim bitmap As New BitmapImage
If value.Length > 0 Then
bitmap.BeginInit()
bitmap.UriSource = New Uri(value, UriKind.Absolute)
bitmap.EndInit()
Else
bitmap = CType(Me.Resources("RADIO"), BitmapImage)
End If
Logo = bitmap
End Set
End Property
Public Sub RadioClicked()
'binded in the xaml code
RaiseEvent Play(Me, New RoutedEventArgs)
End Sub
End Class

I have a Page as follows:

<Page x:Class="PageRadio"  
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
      xmlns:local="clr-namespace:MusicPlayerWPF"  
      mc:Ignorable="d"   
      d:DesignHeight="455"  
      ScrollViewer.VerticalScrollBarVisibility="Visible"  
      Title="PageRadio" Width="675">  
 </Page>  

In its code behind:
Class PageRadio
Public Event Play(sURL As String)

Public Sub New()  

    ' This call is required by the designer.  
    InitializeComponent()  

' -----------------cRadios is a dictionnary of cRadio -----------------
' -----------------cRadio is a class
Radios = New cRadios("radios.xml")
LoadRadios()
End Sub
Public Sub LoadRadios()

    For Each r As KeyValuePair(Of String, cRadio) In Radios  
        Dim o As New oRadio With {.Titre = r.Key, .LogoLink = r.Value.Logo, .Url = r.Value.Url, .Genre = r.Value.Genre}  
        AddHandler o.Play, AddressOf ElementClicked  

        Me.AddLogicalChild(o)  

    Next  
End Sub  

Private Sub ElementClicked(sender As oRadio, e As RoutedEventArgs)  
    RaiseEvent Play(sender.Url)  
End Sub  

The page is correctly loaded. I can see n Pictured button with its text beneath
When I click, I see the sub RadioClicked() is reached but not the sub ElementClicked()

Any idea?

Thanks

Olivier

XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
830 questions
{count} votes

2 answers

Sort by: Most helpful
  1. oligo92 1 Reputation point
    2022-06-21T11:59:46.327+00:00

    The classes cRadios:

    Imports System.Xml  
      
    Public Class cRadio  
        Public Url As String  
        Public Genre As String  
        Public Logo As String  
    End Class  
    
    Public Class cRadios  
        Inherits Dictionary(Of String, cRadio)  
        Dim XmlDoc As XmlDocument  
        Public Sub New(XMLFilename As String)  
            XmlDoc = New XmlDocument  
      
            Try  
                XmlDoc.Load(XMLFilename)  
                ReadRadios()  
            Catch ex As XmlException  
                Throw New Exception("XML initialisation file is badly formated or corrupted:" & vbCrLf & ex.Message)  
            Catch ex As Exception  
                Throw New Exception("Error loading XML " & XMLFilename & vbCrLf & ex.Message)  
            End Try  
        End Sub  
      
        Private Sub ReadRadios()  
            Dim xChapter As XmlNodeList  
            Dim index As Integer = 0  
            Dim r As cRadio  
      
            xChapter = XmlDoc.DocumentElement.ChildNodes  
            Try  
                For Each cell As XmlElement In xChapter                          
      
                    r = New cRadio With {.Genre = cell.Attributes("genre").Value}  
                    If cell.HasAttribute("genre") Then  
                        r.Genre = cell.Attributes("genre").Value  
                    End If  
                    If cell.HasAttribute("url") Then  
                        r.Url = cell.Attributes("url").Value  
                    End If  
                    If cell.HasAttribute("icon") Then  
                        r.Logo = cell.Attributes("icon").Value  
                    End If  
      
      
                    Me.Add(cell.Attributes("name").Value, r)  
                Next  
            Catch ex As NullReferenceException  
                MsgBox("Probably, some attribute data are missing",, "Error in ReadRadios")  
                Throw New Exception("Missing attribute(s) in XML ")  
            Catch ex As Exception  
                MsgBox(ex.Message,, "Error in ReadRanging ")  
            End Try  
      
        End Sub  
      
    End Class  
    
    0 comments No comments

  2. oligo92 1 Reputation point
    2022-06-22T12:24:08.657+00:00

    Shame on me... Actually this code works. I messed up using a wrong archive of code...

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.