Snippets for advanced controls UWP

I am preparing a new app, and contains complex controls to make user things easy (you know more simple user interface more complex coding). Today 2 snippets:

The features of the first control, a multiple state selector are:

  • Have at least one option always selected.
  • Know what is the latest selection.
  • Just be able to select one.
private void RegisterFrequencyChanges()
{
	CurrentFrequency = frequencies.Last();
	foreach (var frequency in frequencies)
	{
		frequency.PropertyChanged += Frequency_PropertyChanged;
	}
}

private async void Frequency_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
var f = sender as Frequency;

if (e.PropertyName == nameof(f.On))
{
	if (f.On)
	{
		CurrentFrequency = null;
		foreach (var frequency in Frequencies)
		{
			if (frequency != f)
			{
				frequency.On = false;
			}
		}
		CurrentFrequency = f;
	}
	else
	{
		if(f == CurrentFrequency)
		{
			await Task.Delay(60);
			f.On = true;
		}
	}
}
}

The clue here when is raised NotifyPropertyChanged event is the call to await Task.Delay(60); if you do not wait a bit it won’t be reflected in the View.

And the second snippet. An issue I was able to solve is to keep a visual state even the cursor it is outside the window and then when it is released even outside the window, the app get’s the result and goes back to the ‘normal’ state:

protected async override void OnPointerExited(PointerRoutedEventArgs e)
{
	base.OnPointerExited(e);

	while(e.GetCurrentPoint(this).IsInContact)
	{
		await Task.Delay(100);
	}

	VisualStateManager.GoToState(this, "Normal", true);
}

From now on, I will post in this blog instead of the expediteapps blog

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s