Reputation: 3
I am using visual studios to create a Windows IoT app. On the GUI of the app there is a button that when held down with a mouse click or a key being pressed it performs an action, in this case a text box changes its text and a motor is run. When the button is released the text box changes its text and the motor is stopped.
I created a button in the .xml file and button_holding event in the .cs, but this doesn't perform any of the actions.
Upvotes: 0
Views: 213
Reputation: 9700
On the GUI of the app there is a button that when held down with a mouse click or a key being pressed it performs an action, in this case a text box changes its text and a motor is run. When the button is released the text box changes its text and the motor is stopped.
You can use PointerPressedEvent and PointerReleasedEvent to achieve it. This is the code works for me:
public MainPage()
{
this.InitializeComponent();
myButton.AddHandler(PointerPressedEvent, new PointerEventHandler(myButton_PointerPressed), true);
myButton.AddHandler(PointerReleasedEvent, new PointerEventHandler(myButton_PointerReleased), true);
}
private void myButton_PointerPressed(object sender, PointerRoutedEventArgs e)
{
myText.Text = "Running...";
}
private void myButton_PointerReleased(object sender, PointerRoutedEventArgs e)
{
myText.Text = "Stopped";
}
Upvotes: 1
Reputation: 328
You can use Holding event to get similar MouseUp and MouseDown in traditional WinForms, like so
private void Button_Holding(object sender, HoldingRoutedEventArgs e)
{
if (e.HoldingState == Windows.UI.Input.HoldingState.Started)
{
//do things when button on hold
}
else if (e.HoldingState == Windows.UI.Input.HoldingState.Completed ||
e.HoldingState == Windows.UI.Input.HoldingState.Canceled)
{
//do things when button release
}
}
Upvotes: 0