How to open documents and images using launcher in windows phone 8 - c#

I am creating windows phone 8 app, I have to open docs and images using code or launcher. The problem is that those documents are not opening those are not created in MS Office, I am getting error like:
"Document has been damaged, cant open" and
"File Format doesn't recognized"
My code is here:
string file = "Test.xls";
var filerun = await ApplicationData.Current.LocalFolder.CreateFileAsync(file);
await Launcher.LaunchFileAsync(await ApplicationData.Current.LocalFolder.GetFileAsync(file));

Try this code
string uriToLaunch = #"http://www.contoso.com/SomeFile.docx";
var uri = new Uri(uriToLaunch);
async void DefaultLaunch()
{
// Set the URI’s content type
var options = new Windows.System.LauncherOptions();
options.ContentType = "application/vnd.ms-word.document.12";
// Launch the URI with the content type
var success = await Windows.System.Launcher.LaunchUriAsync(uri, options);
if (success)
{
// URI launched
}
else
{
// URI launch failed
}
}
Search for the right MIME type of your file.

Related

How can I open a .pdf file in the browser from a Xamarin UWP project?

I have a Xamarin Project where I generate a .pdf file from scratch and save it in my local storage. This works perfectly fine and can find it and open it in the disk where I saved it. However, I need to open the .pdf file immediately after creation programmatically.
I already tried different variations using Process and ProcessStartInfo but these just throw errors like "System.ComponentModel.Win32Exception: 'The system cannot find the file specified'" and "'System.PlatformNotSupportedException'".
This is basically the path I am trying to open using Process.
var p = Process.Start(#"cmd.exe", "/c start " + #"P:\\Receiving inspection\\Inspection Reports\\" + timestamp + ".pdf");
I also tried ProcessStartInfo using some variations but I'm getting the same errors all over and over.
var p = new Process();
p.StartInfo = new ProcessStartInfo(#"'P:\\Receiving inspection\\Inspection Reports\\'" + timestamp + ".pdf");
p.Start();
The better way is that use LaunchFileAsync method to open file with browser. You could create FileLauncher DependencyService to invoke uwp LaunchFileAsync method from xamarin share project.
Interface
public interface IFileLauncher
{
Task<bool> LaunchFileAsync(string uri);
}
Implementation
[assembly: Dependency(typeof(UWPFileLauncher))]
namespace App14.UWP
{
public class UWPFileLauncher : IFileLauncher
{
public async Task<bool> LaunchFileAsync(string uri)
{
var file = await Windows.Storage.StorageFile.GetFileFromPathAsync(uri);
bool success = false;
if (file != null)
{
// Set the option to show the picker
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = true;
// Launch the retrieved file
success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not
}
return success;
}
}
}
Usage
private async void Button_Clicked(object sender, EventArgs e)
{
await DependencyService.Get<IFileLauncher>().LaunchFileAsync("D:\\Key.pdf");
}
Please note if you want to access D or C disk in uwp, you need add broadFileSystemAccess capability. for more please refer this .
Update
If the UWP files are network based, not local zone based, you could use Xamarin.Essentials to open file with browser. And you must specify the privateNetworkClientServer capability in the manifest. For more please refer this link.

DisplayApplicationPicker not showing open with prompt

I'm creating a Xamarin Forms application (currently only UWP) where I want to open an PDF file from the local storage. In the UWP project I receive the file path form the Xamarin Portable Project. I use the following function to open the Open With Prompt.
public void OpenFileWith(string path)
{
Task.Run(async () =>
{
var file = await StorageFile.GetFileFromPathAsync(path);
if (file != null)
{
var options = new LauncherOptions();
options.DisplayApplicationPicker = true;
var success = await Launcher.LaunchFileAsync(file, options);
if (success)
{
//File Launched
}
else
{
//File Launch Failed
}
}
});
}
I think I've done everything correct according to the Documentation. When I don't add the LauncherOptions the file opens correctly in the default selected Application.
Is there something I'm missing. Permissions maybe? I know the documentation has a remark "This property is only implemented on Desktop devices.". I'm testing on a Desktop (Windows 10 via VMWare)
This should work:
public async Task OpenFileWithAsync(string path)
{
var file = await StorageFile.GetFileFromPathAsync(path);
if (file != null)
{
var options = new LauncherOptions();
options.DisplayApplicationPicker = true;
var success = await Launcher.LaunchFileAsync(file, options);
if (success)
{
//File Launched
}
else
{
//File Launch Failed
}
}
}
Call:
await OpenFileWithAsync(...);

C# UWP Open web Url with Microsoft Edge

I want open a URL using Microsoft Edge in my UWP. Searching, I found this code:
using System.Diagnostics;
using System.ComponentModel;
private void button_Help_Click(object sender, RoutedEventArgs e)
{
Process.Start("microsoft-edge:http://www.bing.com");
}
But it shows the following error:
The name Process do not exist in the current context
If I press Ctrl+., it only shows generate class options.
Any help is appreciated.
Process.Start is a traditional method used in .NET Framework which can't be used in UWP apps directly. To open web URI with Microsoft Edge in UWP, we can use
Launcher.LaunchUriAsync method. For example:
// The URI to launch
string uriToLaunch = #"http://www.bing.com";
// Create a Uri object from a URI string
var uri = new Uri(uriToLaunch);
// Launch the URI
async void DefaultLaunch()
{
// Launch the URI
var success = await Windows.System.Launcher.LaunchUriAsync(uri);
if (success)
{
// URI launched
}
else
{
// URI launch failed
}
}
However this will open the URI with the default web browser. To always open it with Microsoft Edge, we can use Launcher.LaunchUriAsync(Uri, LauncherOptions) method with specified LauncherOptions.TargetApplicationPackageFamilyName property. TargetApplicationPackageFamilyName property can specify the target package that should be used to launch a file or URI. For Microsoft Edge, its Package Family Name is "Microsoft.MicrosoftEdge_8wekyb3d8bbwe". Following is an example shows how to use this.
// The URI to launch
string uriToLaunch = #"http://www.bing.com";
var uri = new Uri(uriToLaunch);
async void LaunchWithEdge()
{
// Set the option to specify the target package
var options = new Windows.System.LauncherOptions();
options.TargetApplicationPackageFamilyName = "Microsoft.MicrosoftEdge_8wekyb3d8bbwe";
// Launch the URI
var success = await Windows.System.Launcher.LaunchUriAsync(uri, options);
if (success)
{
// URI launched
}
else
{
// URI launch failed
}
}
You can do it, but Microsoft Edge must be your default browser. See the code bellow
private async void launchURI_Click(object sender, RoutedEventArgs e)
{
// The URI to launch
var uriBing = new Uri(#"http://www.bing.com");
// Launch the URI
var success = await Launcher.LaunchUriAsync(uriBing);
}
I have tried this and it works for me without setting Edge as default browser:
await Launcher.LaunchUriAsync(new Uri("microsoft-edge:https://www.bing.com"));

Open PDF using C#-- Windows 8 Store Application

I want to be able to open a PDF using the native Windows Reader Application when a user clicks on a button. So far I am able to use the following code to successfully open files that end with the (.PNG) extension. However, when I let the link to open the (.PDF) file I get the following error.
The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
The file destination is correct.
Here is my code:
private async void btnLoad_Click(object sender, RoutedEventArgs e)
{
// Path to the file in the app package to launch
string imageFile = #"Data\Healthcare-Flyer.pdf";
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
if (file != null)
{
// Set the option to show the picker
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = true;
// Launch the retrieved file
bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
}
}
When you add PDF document in project, you have to change it's build action.
Right click on PDF document.
Click on properties.
Change Build Action from None to Content

Open A PDF file on button click

I want to open a pdf file in winRT app(metro style app) by clicking on a button the file should open in windows8 default reader. I tried this code, where button click method name is DefaultLaunch_click():
async void DefaultLaunch_click()
{
// Path to the file in the app package to launch
string imageFile = #"images\ret.png";
// Get the image file from the package's image directory
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
if (file != null)
{
// Set the recommended app
var options = new Windows.System.LauncherOptions();
options.PreferredApplicationPackageFamilyName = “Contoso.FileApp_8wknc82po1e”;
options.PreferredApplicationDisplayName = “Contoso File App”;
// Launch the retrieved file pass in the recommended app
// in case the user has no apps installed to handle the file
bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
}
It worked for .png file but i want for .pdf file i replaced 1.png with M.pdf(after including it in images folder) and set the build content of M.pdf to Embedded Resource , run the program but it showed error that
**The system cannot find the file specified. (Exception from HRESULT: 0x80070002)**
This code works for me after I set PDF file build action to content and copy always to output directory.
private async void Button_Click(object sender, RoutedEventArgs e)
{
string imageFile = #"images\somepdffile.pdf";
// Get the image file from the package's image directory
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
if (file != null)
{
bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
}

Categories