Silverlight 循环运行时,如何根据复选框选中的值中断循环?
假设如果用户选择了一个名为“repeat”的复选框,我想重复调用函数,但是一旦用户“取消选中”该复选框,对函数的调用就会停止 我尝试使用复选框的“Checked”、“Click”和“Tap”事件,但一旦循环开始,它就感觉不到复选框状态的任何更改 我甚至尝试在另一个按钮的点击方法中使用一个循环,但这会在按钮的“按下”状态上创建一个锁Silverlight 循环运行时,如何根据复选框选中的值中断循环?,silverlight,windows-phone-7,Silverlight,Windows Phone 7,假设如果用户选择了一个名为“repeat”的复选框,我想重复调用函数,但是一旦用户“取消选中”该复选框,对函数的调用就会停止 我尝试使用复选框的“Checked”、“Click”和“Tap”事件,但一旦循环开始,它就感觉不到复选框状态的任何更改 我甚至尝试在另一个按钮的点击方法中使用一个循环,但这会在按钮的“按下”状态上创建一个锁 有什么想法/替代建议吗?在UI线程上循环旋转是一种糟糕的做法。为什么不绑定到依赖项属性,然后执行属性ChangedCallback中所需的操作 XAML 不要使用循环
有什么想法/替代建议吗?在UI线程上循环旋转是一种糟糕的做法。为什么不绑定到依赖项属性,然后执行
属性ChangedCallback
中所需的操作
XAML
不要使用循环,请使用
计时器
,并根据复选框状态将其打开和关闭
// your loop replacement: a timer
Timer timer;
// this method is called periodically by the timer - do whatever you want here
// but make sure you use proper dispatching when accessing the UI (!)
private void MyTimerCallback(object state)
{
System.Diagnostics.Debug.WriteLine("Action!");
}
// this creates and starts the timer
private void StartTimer()
{
// set the timer to call your function every 500ms
timer = new Timer(MyTimerCallback, null, 500, 500);
}
// stop the timer
private void StopTimer()
{
timer.Dispose();
}
// Checked handler for the checkbox: start the timer
private void checkBox1_Checked(object sender, RoutedEventArgs e)
{
StartTimer();
}
// Unchecked handler for the checkbox: stop the timer
private void checkBox1_Unchecked(object sender, RoutedEventArgs e)
{
StopTimer();
}
关于回调(“MyTimerCallback”)的一些注释:
该方法不会在创建计时器的线程上执行;信息技术
在系统提供的线程池线程上执行。
(来源:计时器的文档)
这很重要,告诉您不要直接从这个方法访问任何UI元素。改为这样做:
textBlock1.Dispatcher.BeginInvoke(() => { textBlock1.Text = "Changed text"; });
对不起,我是个新手。如何准确使用OnPropertyChanged?例如,如果每次选中复选框时我都需要打印一个消息框,上面写着“checkbox checked”,反之亦然,“unchecked”,那么我该如何实现呢?在XAML中,我将checkbox.IsChecked属性绑定到代码中的checked属性,它只是依赖属性的包装。请参阅DependencyProperty.Register()调用。PropertyMetadata构造函数接受回调,您可以在其中检查我的示例中也显示的属性值。我添加了XAML,然后添加了后端。但是,在运行时,由于某些原因,CheckedProperty changed事件不会发生。调试区域中没有消息。也许我还在做错事:(我测试了代码,代码运行正常。可能您没有正确使用ElementName。关键是要告诉您的复选框您绑定到的属性所在的位置。如果您使用MVVM,则DataContext是视图模型,路径只是属性名。如果您没有使用MVVM,则需要告诉您的复选框属性在UserControl本身上存在。我通过将x:Name MainWindow设置为“MainWindowInstance”,然后在binding的ElementName中引用它来实现。很好。一般的经验法则是:不要在主线程中创建任何忙循环,因为它们会阻塞用户界面。而是使用一些回调机制或基于线程的替代方法,如
计时器
。
// your loop replacement: a timer
Timer timer;
// this method is called periodically by the timer - do whatever you want here
// but make sure you use proper dispatching when accessing the UI (!)
private void MyTimerCallback(object state)
{
System.Diagnostics.Debug.WriteLine("Action!");
}
// this creates and starts the timer
private void StartTimer()
{
// set the timer to call your function every 500ms
timer = new Timer(MyTimerCallback, null, 500, 500);
}
// stop the timer
private void StopTimer()
{
timer.Dispose();
}
// Checked handler for the checkbox: start the timer
private void checkBox1_Checked(object sender, RoutedEventArgs e)
{
StartTimer();
}
// Unchecked handler for the checkbox: stop the timer
private void checkBox1_Unchecked(object sender, RoutedEventArgs e)
{
StopTimer();
}
textBlock1.Dispatcher.BeginInvoke(() => { textBlock1.Text = "Changed text"; });