Kind, I have an associated List collection to ListBox . The collection contains values that need to be translated according to the following logic:
- We receive from the JSON server.
- Save it and find the required value in the file.
- If the value is found, then output it, if not - output the original value.
I implemented all this with the help of IValueConverter , which is set using a binding. But that's the problem! So, as the collection is updated every 1 minute and there are 8 values, then it turns out that at least 8 times it loads the JSON file (this is without a read file from the server connected ...).
In this case, I need to prohibit the conversion, if the value has already been transferred. Tried by this principle (that is, we set a variable and enter a value into it, if the variable is not empty - we derive it without performing all the logic), but this option does not work for me, it simply remembers the first value and constantly translates it.
Actually the question: How to prevent the conversion to be performed more than 1 time?
The actual code of the current converter:
class PlanetConvert : IValueConverter { private string _item; public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (_item != null) return _item; string[] planet; Debug.WriteLine($"Переводим значение {value}!", $"[{DateTime.Now}]"); try { using (StreamReader r = new StreamReader($"{Settings.Program.Directories.Data}/Filters/Planets.json")) { var json = r.ReadToEnd(); var data = (JObject)JsonConvert.DeserializeObject(json); planet = System.Convert.ToString(data["Items"].First[value]).Split('|'); } } catch (Exception e) { Debug.WriteLine(e); return value; } if (parameter != null && parameter.ToString() == "Sector" && planet.Length >= 2) { _item = planet[1].ToUpper(); return _item; } if (parameter != null && parameter.ToString() == "Planet" && planet.Length >= 2) { _item = planet[0].ToUpper(); return _item; } return value; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return DependencyProperty.UnsetValue; } } Binding:
<Run Text="{Binding MissionInfo.Location, ConverterParameter=Planet, Converter={StaticResource PlanetConvert}}" />
HashSet, but this is an improvement and optimization. First write a simple bypass option in a loop. - VladD