We're building a multi-platform (desktop- and tablet-computers) application targeting Windows 8.1 as well as 10. As we process spatial data, we offer the user the possibility to use his current location using the device's gps receiver. To allow easy adaption to the (hardware) environment, we placed the gps-logic (including the API-calls) into an external assembly that we load based on a configuration.
Recently we discovered some problems with the gps-module using the Geolocator from Windows.Devices.Geolocation-API under windows 10 (but previously running without problem on Windows 8.1), not delivering any location. Further investigation and RTFM showed up, that - under Windows 10 - we were obliged to call the RequestAccessAsync before calling for the location.
As the RequestAcessAsync-method isn't available in Windows 8.1, we decided to create a new assembly, targeting Windows 10 (and then easily being bound/used through our configuration), what worked quite well:
public Win10GpsProvider()
{
RequestAccessAsync();
}
public async void RequestAccessAsnyc()
{
//next line causes exception:
var request = await Geolocator.RequestAccessAsync();
switch (request)
{
// we don't even get here... :(
}
}
Only up to running into an exception that gets thrown as soon as the RequestAccessAsync-method is being called (in UI-thread), stating that the call has to be made in the context of an app container.
This operation is only valid in the context of an app container. (Exception from HRESULT: 0x8007109A)
The exception occurs on both, desktop as well as tablet (verified through remoted debugging).
Searching a bit more, adding the "location" as a required capability to the package.appxmanifest may help:
<Capabilities>
<Capability Name="location"/>
</Capabilities>
That's where we're actually stuck at the moment:
We don't have an UWP-application (and actually don't want/can change that, as we're targeting Win 8.1 as well and have a defined deployment workflow including setups)
We can't get the assembly run without exception as there is no UWP app/context
Is there a way to get a separate assembly that targets the Windows 10 version of the Windows.Devices.Geolocation-API and can be called/loaded by a Win32-application?
AFAIK, it is not doable to call RequestAcessAsync method and make it work in Windows 8.1 app.
But if you directly use Geoposition in windows 8.1 project, and run this windows 8.1 app on windows 10 device for example on desktop, the permission request dialog will also be displayed:
So there should be no problem if you don't call RequestAcessAsync method in windows 8.1 app.
But in case user choose "No" in this permission request dialog, we can catch the exception and launch the Setting page to force user to enable the Location setting for this app for example like this:
private Geolocator _geolocator = null;
private uint _desireAccuracyInMetersValue = 0;
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
try
{
var _cts = new CancellationTokenSource();
CancellationToken token = _cts.Token;
_geolocator = new Geolocator { DesiredAccuracyInMeters = _desireAccuracyInMetersValue };
Geoposition pos = await _geolocator.GetGeopositionAsync().AsTask(token);
// Subscribe to PositionChanged event to get updated tracking positions
_geolocator.PositionChanged += OnPositionChanged;
// Subscribe to StatusChanged event to get updates of location status changes
_geolocator.StatusChanged += OnStatusChanged;
}
catch (Exception ex)
{
await Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-location"));
}
}
This code in windows 8.1 project works fine both on windows 8.1 device and windows 10 device. And I don't quite understand what is your Win32-application for here?
Related
I have access fine and coarse location checked in the manifest file and first time the application runs on the android emulator (Android_Accelerated_x86 (Android 7.1 - API 25)) it logs the latitude. But if I deploy the app for the second time, third time and so on it runs endlessly even though the timeout is 10000 ms (no Exception thrown). If I close and open the emulator and deploy the app again it works again and so on. Any ideas on how to fix this?
public static class NetworkingTools
{
private static IGeolocator locator = Resolver.Resolve<IGeolocator>();
static NetworkingTools()
{
locator.DesiredAccuracy = 50;
locator.StartListening(2000, 0, true);
GetPosition();
}
public async static void GetPosition()
{
var position = await locator.GetPositionAsync(10000);
System.Diagnostics.Debug.WriteLine(position.Latitude);
}
}
Please note the remark they have on their GitHub page:
NOTICE: This project is no longer maintained. It may not work with newer versions of Xamarin.Forms.
I think it is unwise to integrate this into your project if you have the choice. Instead have a look at the Geolocator Plugin by James Montemagno.
I was having the same issue, but apparently that's the way the emulators work. There is no problem with the code, you have to open the EMULATOR Settings, go to Location and Click SEND.
More details here:
Getting Location on Android Emulator more then once
We are currently working on a project in UWP where we have to start an external application to modify some documents.
We looked into the Windows.System.Launcher API but it seems that we need more than what it can offer us.
As we launched the application from a file, we use the LaunchFileAsync method, based on the example given by the MSDN :
async void DefaultLaunch()
{
// Path to the file in the app package to launch
string imageFile = #"images\test.png";
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
if (file != null)
{
// Launch the retrieved file
var success = await Windows.System.Launcher.LaunchFileAsync(file);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
}
So far, the example suit us well but we also need to be warned when the user is done with the file. The best would be to be able to give the launcher a callback method.
We haven't found anything like that yet in the documentation. Is this even possible ? Do we need to use another solution ?
TL;DR : Is there a solution to open another application from a UWP app and wait for it to return a result object ?
If the external app is also a UWP app then Launcher.LaunchUriForResultsAsync is designed for this. It will launch the target app then wait for the app to call back with the results.
See Launch an app for results for a full walkthrough of how this works.
If the target app isn't a UWP app then you can implement the same thing yourself: both apps declare a protocol. The client launches the server with the server's protocol. When the server's done it notifies the caller by launching the client's protocol.
You might also want to look into App Services which allow a UWP server app to expose a REST-like service to clients on the local system.
The app process isolation model means you can't do this from a UWP app. As you just want to know when an arbitrary program has finished you could write this in traditional .net/win32 and include that in your UWP app via the desktop bridge.
I'm trying to register an in process background task that will enable me to process a background-event from a toast-notification action. I have created an override of the OnBackgroundActivated in my app class, but when i call register on my BackgroundTaskBuilder i get this exception:
Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
BackgroundTaskBuilder builder = new BackgroundTaskBuilder()
{
Name = "myTask"
};
builder.SetTrigger(new ToastNotificationActionTrigger());
BackgroundTaskRegistration task = builder.Register();
I find a lot of solutions that suggests this is because the name of the entry point in Package.appxmanifest is incorrect. For example here, here, and here. But according to this article on msdn, i'm not supposed to specify the entry point when i want to perform an in-process background task:
For in-process background activities you do not set TaskEntryPoint. Leaving it blank enables the default entry point, a new protected method on the Application object called OnBackgroundActivated().
The msdn-article states that i have to call RequestAccessAsync, but both documentation and intellisense indicates that it's not necessary. However, if i call RequestAccessAsync, it throws the same exception.
The problem was caused by the OS on the Windows Phone emulator I used. Reinstalling the SDK seems to be the proper way to solve this as Vincent suggested in his comment. Reinstalling the SDK on my computer obviously had no effect since the problem occurred after deployment to the virtual device.
By resetting the phone in the device settings on the emulator I was able to deploy and run the application successfully on the emulator again.
I have an exception only in one PC, in others all work fine, anyone know wher it is comming from?
dditional information: Requested Windows Runtime type
'Windows.Media.Capture.MediaCapture' is not registered.
This exception is showing only in modern style apps (windows strore app) in windows 8.1. In WPF or Windows Form apps camera works fine. Code is fine, because in other pc work great:) i install system one more time, but the exception still showing up.
Looking at Microsoft's Windows Universal Samples (https://github.com/Microsoft/Windows-universal-samples/blob/e13cf5dca497ad661706d150a154830666913be4/Samples/SpeechRecognitionAndSynthesis/cs/AudioCapturePermissions.cs#L35) shows following piece of code
try
{
// Request access to the microphone only, to limit the number of capabilities we need
// to request in the package manifest.
MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings();
settings.StreamingCaptureMode = StreamingCaptureMode.Audio;
settings.MediaCategory = MediaCategory.Speech;
MediaCapture capture = new MediaCapture();
await capture.InitializeAsync(settings);
}
catch (TypeLoadException)
{
// On SKUs without media player (eg, the N SKUs), we may not have access to the Windows.Media.Capture
// namespace unless the media player pack is installed. Handle this gracefully.
var messageDialog = new Windows.UI.Popups.MessageDialog("Media player components are unavailable.");
await messageDialog.ShowAsync();
return false;
}
So you have to install "Media player components".
I just had this issue with Windows 10. I had installed the N edition, but it looks like since it is missing Media Player, the classes related to MediaCapture are also missing.
As Hans Passant mentioned, the MediaCapture class was not registered in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsRuntime\ActivatableClassId.
I reinstalled Window 10 (not the N edition) and now the class is registered.
i am facing a weird problem. I am using Windows phone 8.1 silverlight and Windows universal link for toast notification. I am able to get the toast notification in Windows Phone app it is nearly same way in Windows 8.1 as mentioned in the link. But toasts are not comming in Windows 8.1 part.
Steps :-
1 -> I have enabled the Toast from Package.appxmanifest file.
2 -> Then i have added this code in App.Xaml.cs in on_launched event as mentioned
It is showing the registeration successful as mentioned in the link
private async void InitNotificationsAsync()
{
var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
var hub = new NotificationHub("<hub name>", "<connection string with listen access>");
var result = await hub.RegisterNativeAsync(channel.Uri);
// Displays the registration ID so you know it was successful
if (result.RegistrationId != null)
{
var dialog = new MessageDialog("Registration successful: " + result.RegistrationId);
dialog.Commands.Add(new UICommand("OK"));
await dialog.ShowAsync();
}
}
3 -> Now i send the notification from back end(Hub is working in case of phone mpns notification).
public static async void SendNotificationAsync()
{
NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString("<connection string with full access>", "<hub name>");
var toast = #"<toast><visual><binding template=""ToastText01""><text id=""1"">Hello from a .NET App!</text></binding></visual></toast>";
await hub.SendWindowsNativeNotificationAsync(toast);
}
Question : -
Do you guys have a clue why it is not working in Windows 8.1 or what i am missing. This is the simplest example i am going through. Any help is appreciated.
Edit :- This is something interesting.
When i tried to send the notification from azure portal. From DEBUG tab of My-NotificationHub and from here i choose WNS toast then it showing an error.
Error - The token obtained from the token provider is wrong
but when i tried to send MPNS notification then there is no error. as i already mentioned it is working for WP 8.1 silverlight MPNS toast.
What could be the cause of this error ?
Toast will not work on simulator, as stated in the MSDN documentation:
Note When testing toast notification code functionality through
Microsoft Visual Studio, you must use either the Local Machine or
Remote Machine debug setting on a Windows x86, x64, or Windows Runtime
machine. You cannot use the Visual Studio Simulator debug function
option—your code will compile and run in the Simulator, but the toast
will not appear.
Source: http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh868254.aspx
If I remember correctly on Windows 8 the toast notifications did not work when I was using the simulator. I had to deploy my Windows 8 application on my computer and not the simulator for me to be able to see toast notifications.