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}}" /> 
  • 2
    Why such operations are performed in the converter, and not in the representation model? - Vlad
  • @Vlad: Via "get; set;" mean? If so, then there will also be a multiple reading, with each update of the collection. - EvgeniyZ
  • You still download all the files from the server. I do not understand what kind of operation confuses you? Just a converter, in my opinion, is an extremely unfortunate place for I / O operations. - Vlad
  • @Vlad: If you use this code, then it opens each time a JSON file, looks for a value in it and returns it. You can see the log of how many times it has completed the translation. That is, in this case, he opened the file 10 times and gave out a value (I translated only a part of the necessary data), and I also read other data from the server every 1 minute, which updates this collection and even the old data again. . - EvgeniyZ
  • one
    @EvgeniyZ: Well, like this. You can use some tricks with HashSet , but this is an improvement and optimization. First write a simple bypass option in a loop. - VladD

0