I'm a little unsure how to ask this question.
Update - code samples now included.
I'm building a 'for fun' UWP application; I have a user initiated operation where I perform an action on each item in a collection. I want to keep the user informed of progress as this happens.
An approach that has worked for me in the past (Winforms) is to fire an event each time I process an item, and update (for example) the text of a textbox. My UWP code works OK except that it only shows the 'status' of the last action, and not the preceding ones.
Question: Am I approaching this entirely wrong, or do I need to do the equivalent of textbox.Refresh() ? (I'd be surprised if I do, .Refresh() seems a little 1990's)
FYI, Another approach I've used is to update a 'status' property in the logic, and have the UI run a timer which polls the logic for the current status (i.e. showing status at a point in time rather than on an event inside the logic). I just haven't had time to try this yet in any UWP app.
I'm happy to be told this second way is 'better' but I'd specifically also like to understand more about why the first way isn't working.
Firstly there's this class which handles the heavy-lifting:
public class Enigma1Device : IEnigmaDevice
{
public event CharacterEnciphered OnCharacterEnciphered;
public string TypeMessage(string message)
{
StringBuilder output = new StringBuilder();
char[] chars = message.ToCharArray();
char temp;
if (OnCharacterEnciphered != null)
{
for (int c = 0; c < chars.Length; c++)
{
if (ValidInput(chars[c]))
{
temp = ProcessLetter(chars[c], c + 1);
output.Append(temp);
OnCharacterEnciphered(temp);
}
}
}
else
{
...
}
return output.ToString();
}
}
And the XAML page which calls this:
public sealed partial class Enigma1Page : Page
{
public Enigma1Device EnigmaDevice;
private void Page_Loaded(object sender, RoutedEventArgs e)
{
EnigmaDevice = new Enigma1Device();
EnigmaDevice.OnCharacterEnciphered += EnigmaDevice_OnCharacterEnciphered;
}
private void stdEnigmaKeyboard_OnHereIsSomeTextToEncipher(string plaintext)
{
txtOutput.Text = EnigmaDevice.TypeMessage(plaintext);
}
private void EnigmaDevice_OnCharacterEnciphered(char l)
{
txtStatus.Text = l.ToString();
}
}
Am I approaching this entirely wrong, or do I need to do the equivalent of textbox.Refresh() ? (I'd be surprised if I do, .Refresh() seems a little 1990's)
Ah...No, if you need to update TextBlock.Text
each time the value is changed, we recommend to use data binding and with the implementation of INotifyPropertyChanged interface. There are lots of samples you can search for them.
I have a user initiated operation where I perform an action on each item in a collection. I want to keep the user informed of progress as this happens.
I didn't find any of your posted code is related to a collection, I just wonder if you actually used ListView or GridView to show all the items in this collection, and you want change the text of TextBlock
in each ListViewItem
/ GridViewItem
. If so, you can refer to the official ListView and GridView sample to see how they work with collection and data binding.
Any way, what you need is Data binding in UWP, since I'm unsure with what you want to achieve, I didn't wrote a demo here, if you have any problem, you can leave a comment.
By the way, what you mean by "My UWP code works OK except that it only shows the 'status' of the last action, and not the preceding ones."?