How to call the default browser in the Hololens app - c#

I want to create an app that calls the Hololens default browser. I used the following code, but whenever I call this function, the Application will crash. If this code is not suitable for UWP, how can I call HoloLens' default browser in an app?
The version of the software I am using is:
1.Unity 2018 3.11f
2.Mixed Reality Toolkit v2.0.0 RC1
3.Visual Studio 2017
public void OpenAnlagenWiKi_URL()
{
string tempUrl = string.Format("{0}", AnlagenWiKi_Link.text);
Application.OpenURL(tempUrl);
}
I hope to successfully call HoloLens' default browser in the app.

It's a bit tricky. Any Hololens application is actually UWP process.
First of all, you need to switch it into the background mode by calling AppResourceGroupInfo.StartSuspendAsync. The details are here.
When the app would go into background, you should call open process
System.Diagnostics.Process.Start("http://google.com");

Related

WinRT & UWP WebView localhost url doesn't fire when app is packaged

Boy I'm really struggling with this one!
So I have a WinRT Metro application that has an HTML webpage embedded into a webview within the application. In the HTML page there is a div with an href to a localhost url. I'm using this localhost call to communicate with a .NET desktop application that is listening for this url on a localhost port.
When I build the application (as debug or release) in Visual Studio (2015, update 1 or 2), the application works as expected. I can click on the div, the url is fired, and the communication is successful.
However, when I package the application and sideload it on to my machine, the functionality does not work as expected. I can click on the div...but the url is never fired.
HTML Url Example ([...] = code removed):
<a href="http://localhost:8123/?Api [...] >Click here</a>
Codebehind Navigating to HTML Example:
this.webView.Navigate(new Uri("ms-appdata:///local/index.html"));
XAML Webview Instantiation:
<WebView x:Name="webView" Grid.Row="1" ScriptNotify="webView_ScriptNotify" Grid.Column="1" Visibility="Collapsed"/>
Here are the things I've tried:
I created a WinRT app that only contained the webview and the embedded HTML page. Functionality works when built, but not when packaged.
I created a UWP app that only contained the webview and the embedded HTML page. Functionality works when built, but not when packaged.
I tried packaging the app outside of Visual Studio using the command line as described here. The functionality does not work.
I tried using different versions of Visual Studio 2015. Same results.
I tried building/packaging on different machines. Same results.
I tried to navigate to google instead of the localhost. Works.
I tried to navigate to another html file instead of the localhost. Works.
Naturally, I tried the obvious things too like cleaning, changing package name, changing settings in the Visual Studio App Packager, reboots, removing references, etc.
Has anyone experienced this before? It seems to be some sort of bug with the packaging process. Any suggestions/tips/answers are welcome! Thanks!
And just in case you're wondering, this application is intended for enterprise use only within the company I work for and will not be uploaded to the Windows Store.
Normally, you can't open a loopback connection in a UWP store app. Full stop. If you're side loading, or in an enterprise environment, there is a workaround:
https://msdn.microsoft.com/en-us/library/windows/apps/dn640582.aspx
I had a problem similar enough to this for it to be worth sharing my experience.
My app is a Universal app with a WebView control. The WebView source is a file in the app package. I define my WebView in the XAML as follows.
<WebView x:Name="myWebview" Source="ms-appx-web:///Assets/www/mylocalwebpage.html"/>
In Debug mode this works perfectly. In the first run in Release mode it does not load the page. In all subsequent runs of the Release mode app it works perfectly. It's a reproducible and incredibly frustrating bug.
My workaround is simple but effective. I use a timer to set the Source of the webview after one second. I add the following line below this.InitializeComponent();
// this worksaround a bug: on first launch in release mode the webview doesn't load
loadWebviewTimer = new Timer(loadWebview, null, 1000, Timeout.Infinite);
And then another
private async void loadWebview(object state)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
myWebview.Navigate(new Uri("ms-appx-web:///www/mylocalwebpage.html"));
});
}
It's not a pretty fix, but it works. Of interest, mylocalwebpage.html has quite a lot of javascript in it; I suspect that this is related to the problem.

Check if Windows.System.Launcher.LaunchUriAsync was handled

I try to solve problem with the Windows Phone Launcher.
I have application and I would like to call another application using the uri and pass it some data. And I would like to know if the second application started. If the application was not started I would like to do some fallback.
Example:
bool result = await Windows.System.Launcher.LaunchUriAsync(new Uri("secondApp://data/123", UriKind.RelativeOrAbsolute));
if (!result)
{
// Opps. The second application is not installed.
ShowToast("Oops. The applications is not installed.");
UseInternalTinyFunctionalityInstead();
}
But the result is always true. Even though the second application is not installed. And additionally the windows message box is displayed "Search for app in the Store? You need to install an app for this task. Would you like to search for one in the Store? yes, no".
Is it possible to check if the second application was started/ is installed?
Is it possible NOT to display this dialog?
Thank you.
Myth Rush

System.Windows.Forms.Application' does not contain a definition for 'ExecutablePath

