[英]Randomly selecting a resource file and using it to display an image
I am using WPF with VB.net code behind. 我正在使用WPF和VB.net代码。 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.
我有一个窗口,该窗口的顶部显示5个与团队相关的图片,我想随机选择一个。 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. myList中的字符串引发系统I / O异常,表明找不到文件名。 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?
因此,问题就变成了如何获取myList中字符串的名称以将其视为属性名称而不是字符串? For instance if the name of the picture is "MyTeamPic" and this is the string in myList, it throws the Exception.
例如,如果图片的名称是“ MyTeamPic”,并且这是myList中的字符串,则它将引发异常。 However, if I type in MyTeamPic it works because it references the ReadOnly Property in the Resource file and not the string.
但是,如果我键入MyTeamPic,则可以使用它,因为它引用了Resource文件中的ReadOnly属性,而不是字符串。
Or is there an easier way to do this I am not thinking of? 还是我没有想到的一种更简单的方法?
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 它正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.