Search code examples
wpfvb.netresourcesresx

Randomly selecting a resource file and using it to display an image


I am using WPF with VB.net code behind. I have a window that has an area for 5 team related pictures at the top to display, which I want to be chosen randomly. These pictures are included in the project as resource files, and I have the following code to grab a list of the resource file names associated with each team, which works:

Private function LoadPics(byval TeamID) As List(Of string)
    Dim dictEntry as new DictionaryEntry
    Dim runTimeResourceSet as Object
    Dim teamNick as String=NewGame.TeamDT.Rows(TeamID).Item("TeamNickname")

    runTimeResourceSet=My.Resources.ResourceManager.GetResourceSet(CultureInfo.InvariantCulture,False,True)

    for each dictEntry In runTimeResourceSet          
        if dictEntry.Key.ToString().StartsWith(teamNick) then
            myList.Add(dictEntry.key)
        end if              
    Next dictEntry

    return myList
End function

This returns a list containing the name for every picture for that team.

However this code is where I am getting stuck:

Sub New(TeamID As integer)
    Dim filepath = "pack://application:,,,/Project Files/"
    dim myNum as integer

    ' This call is required by the designer.
    InitializeComponent()   
    ' Add any initialization after the InitializeComponent() call.
    DataContext=MyVM   
    myTeam = TeamID   
    LoadPics(myteam)

    myNum=myRand.NextUInt(0,myList.Count-1)
    MyVM.Image1=New BitmapImage(New Uri(filepath+myList(mynum),UriKind.RelativeOrAbsolute)))
    myList.Removeat(myNum)    
    myNum=myRand.NextUInt(0,myList.Count-1)  
    MyVM.Image2=New BitmapImage(New Uri(filepath+myList(myNum),UriKind.RelativeOrAbsolute))
    myList.RemoveAt(myNum)
    myNum=myRand.NextUInt(0,myList.Count-1)
    MyVM.Image3=New BitmapImage(New Uri(filepath+mylist(myNum),UriKind.RelativeOrAbsolute))
    myList.RemoveAt(myNum)
    myNum=myRand.NextUInt(0,myList.Count-1)
    MyVM.Image4=New BitmapImage(New Uri(filepath+myList(myNum),UriKind.RelativeOrAbsolute)) 
    myList.RemoveAt(myNum)
    myNum=myRand.NextUInt(0,myList.Count-1)
    MyVM.Image5=New BitmapImage(New Uri(filepath+myList(myNum),UriKind.RelativeOrAbsolute))

The string from myList throws a System I/O Exception saying it can't find the file name. However, if I simply type in the name of the string it works because its set to a property. So the question becomes how do I get the name of the string in myList to be viewed as the property name and not a string? For instance if the name of the picture is "MyTeamPic" and this is the string in myList, it throws the Exception. However, if I type in MyTeamPic it works because it references the ReadOnly Property in the Resource file and not the string.

Or is there an easier way to do this I am not thinking of?


Solution

  • OK I figured it out---

    I replaced

    MyVM.Image1=New BitmapImage(New Uri(filepath+myList(mynum),UriKind.RelativeOrAbsolute)))
    

    With

    MyVM.Image1=New BitmapImage(New Uri(filepath+ResourceManager.GetObject(myList(myNum),CultureInfo.InvariantCulture).ToString(),UriKind.RelativeOrAbsolute))
    

    And it works properly