按住按钮时执行操作,释放按钮时执行另一个操作
Perform Action When Button Is Held And Another Action When Button Is Released
我正在使用 visual studio 创建一个 Windows IoT 应用程序。在应用程序的 GUI 上有一个按钮,当通过单击鼠标或按下某个键来按住该按钮时,它会执行一个操作,在这种情况下,文本框会更改其文本,而电机是 运行。释放按钮时,文本框会更改其文本并且电机会停止。
我在 .xml 文件中创建了一个按钮,并在 .cs 中创建了 button_holding 事件,但这并没有执行任何操作。
你可以使用Holding事件来获得类似传统WinForms中的MouseUp和MouseDown,像这样
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
}
}
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.
可以使用PointerPressedEvent和PointerReleasedEvent来实现。这是适合我的代码:
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";
}
我正在使用 visual studio 创建一个 Windows IoT 应用程序。在应用程序的 GUI 上有一个按钮,当通过单击鼠标或按下某个键来按住该按钮时,它会执行一个操作,在这种情况下,文本框会更改其文本,而电机是 运行。释放按钮时,文本框会更改其文本并且电机会停止。
我在 .xml 文件中创建了一个按钮,并在 .cs 中创建了 button_holding 事件,但这并没有执行任何操作。
你可以使用Holding事件来获得类似传统WinForms中的MouseUp和MouseDown,像这样
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
}
}
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.
可以使用PointerPressedEvent和PointerReleasedEvent来实现。这是适合我的代码:
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";
}