silverlight - opening a file
I'm brand new to silverlight and looking for a little direction. I have a simple gallery app written with silverlight. I'd like to be able to in this example, just ftp a xml or json file to the server in a folder the app knows to look in, and have the silverlight app open the file. The file would contain a gallery category, it's title desc, images and their title, desc. I would deseralize this data to use to show the new uploaded category. I've done this sort of thing many times in wpf, but can't see to figure out the best way to handle this in silverlight. Thanks for any direction, and please let me know if I need to provide more info. I'd also u开发者_C百科pload the images, the xml or json file would contain the information to link to them.
you have to use the WebClient for this. Here is a little example:
public MainPage()
{
InitializeComponent();
GetFileContent("http://localhost/test/myjson.txt", ProcessResult, error => { throw error; });
}
private void ProcessResult(String result)
{
//Do stuff here
}
private void GetFileContent(String uri, Action<String> onData, Action<Exception> onError)
{
var wc = new WebClient();
DownloadStringCompletedEventHandler handler = null;
handler = (s, args) =>
{
wc.DownloadStringCompleted -= handler;
if(args.Error != null)
{
if(onError != null)
onError(args.Error);
return;
}
if(onData != null)
onData(args.Result);
};
wc.DownloadStringCompleted += handler;
wc.DownloadStringAsync(new Uri(uri, UriKind.Absolute)); }
You might have a look here http://msdn.microsoft.com/en-us/library/cc197955(v=vs.95).aspx, because you need a clientaccesspolicy to access the file.
Is this what you need?
BR,
TJ
精彩评论