How do I switch between windows in UI?

How do I change windows with the UI. For testing, I have 2 windows and swapping works but the Click handler for the button fires the UP state more than one.

Touch down
Touch up
Touch up
Touch up
Touch up
Touch up

This is the code I call in the first screen for the button click handler. This fires a delegate to inform the main code that it should switch windows.

private void CancelButton_Click(object sender, RoutedEventArgs e)
{
    //
    // Send signal back to calling function that cancel was pressed
    //
    OnCancelButtonPressed(this);
}

Then in Program.cs I have this handler to do the swapping.

private static void CookingTimesWindow_cancelButtonPressed(CookingTimesWindow sender)
{
    WpfWindow.Background = new SolidColorBrush(Colors.Blue);
    WpfWindow.Child = cookingWindow.Elements;
    WpfWindow.Invalidate();
}

These 2 functions get called multiple times due to the extra Touch up events.

What am I doing wrong? I checked the car wash sample and could not see any calls to prevent this.

I am actually getting multiple ups for each button press, for all buttons. This multiple up only seems to occur when I press buttons. For other elements, I only see a Touch down and Touch up in the debug output from the touch handler functions.

private static void Touch_TouchUp(FT5xx6Controller sender, GHIElectronics.TinyCLR.Drivers.FocalTech.FT5xx6.TouchEventArgs e)
{
    Application.Current.InputProvider.RaiseTouch(e.X, e.Y, TouchMessages.Up, DateTime.UtcNow);
    Debug.WriteLine("Touch up");
}

private static void Touch_TouchDown(FT5xx6Controller sender, GHIElectronics.TinyCLR.Drivers.FocalTech.FT5xx6.TouchEventArgs e)
{
    Application.Current.InputProvider.RaiseTouch(e.X, e.Y, TouchMessages.Down, DateTime.UtcNow);
    Debug.WriteLine("Touch down");
}

Anyone else seeing multiple clicks? I am using the Focaltech FT5xxx driver.

Yes, I recall having this issue, and having to filter for that, but now I can’t find where I did that in my code… Still searching.

Looks like I just pay attention to a touch-up if there was a preceding ‘down’. If and ‘up’ comes after an ‘up’ it will get ignored. ‘touchState’ is the interesting flag here. HaveActivity is my screensaver hold-off.

private void Touch_TouchUp(
    FT5xx6Controller sender, 
    GHIElectronics.TinyCLR.Drivers.FocalTech.FT5xx6.TouchEventArgs e)`
{
    if (this.ScreenIsActive)
    {
        if (this.touchState)
        {
            this.app.InputProvider.RaiseTouch(e.X, e.Y, TouchMessages.Up, DateTime.UtcNow);
            this.touchState = false;
        }
    }
    this.HaveActivity();
}

private void Touch_TouchMove(
   FT5xx6Controller sender, 
   GHIElectronics.TinyCLR.Drivers.FocalTech.FT5xx6.TouchEventArgs e)
{
    if (this.ScreenIsActive)
    {
        this.app.InputProvider.RaiseTouch(e.X, e.Y, TouchMessages.Move, DateTime.UtcNow);
    }
    this.HaveActivity();
}

private void Touch_TouchDown(
    FT5xx6Controller sender, 
    GHIElectronics.TinyCLR.Drivers.FocalTech.FT5xx6.TouchEventArgs e)
{
    if (this.ScreenIsActive)
    {
        if (!this.touchState)
        {
            this.app.InputProvider.RaiseTouch(e.X, e.Y, TouchMessages.Down, DateTime.UtcNow);
            this.touchState = true;
        }
    }
    this.HaveActivity();
}

I added the touchState and it is no longer showing multiple touch up but it is still passing the touch to the next window that opens so the cancel button in my video is still showing as if it was pressed and the new window closes because of this when I press cancel on the new window. It should only go back 1 level but is going back 2. This also happens on the SCM20260D dev board with the 4.3" LCD that I reported in another post.

Are you sure that it’s a wayward event? The touchState flag should be false and still short-circuit the up’ event. I have also noticed that exceptions during Click handlers will cause a button to stay in the pressed state (and in fact cause most controls to have problems). I opened an issue for that and I have a partial fix. This happens because the controls have visual state actions that occur after the delegate call, and those get skipped if any delegate handler throws.

I do have a full UWP/Android-like page-based navigation system that I am happy to share. I have been meaning to publish it as a nuget but had a few other more profitable fish to fry first.

UPDATE:
The issue is here : Exceptions in event handlers leave the UI corrupted · Issue #498 · ghi-electronics/TinyCLR-Libraries · GitHub

1 Like

I also just watched the video. I would recommend triggering actions on the ‘up’ event, not the ‘down’ event. That’s more consistent with other UI platforms (windows, UWP, Android, iOS, etc).

The reason for using UP is so that the user can cancel by dragging out of the control and then raising their finger. The other reason is that you might pick up a false ‘down’ or ‘move’ on the next page if the page transitions before the user raises their finger.

1 Like

Thanks Martin.

I changed the event for the buttons to TouchUp but the events never fire. I tried with TouchDown and same, no event fires. only Click works but sends for down and also up.

I’ve removed the Click event in the UI.Button control from the down state and only fire this for the UP state and now there is no action on the subsequent window that appears. I can now move between windows and buttons no longer generate 2 click events or pass the state to the next window that opens.

1 Like

I put back the click event and there is a variable that can be checked for UP or DOWN state in the click event that fires. This works. It just needs to be documented. :slight_smile:

    if (e.RoutedEvent.Name.Equals("TouchUpEvent"))
    {
    }
4 Likes

Persistence pays!!

2 Likes