I am using compact framework 3.5 to build a windows mobile application.I need to restart the application after saving the application settings. I tried the below one,from How do I restart my C# WinForm Application?
string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
Process.Start(path, "");
I am not getting any error,but my application is not restarting.I am checking in my simulator.Do restart working in mobile simulator.
Need solution to solve this problem.
Thanks
Try using this code to access the path:
string path;
path = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
But to restart the application you can use RunAppAtTime method:
var time = DateTime.Now.AddSeconds(3); // immediate restart
Notify.RunAppAtTime(thisName, time); // restart the app
Note that although the time is set to 3 seconds from now it will restart immediately. To have a real delay the time difference must be higher than 10 seconds.
More on that here: https://stackoverflow.com/a/6133136/3330348
An .net application can not restart itself. As it is running on Mobile only one instance is ensured by the framework, if you had targetted Windows CE, you would be able to run multiple instances.
So, RunAppAtTime is a good solution to let the app be started by the Mobile scheduler after the app itself has terminated using Application.Exit().
Another option would be a second application that is started at Application.Exit(), watches the process list to see when main applicaton is terminated (or use GetProcessExitCode), and then starts a new instance of main application. This techique is used for updaters etc.

Reinstantiation issue using Inter App Communication with Windows Phone

I am developing an WP8 app that uses a webbrowser control that shows statefull server content. On WP8 you can switch between apps manually. E.g. if you want to copy&paste some information from one app into a browser input field. If you switch that way, the current app instance stays alive. That means the web session and the current page of the browser control will stay available.
Now I want another app to send some data directly into the app with the browser control - without restarting it...
From what I know, there are three ways to handle inter app communication:
register a file type that will open the app by launching that file from local storage
register an app protocol and use Launcher.LaunchUriAsync() to submit parameters in a query string
use a shared storage file
Detailed information can be found here.
I think the last approach is not usefull, because after you have started the second app, there is now way to activate the calling app or is there any usefull way to reactivate the webbrowser app?
I tried the second approach, but I am running in an issue, because it starts a new instance by design. That means InitializePhoneApplication is called. There is the entry point for the custom UriMapper that reads the incoming parameters. So the old app instance is killed and all session data, cookies and input fields are gone. With WP webbrowser control you are not able to store the cookie and page state, so a fast app resume is not possible also.
private void InitializePhoneApplication()
{
if (this.phoneApplicationInitialized)
{
return;
}
RootFrame = new TransitionFrame();
RootFrame.Navigated += this.CompleteInitializePhoneApplication;
RootFrame.UriMapper = new AssociationUriMapper();
//...
this.phoneApplicationInitialized = true;
}
Is there any other way or a possibility to use the shown approaches to send data between apps without restarting them using LanchUri()?
That means, to send some data back to a running instance without reinitializing the whole app, so that the page state and session state are still available on the target app.
Best regards
Holger
FastAppResume is the solution. I haven't used it and thought it also reinitiates the app. But it doesnt. This example shows how to reuse the existing instance.
Regards
Holger

Keep C# application running

I'm building a Windows Service that uses FileSystemWatcher, and runs in the background.
I don't want to keep on uninstalling and installing the service every time I want to debug, so would like to do most of my development in a normal program before moving it into a service. But I'm quite new to this, and when I run it, it just runs through the block and exits.
What would be a good way to keep the program running?
http://einaregilsson.com/run-windows-service-as-a-console-program/
I've used this before to debug my service as a Console application based on whether its running in an interactive user environment.
public partial class DemoService : ServiceBase
{
static void Main(string[] args)
{
DemoService service = new DemoService();
if (Environment.UserInteractive)
{
service.OnStart(args);
Console.WriteLine("Press any key to stop program");
Console.Read();
service.OnStop();
}
else
{
ServiceBase.Run(service);
}
}
while (true)
{
// Execute your program's functionality here.
}
I wrote a 7 part series a while ago titled: Building a Windows Service. It covers all the intricacies of building services, making them friendly to debug, and self-installing.
The basic feature set I was looking for was as follows:
Building a service that can also be used from the console
Proper event logging of service startup/shutdown and other activities
Allowing multiple instances by using command-line arguments
Self installation of service and event log
Proper event logging of service exceptions and errors
Controlling of start-up, shutdown and restart options
Handling custom service commands, power, and session events
Customizing service security and access control
The final result was a Visual Studio project template that creates a working service, complete with all of the above, in a single step. It's been a great time saver for me.
see Building a Windows Service – Part 7: Finishing touches for a link to the project template and install instructions.
Here’s documentation from MSDN # http://msdn.microsoft.com/en-us/library/7a50syb3(v=vs.80).aspx?ppud=4 . I have tried it before and it works under .NET Framework 3.x. I could not find my descriptive notes on it, at the moment.
Use the pragma #If DEBUG for debugging purposes like console outputs. Another is using the Debug object.
If you have any trouble with this, say so. I may be able to find my notes or make a Windows Service app myself, just to see if the steps on MSDN still work.

Categories