Essentials icon indicating copy to clipboard operation
Essentials copied to clipboard

UWP Idiom must be called on UI Thread

Open jrahma opened this issue 8 years ago • 8 comments

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;
            }
        }
    }
}


jrahma avatar Jun 22 '18 19:06 jrahma

What happens if you wrap the get location in a try and catch the exception? It may tell you something... Maybe permissions...

mattleibow avatar Jun 25 '18 14:06 mattleibow

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

jamesmontemagno avatar Jun 25 '18 15:06 jamesmontemagno

Closing due to lack of information and activity.

jamesmontemagno avatar Jul 06 '18 18:07 jamesmontemagno

@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.

abhimanyusinghal avatar Sep 10 '18 01:09 abhimanyusinghal

this is still an issue. trying to get a background task to work on UWP, and DeviceInfo.Idiom will fail because of this check.

MitchBomcanhao avatar Sep 25 '19 12:09 MitchBomcanhao

Reopening now that we have more information on this

jamesmontemagno avatar Oct 14 '19 04:10 jamesmontemagno

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

weihangChen avatar Oct 18 '19 19:10 weihangChen

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.

jamesmontemagno avatar Dec 20 '19 00:12 jamesmontemagno