Localize with values from Dictionary of enums and strings in XAML - xaml

I´m trying to databind the current culture with the right enum set in a Dictionary in Xamarin Forms XAML.
I get a list of Events from the server that contain the Dictionary properties containing the languages that I´m going to use to show the right language depending on what Culture is set.
public class Event
{
// Having problem binding with the right CultureInfo í the Dictionary
public Dictionary<Language, string> Title { get; set; }
}
// Types of languages that I could use to pick the right language string
public enum Language
{
English = 0,
German
}
// Just a basic view-model
public class EventsViewModel
{
public EventsViewModel()
{
}
//James Montemagno´s MVVM Helper https://github.com/jamesmontemagno/mvvm-helpers
public ObservableRangeCollection<Event> Events { get; } = new ObservableRangeCollection<Event>();
}
// Just a basic Content page
public partial class EventsPage : ContentPage
{
public EventsPage ()
{
InitializeComponent();
BindingContext = new EventsViewModel(Navigation);
}
}
Now I just need the XAML to bind the right value in the Dictionary based on the enum/culture...
I have tried doing this "semi-manual" by using a Converter and checking the CultureInfo and picking the right value but for some reason I just can´t get the Converters to fire up.
I also tried to mix up IValueConverter and IMarkupExtension but no good.

Ok I found out what was missing from the Converter method I had been trying to use. I was missing the ConvertParameter and that was the reason the Convert method did´t fire.
So what I did.
1.
I put this line into the App.xaml to be able to access it from anywhere
<converters:LanguageDictionaryEnumConverter x:Key="LanguageConverter" />
2.
Then I put this one into my page. I do not use the parameter at all. (But I would if I could pass in my settings culture object)
<Label Text="{Binding Title,Converter={StaticResource LanguageConverter},ConverterParameter={x:Static dataObjects:Language.English}}" />
3.
And then I implemented a crude dictionary-language-enum converter/picker that chooses the right language string to send back to the page.
public class LanguageDictionaryEnumConverter : IMarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var dic = (Dictionary<Language, string>)value;
var cultInMySettings= Settings.Current.GetCultureInfoOrDefault;
switch (cultInMySettings.Name)
{
case "de-DE":
return dic[Language.German];
case "en-US":
return dic[Language.English];
default:
return dic[Language.English];
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
But if you have some better way to databind the values straight to the XAML in some other way I want to hear about it and will choose that answer over this one for sure if its more elegant.

Related

Derived type's properties missing in JSON response from ASP.NET Core API

The JSON response from my ASP.NET Core 3.1 API controller is missing properties. This happens when a property uses a derived type; any properties defined in the derived type but not in the base/interface will not be serialized to JSON. It seems there is some lack of support for polymorphism in the response, as if serialization is based on a property's defined type instead of its runtime type. How can I change this behavior to ensure that all public properties are included in the JSON response?
Example:
My .NET Core Web API Controller returns this object that has a property with an interface type.
// controller returns this object
public class Result
{
public IResultProperty ResultProperty { get; set; } // property uses an interface type
}
public interface IResultProperty
{ }
Here is a derived type that defines a new public property named Value.
public class StringResultProperty : IResultProperty
{
public string Value { get; set; }
}
If I return the derived type from my controller like this:
return new MainResult {
ResultProperty = new StringResultProperty { Value = "Hi there!" }
};
then the actual response includes an empty object (the Value property is missing):
I want the response to be:
{
"ResultProperty": { "Value": "Hi there!" }
}
While the other answers are good and solves the problem, if all you want is the general behavior to be like pre netcore3, you can use the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package and in Startup.cs do:
services.AddControllers().AddNewtonsoftJson()
More info here. This way, you don't need to create any extra json-converters.
I ended up creating a custom JsonConverter (System.Text.Json.Serialization namespace) which forces JsonSerializer to serialize to the object's runtime type. See the Solution section below. It's lengthy but it works well and does not require me to sacrifice object oriented principles in my API's design. (If you need something quicker and can use Newtonsoft then check out the top voted answer instead.)
Some background: Microsoft has a System.Text.Json serialization guide with a section titled Serialize properties of derived classes with good information relevant to my question. In particular it explains why properties of derived types are not serialized:
This behavior is intended to help prevent accidental exposure of data
in a derived runtime-created type.
If that is not a concern for you then the behavior can be overridden in the call to JsonSerializer.Serialize by either explicitly specifying the derived type or by specifying object, for example:
// by specifying the derived type
jsonString = JsonSerializer.Serialize(objToSerialize, objToSerialize.GetType(), serializeOptions);
// or specifying 'object' works too
jsonString = JsonSerializer.Serialize<object>(objToSerialize, serializeOptions);
To accomplish this with ASP.NET Core you need to hook into the serialization process. I did this with a custom JsonConverter that calls JsonSerializer.Serialize one of the ways shown above. I also implemented support for deserialization which, while not explicitly asked for in the original question, is almost always needed anyway. (Oddly, supporting only serialization and not deserialization proved to be tricky anyway.)
Solution
I created a base class, DerivedTypeJsonConverter, which contains all of the serialization & deserialization logic. For each of your base types, you would create a corresponding converter class for it that derives from DerivedTypeJsonConverter. This is explained in the numbered directions below.
This solution follows the "type name handling" convention from Json.NET which introduces support for polymorphism to JSON. It works by including an additional $type property in the derived type's JSON (ex: "$type":"StringResultProperty") that tells the converter what the object's true type is. (One difference: in Json.NET, $type's value is a fully qualified type + assembly name, whereas my $type is a custom string which helps future-proof against namespace/assembly/class name changes.) API callers are expected to include $type properties in their JSON requests for derived types. The serialization logic solves my original problem by ensuring that all of the object's public properties are serialized, and for consistency the $type property is also serialized.
Directions:
1) Copy the DerivedTypeJsonConverter class below into your project.
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
public abstract class DerivedTypeJsonConverter<TBase> : JsonConverter<TBase>
{
protected abstract string TypeToName(Type type);
protected abstract Type NameToType(string typeName);
private const string TypePropertyName = "$type";
public override bool CanConvert(Type objectType)
{
return typeof(TBase) == objectType;
}
public override TBase Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// get the $type value by parsing the JSON string into a JsonDocument
JsonDocument jsonDocument = JsonDocument.ParseValue(ref reader);
jsonDocument.RootElement.TryGetProperty(TypePropertyName, out JsonElement typeNameElement);
string typeName = (typeNameElement.ValueKind == JsonValueKind.String) ? typeNameElement.GetString() : null;
if (string.IsNullOrWhiteSpace(typeName)) throw new InvalidOperationException($"Missing or invalid value for {TypePropertyName} (base type {typeof(TBase).FullName}).");
// get the JSON text that was read by the JsonDocument
string json;
using (var stream = new MemoryStream())
using (var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Encoder = options.Encoder })) {
jsonDocument.WriteTo(writer);
writer.Flush();
json = Encoding.UTF8.GetString(stream.ToArray());
}
// deserialize the JSON to the type specified by $type
try {
return (TBase)JsonSerializer.Deserialize(json, NameToType(typeName), options);
}
catch (Exception ex) {
throw new InvalidOperationException("Invalid JSON in request.", ex);
}
}
public override void Write(Utf8JsonWriter writer, TBase value, JsonSerializerOptions options)
{
// create an ExpandoObject from the value to serialize so we can dynamically add a $type property to it
ExpandoObject expando = ToExpandoObject(value);
expando.TryAdd(TypePropertyName, TypeToName(value.GetType()));
// serialize the expando
JsonSerializer.Serialize(writer, expando, options);
}
private static ExpandoObject ToExpandoObject(object obj)
{
var expando = new ExpandoObject();
if (obj != null) {
// copy all public properties
foreach (PropertyInfo property in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.CanRead)) {
expando.TryAdd(property.Name, property.GetValue(obj));
}
}
return expando;
}
}
2) For each of your base types, create a class that derives from DerivedTypeJsonConverter. Implement the 2 abstract methods which are for mapping $type strings to actual types. Here is an example for my IResultProperty interface that you can follow.
public class ResultPropertyJsonConverter : DerivedTypeJsonConverter<IResultProperty>
{
protected override Type NameToType(string typeName)
{
return typeName switch
{
// map string values to types
nameof(StringResultProperty) => typeof(StringResultProperty)
// TODO: Create a case for each derived type
};
}
protected override string TypeToName(Type type)
{
// map types to string values
if (type == typeof(StringResultProperty)) return nameof(StringResultProperty);
// TODO: Create a condition for each derived type
}
}
3) Register the converters in Startup.cs.
services.AddControllers()
.AddJsonOptions(options => {
options.JsonSerializerOptions.Converters.Add(new ResultPropertyJsonConverter());
// TODO: Add each converter
});
4) In requests to the API, objects of derived types will need to include a $type property. Example JSON: { "Value":"Hi!", "$type":"StringResultProperty" }
Full gist here
The documentation shows how to serialize as the derived class when calling the serializer directly. The same technique can also be used in a custom converter that we then can tag our classes with.
First, create a custom converter
public class AsRuntimeTypeConverter<T> : JsonConverter<T>
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return JsonSerializer.Deserialize<T>(ref reader, options);
}
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value, value?.GetType() ?? typeof(object), options);
}
}
Then mark the relevant classes to be used with the new converter
[JsonConverter(typeof(AsRuntimeTypeConverter<MyBaseClass>))]
public class MyBaseClass
{
...
Alternately, the converter can be registered in startup.cs instead
services
.AddControllers(options =>
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new AsRuntimeTypeConverter<MyBaseClass>());
}));
I had a similar issue, where I was returning an enumerable of type TAnimal (but the object instances were of derived types such as Dog, Cat, etc.):
[HttpGet]
public IEnumerable<TAnimal> GetAnimals()
{
IEnumerable<TAnimal> list = GetListOfAnimals();
return list;
}
This only included properties defined in TAnimal.
However, in ASP .NET Core 3.1 at least, I found that I could just cast the object instances to object, and the JSON serializer then included all the properties from the derived classes:
[HttpGet]
public IEnumerable<object> GetAnimals()
{
IEnumerable<TAnimal> list = GetListOfAnimals();
return list.Select(a => (object)a);
}
(Note that the signature of the GetAnimals method must also changed, but that doesn't usually matter much in a web API context). If you need to provide type information for Swagger or whatever, you can annotate the method:
[HttpGet]
[Produces(MediaTypeNames.Application.Json, Type = typeof(TAnimal[]))]
public IEnumerable<object> GetAnimals()
{
...
}
Casting to object is a simple solution if you only have a 1-layer-deep object hierarchy to worry about.
This is the expected result. You're upcasting when you do that, so what will be serialized is the upcasted object, not the actual derived type. If you need stuff from the derived type, then that has to be the type of the property. You may want to use generics for this reason. In other words:
public class Result<TResultProperty>
where TResultProperty : IResultProperty
{
public TResultProperty ResultProperty { get; set; } // property uses an interface type
}
Then:
return new Result<StringResultProperty> {
ResultProperty = new StringResultProperty { Value = "Hi there!" }
};
I solved it by writing this extension:
public static class JsonSerializationExtensions
{
public static string ToJson<T>(this IEnumerable<T> enumerable, bool includeDerivedTypesProperties = true)
where T : class
{
var jsonOptions = new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
if (includeDerivedTypeProperties)
{
var collection = enumerable.Select(e => e as object).ToList();
return JsonSerializer.Serialize<object>(collection, jsonOptions);
}
else
{
return JsonSerializer.Serialize(enumerable, jsonOptions);
}
}
}
I was also struggling with this in a .NET Core 3.1 API, where I wanted the result to include $type attribute.
As suggested, install the correct package and then 'AddNewtonsoftJson'.
I wanted the $type field to be added to show the derived type handling, to get that
services.AddControllers().AddNewtonsoftJson(options =>
{
options.SerializerSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All;
});
Not knocking Newtonsoft, but I found an easier way to resolve this with the built handlers.
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/emps", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
List<emp> GetEmps();
//[DataContract(Namespace = "foo")] <<< comment/removed this line
public class emp
{
public string userId { get; set; }
public string firstName { get; set; }
}
public class dept
{
public string deptId{ get; set; }
public string deptName{ get; set; }
}
In my case dept objects where working fine, but emp ones were not - they came across as empty.

LookUpEdit many-to-many

I have this relation:
PRODUCT o---o< PRODUCTCATEGORY >o---o CATEGORY
Category information to show to the user: Name
This information loads from database correctly to the model (EF Core 2). The problem i can't resolve is to use LookUpEdit for choose different categories to a product and show this selection at the text control. Of course, i need to show ALL the categories to the user first, but i need to load the previosly database saved product's categories too when the product is selected.
Then essential problem is that i try to work with one criteria ( product.ProductCategory.Category ) but LookUpEdit binds at the start with all the categories:
LOOKUPEDIT XAML CODE:
<dxg:LookUpEdit ItemsSource="{Binding Categories}" IsTextEditable="False" EditValue="{Binding Product.ProductCategories, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource CategoryConverter}}">
AND CATEGORY CONVERTER CODE:
public class CategorYConverter : MarkupExtension, IValueConverter
{
public CategorYConverter() { }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
if (value is ObservableHashSet<ProductCategory>)
{
ObservableHashSet<ProductCategory> ProductCategories = value as ObservableHashSet<ProductoCategory>;
return new List<object>((ProductCategory.Select(c => c.Category)));
}
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
I can load producselected categories but i can't resolve when the user chooses a category from the lookupedit. It doesnt bind correctly to lookupedit. So i can't work with the both solutions together.
Finally i found out the problem: when the user select categories from the lookupedit category control, the ConvertBack function is called. So I only needed to 'transform' the data received to the data that expects the EditValue Binding: Product.ProductCategories. Exactly in the same format, in my case an ObservableHashSet
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
IList<object> map = value as IList<object>;
ObservableHashSet<ProductCategory> pc = new ObservableHashSet<ProductCategory>();
foreach (var m in map)
{
pc.Add(new ProductCategory() { Category = m as Category });
}
return pc;
}
Hope this helps someone

How would I default visibility of a button to false if binding is null in Xamarin

Apparantly in Xamarin forms there isn't an option to use FallbackValue or TargetNullValue, how could I use a converter to accomplish the task?
I'm looking to have visibility default to null if the data binding object is null.
NullConverter.cs
public class NullConverter : IMarkupExtension, IValueConverter
{
public object IsNullValue { get; set; }
public object IsNotNullValue { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null ? IsNullValue : IsNotNullValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
XAML
<Entry Text="{Binding WhateverProperty}" />
<Button IsVisible="{Binding WhateverProperty, Converter={local:NullConverter IsNullValue=False, IsNotNullValue=True}}" />
For my testing, WhateverProperty was a string that was originally set to null, whenever I update the Entry, the button shows up. Of course, you can use it with any type of property.

custom serializer for just one property in Json.NET

UPDATE
Found the issue -- was inheriting from wrong class, needed to be JsonConverter.
I have a class that has a Location property of type System.Data.Entity.Spatial.DbGeography. The default Json.NET serializer puts out JSON text like this:
...
"PlaceType": 0,
"Location": {
"Geography": {
"CoordinateSystemId": 4326,
"WellKnownText": "POINT (-88.00000 44.00000)"
}
},
"AddedDT": null,
...
I want it to put out text like this:
...
"PlaceType": 0,
"Location": [-88.00000,44.00000],
"AddedDT": null,
...
...so it seems to me what I should do would be to override whatever converter is currently being used on the DbGeography type.
The examples I've seen so far that use CustomCreationConverters and ContractResolvers seem to address how you'd replace the serializer for the main class being serialized, not for a type that's only a property of that class. The examples that involve annotating the class that's being overridden don't work for me because I don't define DbGeography in my code and it's effectively a sealed class because it has no constructor and can only be instantiated by internal factory methods.
Is there a way to apply a JsonConverter to a type fluently? If so, what would the converter look like? Do I just override the WriteJson() method?
You can add a custom serializer to a single attribute like this:
public class Comment
{
public string Author { get; set; }
[JsonConverter(typeof(NiceDateConverter))]
public DateTime Date { get; set; }
public string Text { get; set; }
}
public class NiceDateConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var date = (DateTime) value;
var niceLookingDate = date.ToString("MMMM dd, yyyy 'at' H:mm tt");
writer.WriteValue(niceLookingDate);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
}
public override bool CanRead
{
get { return false; }
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(DateTime);
}
}
Then, when you serialize your object with JsonConvert.SerializeObject(), the custom serializer will be used for the Date property.
Turns out I just needed to inherit from JsonConverter instead of CustomCreationConverter, and everything else I was trying to change was OK all along.
I'm still not sure if there's a way to apply the JsonConverter fluently, but there is another way to apply the JsonConverter without referencing Json.NET in your domain/core project or marking up your domain classes with references to a peripheral library:
var jsonSerializer = new JsonSerializer();
jsonSerializer.Converters.Add(new DbGeographyConverter());
jsonSerializer.Serialize(jsonWriter, place);
Use the JsonConverterAttribute on the property and define a custom converter-
for example, we have a property that comes in as a unix value (long int) and we serialize it to a .Net DateTime:
[JsonConverter(typeof(UnixTimeJsonConverter))]
public DateTime Requested { get; set; }

What is more efficient {X:Static Class.Default} or {StaticResource Class}?

Suppose I have class I want to reference in XAML.
public class MyConverter
{
public static readonly MyConverter Default = new MyConverter();
...
};
And then in XAML I can reference it either
<Label Content="{Binding Text,Converter={x:Static local:MyConverter.Default}"/>
or
<local:MyConverter x:Key="MyConverter"/>
...
<Label Content="{Binding Text,Converter={StaticResource local:MyConverter}"/>
Which way is more efficient?
I doubt anything here will be more effecient than other but the key difference here is what is actually going on:
In first approach you're referencing static field of class MyConverter
In second case you're creating an instance of MyConverter and using it.
I believe first one might couple percents faster (or what do you mean by efficient?) but this difference won't give you much profit. I would choose option #1 if you already have a static field. Also as far as I remember x:Static still is not available in Silverlight.
I find the first option interesting as it provides a nice and clean possibility to use the class for different variations of a task in combination with a singleton. Imagine a visiblity value converter like this:
public class VisibilityConverter : IValueConverter
{
private static readonly VisibilityConverter defaultInstance = new VisibilityConverter();
public static VisibilityConverter Default = new VisibilityConverter();
public static VisibilityConverter InverseBoolean = new VisibilityConverter() { Inverse = true };
public bool Inverse { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var isVisible = value != null
&& value.GetType() == typeof(bool)
&& (bool)value;
if (Inverse)
{
isVisible = !isVisible;
}
return isVisible ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
}
This can then easily be used in a default and in a inverse mode:
Converter={x:Static converters:VisibilityConverter.Default}
Converter={x:Static converters:VisibilityConverter.InverseBoolean}