Display file type icon in Silverlight
I have this situation where I need to display a bunch of files pulled from a Sharepoint site in a Silverlight application. I use a WCF service to get the data from the Sharepoint site.
So what I have is file names with their corresponding extensions. What I need to do is, display the files in the Silverlight application with their corresponding file type icon and file name. The following is the Heirarchical data template I use to display the fil开发者_C百科es in the Silverlight application.
<sdk:HierarchicalDataTemplate x:Key="FileTemplate">
<StackPanel Orientation="Vertical">
<Image x:Name="imgFile" Source="{Binding FileImage}" Height="35" ></Image>
<TextBlock x:Name="txtFile" Text="{Binding FileName}"></TextBlock>
</StackPanel>
</sdk:HierarchicalDataTemplate>
Where FileImage and FileName are strings.
PS: The reason I don't have file type and size is because I using Sharepoint web services and XMLReader to get the information about the files.
Other than the obvious solution of maintaining a internal mapping of file extensions to file icons of our own, the only other solution that I can think of is that you call back to the server to get the image for the file type.
There is a native shell function called SHGetFileInfo which can provide the file info for a file. Note that if you use the SHGFI_USEFILEATTRIBUTES flag the file does not have to physically exist on the server. Of course the problem is that you will need to use P/Invoke to actually call this function since it does not exist as a managed function in the standard .NET libraries.
The following Microsoft KB article should get you started with how to call the SHGetFileInfo from .NET. How to use the SHGetFileInfo function to get the icons that are associated with files in Visual C# .NET
Fro more detailed info on using P/Invoke to call SHGetFileInfo you can also see the following URL, it includes the definition of all the flags.
http://www.pinvoke.net/default.aspx/shell32.SHGetFileInfo
NB: Because ICO files are not directly supported by silverlight, you will need a ICO image decoder. I have not tried this code my self, but you can try the code bu Joe Stegman located on his blog.
http://blogs.msdn.com/b/jstegman/archive/2009/09/13/silverlight-ico-icon-decoder-parser.aspx
精彩评论