How can I automatically open a file that I have added to my Windows Store App project?
I want to download a PDF file in response to a Tapped event.
I added a file to my project (Add> Existing Item), set New Action to Content and Copy to Output Directory to Copy if New
I think the code I need could be something like this:
async Task LoadTutorial()
{
await Launcher.LaunchUriAsync(new Uri("what should be here to access the output folder?"));
}
If I'm right, what do I need to pass as Uri? Otherwise, how is this achieved?
UPDATE
On a related note, to add an image to XAML using the suggested schema, I thought this would work:
<Image Source="ms-appx:///assets/axXAndSpaceLogo.jpg"></Image>
... but it isn't.
UPDATE 2
Trying to open a PDF file (which is in the root of the project, not in a subfolder):
async private void OpenTutorial()
{
IStorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
IStorageFile file = await folder.GetFileAsync("ms-appx:///PlatypusTutorial.pdf");
await Launcher.LaunchFileAsync(file);
}
... resulted in this execution exception, which was thrown on the first line above:
UPDATE 3
And with this, adapted from the link provided:
var uri = new System.Uri("ms-appx:///ClayShannonResume.pdf");
var file = Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
await Launcher.LaunchFileAsync(file);
... I am getting compile time errors:
Best overloaded method match for "Windows.System.Launcher.LaunchFileAsync (Windows.Storage.IStorageFile)" has some invalid arguments
th:
Argument 1: Cannot convert from "Windows.Foundation.IAsyncOperation" to "Windows.Storage.IStorageFile"
... on the last line.
UPDATE 4
According to page 76 "Pro Windows 8 Programming" by Lekrensky, The Netherlands, Sanders and Esheli, this should work:
<Image Source="Assets/axXAndSpaceLogo.jpg" Stretch="None"></Image>
... (IOW, "ms-appx: ///" is unnecessary) and that's more or less. In my particular case, with my (large) image, I had to do this:
<Image Source="Assets/axXAndSpaceLogo.jpg" Width="120" Height="80" HorizontalAlignment="Left"></Image>
Without adjusting the width and height, the image appears larger than the rhino and hugs the right side of the popup.
UPDATE 5
I found it works to open the PDF ("PlatypusTut.pdf" was added to the project and "Build Action" is set to "Content" and "Copy to Output Diretory" is set to "Copy if newer"):
IStorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
IStorageFile file = await folder.GetFileAsync("PlatypusTut.pdf");
bool success = await Launcher.LaunchFileAsync(file);
if (!success)
{
MessageDialog dlgDone = new MessageDialog("Unable to open the Tutorial at this time. Try again later.");
await dlgDone.ShowAsync();
}
... but I'm wondering if this will only work during development, locally. Will this work when installed on custom machines? IOW, is it enough to just pass "PlatypusTut.pdf" to GetFileAsync ()?
source to share
Use the ms-appx protocol (for example, ms-appx: ///assets/image.png) to reference items in an app package. See How to load file resources (XAML)
UPDATE:
Use GetFileFromApplicationUriAsync with ms-appx to find the file in the app bundle. If a file is marked as content and is included in the application package, it will be available after deployment, not just in the debugger. ms-appx: ///PlatypusTut.pdf will find PlatypusTut.pdf in the root directory of the application.
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///PlatypusTut.pdf"));
await Launcher.LaunchFileAsync(file);
source to share
We did it like this:
public async Task OpenResearchAsync(string path)
{
if (path.ToLower().StartsWith("http://"))
{
await Launcher.LaunchUriAsync(new Uri(path));
}
else
{
IStorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
IStorageFile file = await folder.GetFileAsync(path);
await Launcher.LaunchFileAsync(file);
}
}
source to share