随机选择资源文件并使用它来显示图像

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

我正在使用带有 VB.net 代码的 WPF。我有一个 window,顶部有一个区域可以显示 5 张与团队相关的图片,我想随机选择这些图片。这些图片作为资源文件包含在项目中,我有以下代码来获取与每个团队关联的资源文件名的列表,这是有效的:

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

此 returns 包含该团队每张照片名称的列表。

然而这段代码是我卡住的地方:

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))

来自 myList 的字符串抛出一个系统 I/O 异常,表示找不到文件名。但是,如果我简单地输入字符串的名称,它就可以工作,因为它设置为 属性。所以问题变成了如何让 myList 中的字符串名称被视为 属性 名称而不是字符串?例如,如果图片的名称是 "MyTeamPic" 并且这是 myList 中的字符串,它会抛出异常。但是,如果我输入 MyTeamPic,它会起作用,因为它引用资源文件中的 ReadOnly 属性 而不是字符串。

或者有没有我没有想到的更简单的方法?

好的我想通了---

我换了

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

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

并且它工作正常