Example #1
0
        private async void showCount()
        {
            // if you generate code for Xamarin.iOS, you use under.

            /*Utf8Json.Resolvers.CompositeResolver.Register(
             *  // code generating resolver
             *  Utf8Json.Resolvers.GeneratedResolver.Instance,
             *  Utf8Json.Resolvers.BuiltinResolver.Instance,
             *  Utf8Json.Resolvers.AttributeFormatterResolver.Instance,
             *  Utf8Json.Resolvers.DynamicGenericResolver.Instance,
             *  Utf8Json.Resolvers.EnumResolver.Default
             * );*/
            // var formatterResolver = new Utf8JsonFormatterResolver(Utf8Json.Resolvers.CompositeResolver.Instance);

            var formatterResolver = new Utf8JsonFormatterResolver(AotStandardResolver.Default);
            var sharedDictionary  = new SharedDictionary(
                new Utf8JsonSerializer(formatterResolver),
                IsolatedFileStorage.Default,
                AesCryptoConverter.Default
                );
            await sharedDictionary.LoadFromStorageAsync();

            if (sharedDictionary.TryGetProperty(welcomeKey, out int count))
            {
                WelcomeCounter.Text = $"Welcome Count: {count}";
            }
            else
            {
                WelcomeCounter.Text = $"First Welcome!";
            }

            count++;

            sharedDictionary.SetProperty(welcomeKey, count);

            await sharedDictionary.SaveToStorageAsync();
        }
Example #2
0
        public static async Task Main(string[] args)
        {
            var sharedDictionary = new SharedDictionary(Utf8JsonSerializer.Default, FileStorage.Default, null);

            sharedDictionary.SetProperty("text", "sssss");
            sharedDictionary.SetProperty("number", 1234);
            sharedDictionary.SetProperty("data", new Data());
            sharedDictionary.SetProperty("list", new List <int> {
                1, 2, 3, 4
            });

            await sharedDictionary.SaveToStorageAsync();

            sharedDictionary = new SharedDictionary(Utf8JsonSerializer.Default, FileStorage.Default, null);
            await sharedDictionary.LoadFromStorageAsync();

            foreach (var property in sharedDictionary)
            {
                WriteLine($"key: {property.Key}");
            }

            WriteLine(sharedDictionary.GetProperty <string>("text"));
            WriteLine(sharedDictionary.GetProperty <int>("number"));
            WriteLine(sharedDictionary.GetProperty <long>("number"));
            WriteLine(sharedDictionary.GetProperty <Data>("data"));
            WriteLine(sharedDictionary.GetProperty <List <int> >("list")?.Count);
            WriteLine(sharedDictionary.GetProperty <IEnumerable <int> >("list").Count());

            var serializer = new Utf8JsonSerializer();

            serializer.MigrationTypeDictionary[TypeCache <Data> .FullName] = TypeCache <MigratedData> .FullName;
            sharedDictionary = new SharedDictionary(serializer, FileStorage.Default, null);
            await sharedDictionary.LoadFromStorageAsync();

            WriteLine($"MigratedData: {sharedDictionary.GetProperty<MigratedData>("data")}");
        }