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"));
Related
I have an wpf app that I want to use to open another wpf app.
Call them image viewer (Gallery) and image storage (AppToAppCommunication).
Image storage app shows a list of available images.
Then, on ListBox selection changed I have this code:
private async void lbImages_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var options = new LauncherOptions();
options.ContentType = "image/jpg";
var filePath = Environment.CurrentDirectory + "\\Images\\" +
((ListBoxItem)((ListBox)sender).SelectedItem).Content;
var file = await StorageFile.GetFileFromPathAsync(filePath);
var token = SharedStorageAccessManager.AddFile(file);
ValueSet inputData = new ValueSet();
inputData.Add("Token", token);
Uri uri = new Uri("myprotocol:");
//await Launcher.LaunchUriAsync(uri); // #works
//await Launcher.LaunchUriAsync(uri, options, inputData); // #doesn't
}
Now, when I use the line at #works, it works, with no image shown, as expected.
But, when instead I use line at #desn't, it gives me this error:
Now I tried clicking OK on the popup and running a new VS instance an waiting for it to load, but it never does.
I've tried running both applications from the same solution and I can get to a breakpoint in override void OnActivated in image viewer app (Gallery) when using the #works line in image storage app (AppToAppCommunication), but I can't get to that same breakpoint when using the #doesn't line.
I don't know what the problem is and I am not able to debug it.
How could I go about finding a way to debug this properly?
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.
I'm creating a simple news feed, it i want it can open the browser to show the details of the news, but i don't know how to close the browser , here is the code of how I open the browser,anyone can teach me how to off the browser using uwp? '
public async void test()
{
RootObject mynews = await NewsProxy.GetNews();
string website = mynews.articles[i].url;
var uriWeb = new Uri(websites);
var success = await Windows.System.Launcher.LaunchUriAsync(uriWeb);
if (success)
{
//Uri launched
}
else
{
// uri launch failed
}
}
var success = await Windows.System.Launcher.LaunchUriAsync(uriWeb);
Your code is actually telling the OS to Launch the given url and the OS in turn launches the default browser with the given URL. So you are not actually launching the browser.
In order to have full control over the browser behavior you can implement your own WebView and then use the url to navigate your WebView.
webView1.Navigate("http://www.contoso.com");
(MSDN Documentation for WebView)
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.
I am looking for a way to implement Internet explorer - a desktop version in my Winodws 8 app . This is caused by my c# application- i need to run javascript page but its impossible in metro version of IE. Is there any method to do this?
I `ll describe my problem: when i want to load html of DOM page(rendered by javascript) i cant because javascript can run (I use WebView). I tried to open this page using Desktop version and it worked perfectly. Unfortunately Metro IE10 cant show the content and my WebView too.
Thanks for help.
You can sure inject in the c# webview a javascript file:
This is the action for a button:
private async void Lamp_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("ms-appx:///js/injected.js");
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);
string text = await FileIO.ReadTextAsync(file); //reading content as string
var script = "(function(){" + text + "})()";
}
try
{
var result = wv.InvokeScript("eval", new string[] { script });
}
catch (Exception) { }
wv is you id name for the webview element in the xaml