Skip to content

Instantly share code, notes, and snippets.

@valeryjacobs
Created April 15, 2015 14:14
Show Gist options
  • Save valeryjacobs/4320dbc3131d6588ba4e to your computer and use it in GitHub Desktop.
Save valeryjacobs/4320dbc3131d6588ba4e to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.Web;
namespace Rememba.Windows.Extensions
{
public sealed class StreamUriWinRTResolver : IUriToStreamResolver
{
/// <summary>
/// The entry point for resolving a Uri to a stream.
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
{
if (uri == null)
{
throw new Exception();
}
string path = uri.AbsolutePath;
if (System.Diagnostics.Debugger.IsAttached)
{
System.Diagnostics.Debug.WriteLine(string.Format("Stream Requested: {0}", uri.ToString()));
}
// Because of the signature of the this method, it can't use await, so we
// call into a seperate helper method that can use the C# await pattern.
return getContent(path).AsAsyncOperation();
}
/// <summary>
/// Helper that cracks the path and resolves the Uri
/// Uses the C# await pattern to coordinate async operations
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private async Task<IInputStream> getContent(string path)
{
var uri = new System.Uri("ms-appx:///html" + path);
try
{
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);
var stream = await file.OpenAsync(FileAccessMode.Read);
return stream;
}
catch(Exception ex)
{
}
uri = new System.Uri("ms-appx:///html/ContentEditor/dummy.png");
StorageFile dummyFile = await StorageFile.GetFileFromApplicationUriAsync(uri);
var dummyStream = await dummyFile.OpenAsync(FileAccessMode.Read);
return dummyStream;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment