UWP Idiom must be called on UI Thread
Hi,
I have below class but when I call the following from my Signup.xaml.cs:
await eventLog.UpdateEventLog(Convert.ToString(data[0].id), "SNUP", "test", "Successfully Signed up.");
it only run until:
Console.WriteLine("event log starting");
and then stops... but the code works fine if I run it from the Signup.xaml.cs directly.
here is the eventLog code:
using System;
using System.Collections.Generic;
using Xamarin.Forms;
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
using System.Collections.ObjectModel;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions;
using System.Net;
using System.Text;
using Com.OneSignal;
using Syncfusion.SfPicker.XForms;
using Xamarin.Essentials;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace XoomPage
{
public class EventLog
{
public async Task<HttpResponseMessage> UpdateEventLog(string user_id, string category, string reference, string text)
{
string country;
double latitude;
double longitude;
// Console.Clear();
Console.WriteLine("event log starting");
var location = await Geolocation.GetLocationAsync();
Console.WriteLine("event log location is ok");
latitude = location.Latitude;
longitude = location.Longitude;
Console.WriteLine("latitude: " + Convert.ToString(latitude));
Console.WriteLine("longitude: " + Convert.ToString(longitude));
var placemarks = await Geocoding.GetPlacemarksAsync(latitude, longitude);
var placemark = placemarks?.FirstOrDefault();
country = placemark.CountryCode;
string model = DeviceInfo.Model;
string manufacturer = DeviceInfo.Manufacturer;
string device = DeviceInfo.Name;
string version = DeviceInfo.VersionString;
string platform = DeviceInfo.Platform;
string idiom = DeviceInfo.Idiom;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://www.domain.com/ws/event_log.php");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("user", user_id)
/*
new KeyValuePair<string, string>("country", country),
new KeyValuePair<string, string>("latitude", Convert.ToString(latitude)),
new KeyValuePair<string, string>("longitude", Convert.ToString(longitude)),
new KeyValuePair<string, string>("model", model),
new KeyValuePair<string, string>("manufacturer", manufacturer),
new KeyValuePair<string, string>("device", device),
new KeyValuePair<string, string>("version", version),
new KeyValuePair<string, string>("platform", platform),
new KeyValuePair<string, string>("idiom", idiom),
new KeyValuePair<string, string>("category", category),
new KeyValuePair<string, string>("reference", reference),
new KeyValuePair<string, string>("text", text)
*/
});
var response = await client.PostAsync("http://www.domain.com/ws/event_log.php", content);
return response;
}
}
}
}
What happens if you wrap the get location in a try and catch the exception? It may tell you something... Maybe permissions...
You may be missing the initialization code for permissions like @mattleibow said. Did you setup the Init: https://docs.microsoft.com/en-us/xamarin/essentials/get-started
Closing due to lack of information and activity.
@jamesmontemagno
This is happening since in Idiom Property Getter for UWP There is a call to
UIViewSettings.GetForCurrentView()
This must be invoked on UI Thread. Otherwise exception is thrown.
this is still an issue. trying to get a background task to work on UWP, and DeviceInfo.Idiom will fail because of this check.
Reopening now that we have more information on this
I have the same problem, created a brand new IOS project. run the code in this project, it just hangs https://docs.microsoft.com/en-us/xamarin/essentials/geocoding?tabs=android
Putting in a fix where we use a cached approach once it is set once. Which means on your startup of your app you should call Device.Idiom on the UI thread. We may introduce an Init() call in the future similar to forms and that would remove this need.