English 中文(简体)
如何打破 lo状,取决于 lo虫在运行时的检查值?
原标题:How to break out of a loop depending on a CheckBox Checked value while the loop is running?

请允许我说,如果用户选择一个称为“重复”的Box公司,那么我就想再说一个功能,但是,当用户 un弄Box时,对该功能的电话应当停止。

我利用支票、Click和Tap事件进行了审判,但一开始,检查箱就没有任何变化。

我甚至试图在另一个纽顿的“Click”方法内使用一个 lo子,但后来却在布顿的受压状态上制造了一个锁。

任何想法/替代建议?

最佳回答

Don t use a loop, use a timer andchange it on and offing on theeck Box state.

    // 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();
    }

Some notes about the callback ("MyTimerCallback"):

The method does not execute on the thread that created the timer; it executes on a ThreadPool thread that is supplied by the system. (Source: Documentation of Timer)

这一点很重要,告诉你不要从这种方法中直接接触任何情报和安全局要素。 相反的:

textBlock1.Dispatcher.BeginInvoke(() => { textBlock1.Text = "Changed text"; });
问题回答

Spinning in a loop on the UI thread is bad practice. Why not bind to a dependency property instead and then do what you need in the PropertyChangedCallback?

XAML

<!-- MainWindowInstance is the x:Name of this class, you can avoid setting the -->
<!-- ElementName if you setup a DataContext -->
<CheckBox Content="Repeat" IsChecked="{Binding ElementName=MainWindowInstance, Path=Checked, Mode=TwoWay}" />

法典背后

public bool Checked
{
   get { return (bool)GetValue(CheckedProperty); }
   set { SetValue(CheckedProperty, value); }
}

public static readonly DependencyProperty CheckedProperty = DependencyProperty.Register("Checked", typeof(bool), typeof(MainPage), new PropertyMetadata((s, e) =>
{
   if ((bool)e.NewValue)
   {
      // Checked
      MessageBox.Show("checkbox checked");
   }
   else
   {
      // Unchecked
   }
}));




相关问题
Silverlight Rich text box control

Our team decided that we need our own custom Rich text box control for Silverlight app we are developing. We looked at existing controls mentioned at A good rich text control for Silverlight but ...

Silverlight ImageBrush not rendering (with Bing Map Control)

I m trying to add an image to a Pushpin instance from the Silverlight Bing Map Control, but I can t seem to get it to render (the pushpin renders fine). This is probably a general WPF question rather ...

Silverlight OpenFileDialog DoEvents equivalent

I m processing large files after they are selected by the user. My code looks like the following: if (FileDialog.ShowDialog() == true) { // process really big file } This freezes up the UI so ...

list of controls with templates in silverlight

Does anyone know where to find a list of controls that you can set the template on in Silverlight? I ve wasted several hours now trying to create control templates only to find that the control doesn ...

Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Silverlight 3 - FindName returns null

This looks a bug to me.. Using Silverlight 3 and i have a user control defined in XAML and trying to access the object during runtime returns a null. <Grid> <common:CommonGridEditPanel x:...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

热门标签