Can someone educate me a little on the status of Xamarin.UWP?

Charles Mills 26 Reputation points
2021-05-03T17:20:22.487+00:00

Level set: I am a very experienced developer but on my first Xamarin Forms app.

My app is working at an alpha level in Android. I have not yet jumped through all of the iOS hoops. I initially ignored UWP because it is semi-unsupported. I subsequently decided to see if I could not build it in UWP also and I have been 98% successful. The app builds and runs and the pages appear correctly.

I ran into one unexplained anomaly. Basically I have an instance of a user-defined class initialized statically. I do an await on SecureStorage.GetAsync() and when I come back from the await, my class instance -- not referenced by the GetAsync() -- has become null. I hacked around that with if (instance == null) instance = new MyClass(); Fortunately I was able to do that because there was no values in the instance that I "needed" at that point in the logic.

I am now hitting another unexplained anomaly. I have not fully investigated but I seem to have code again with await SecureStorage.GetAsync() that seems to fail normally but work if I step through it with the debugger -- which is pretty frustrating! (This is code that works without issue on Android.)

So ... can someone tell me if I am just looking for frustrations and banging my head against the wall trying to use Xamarin Forms UWP ... or if there is something specific different that I need to be aware of ... or ... ?

Thanks!

VS 2019 Community 16.9.4 on Windows Pro 64-bit. Nothing real special in the app. Using SecureStorage, System.Security.Cryptography and System.Linq. No Crypto calls yet at the point of the problems I describe above.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
Universal Windows Platform (UWP)
{count} votes

5 answers

Sort by: Most helpful
  1. Charles Mills 26 Reputation points
    2021-05-08T00:29:27.533+00:00

    I think I solved it.

    Is not any async void method that sets global variables such as my RestoreSystemIndex() an invitation to trouble?

    I can't explain exactly how it would lead to CurrentSystem becoming null but I made all of my async void methods instead async Task and all of the mysterious problems went away, including the CurrentSystem problem.

    I had to move the call to RestoreSystemIndex() from the AppShell constructor to app.OnStart() because (apparently) constructors cannot make await calls.

    I got going down this async void road because this is my first Xamarin project and it worked perfectly on Android.

    1 person found this answer helpful.

  2. Charles Mills 26 Reputation points
    2021-05-04T13:39:10.1+00:00

    Here is the code. When I breakpoint on the GetAsync() CurrentSystem is an instance of Configuration as one would expect. After the GetAsync() completes it is null.

    public class Configuration
        {
            public const int SystemIndexUndefined = -1;
    
            public int SystemIndex = SystemIndexUndefined;
            /* etc. */
        };
    
        public static class State
        {
            public static Configuration CurrentSystem = new Configuration();
            /* snip */
            private static async Task<string> OurGetAsync(string key)
            {
                try
                {
                    return await SecureStorage.GetAsync(key);
                }
                catch (Exception ex)
                {
                     Logger.LogMsg($"Exception {ex} thrown by GetAsync({key})");
                }
                return null;
            }
    
            public static async void RestoreSystemIndex()
            {
                string s = await OurGetAsync("SystemIndex");
                if (s == null) CurrentSystem.SystemIndex = Configuration.SystemIndexUndefined;    // Not recorded
                else CurrentSystem.SystemIndex = Convert.ToInt32(s);
            }
            /* etc. */
    

  3. Charles Mills 26 Reputation points
    2021-05-05T19:05:40.633+00:00

    94122-before-await.jpg
    94078-after-await.jpg

    Here you go! In the first screen shot you can see the debugger stopped on the GetAsync() and in the Watch List CurrentSystem is populated. I hit F11, the debugger executes the Await GetAsync(), and you can see that CurrentSystem has gone to null.


  4. Charles Mills 26 Reputation points
    2021-05-06T03:26:48.877+00:00

    @Nico Zhu (Shanghai Wicresoft Co,.Ltd.) , does not public static Configuration CurrentSystem = new Configuration(); (line 3 above) constitute initializing CurrentSystem? That's a serious question? Is it wrong in some way?

    Also please note my first screen shot that shows CurrentSystem pointing to an instance of Configuration.


  5. Charles Mills 26 Reputation points
    2021-05-07T00:46:57.813+00:00

    I thought perhaps the problem was due to the way I constructed this Solution: build a mostly-working solution, and then shoe-horn a UDP project into it.

    So I spent today starting over: I built a brand-new Xamarin.Forms Solution -- including a UDP project -- from scratch, and then added my existing source code to it.

    Sad to say the CurrentSystem going to null problem still exists, or rather exists also in the new Solution, and the other problem that I cannot describe in a few sentences does also.

    Mind you, this is all common code that works apparently flawlessly on Android. (Have not yet built for iOS.)