To open a file from client side, we need to use a Systems.Windows.Controls.OpenFileDialog object. I found the OpenFileDialog class in SL 2.0 quite interesting. Compare to WinForms counterpart, it has fewer properties and some property names are renamed too but still quite straightforward.
The surprising part is the return type of SelectedFile property. Instead of return the file path of string type, it returns System.Windows.Controls.FileDialogFileInfo object.
FileDialogFileInfo has a Name property, but it returns the file name only, not the full path of the file. I guess there is no way to retrieve the full path.
To read the content of the selected file, I need to use either:
FileDialogFileInfo.OpenText() which returns a System.IO.StreamReader object to read text,
or,
FileDialogFileInfo.OpenRead() which returns System.IO.Stream to read non-text content.
Example (select an image file and display the image on the button):
Dim dlg As New OpenFileDialog
If dlg.ShowDialog() <> DialogResult.OK Then Return
Dim img As New Image
Dim bitmap As New BitmapImage
bitmap.SetSource(dlg.SelectedFile.OpenRead)
img.Source = bitmap
aButton.Content = img
Saturday, April 26, 2008
Open file dialog in Silverlight 2.0
Labels:
Silverlight
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment