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

时间:2022-09-14 13:56:19

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:

我正在使用WPF和VB.net代码。我有一个窗口,顶部有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

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.

myList中的字符串抛出一个系统I / O异常,说它无法找到文件名。但是,如果我只是输入它所使用的字符串的名称,因为它设置为属性。那么问题就是如何将myList中的字符串名称视为属性名称而不是字符串?例如,如果图片的名称是“MyTeamPic”并且这是myList中的字符串,则会抛出异常。但是,如果我输入MyTeamPic它会起作用,因为它引用了Resource文件中的ReadOnly属性而不是字符串。

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

或者有一种更简单的方法来做到这一点我没想到?

1 个解决方案

#1


0  

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

它运作正常

#1


0  

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

它运作正常