これは私がWPFでしたトリックです。これはSilverlightで使用するために移植され、Dispacherクラスからハングします。私はSL5で "Invoke"メソッドが見つからないため、Mauriceの答えについてはわかりません。私はBeginInvokeを見ています。これは遅れた行動に関しては無駄です。
使用法:コードファイルにSystem.Windows名前空間を含める必要があります。この拡張メソッドは表示されません。
// lets say you want to enable a control 1 second after a save event
// lets say you just want to prevent click happy users from going crazy
// This code assumes you disabled the button on the click event
Button b = this.idButton1;
b.Dispatcher.DelayInvoke(TimeSpan.FromSeconds(1),() => { b.Enabled = true; });
それです。ちょうど1行のコードがこのトリックを行います。以下は上記のコードを可能にする拡張クラスです。
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;
namespace System.Windows {
public static class DispatcherExtensions {
public static void DelayInvoke<TArg1, TArg2, TArg3>(this Dispatcher dispatcher, TimeSpan delay, Action<TArg1, TArg2, TArg3> action, TArg1 arg1, TArg2 arg2, TArg3 arg3) {
dispatcher.DelayInvoke(delay, (Delegate)action, arg1, arg2, arg3);
}
public static void DelayInvoke<TArg1, TArg2>(this Dispatcher dispatcher, TimeSpan delay, Action<TArg1, TArg2> action, TArg1 arg1, TArg2 arg2) {
dispatcher.DelayInvoke(delay, (Delegate)action, arg1, arg2);
}
public static void DelayInvoke<TArg1>(this Dispatcher dispatcher, TimeSpan delay, Action<TArg1> action, TArg1 arg1) {
dispatcher.DelayInvoke(delay, (Delegate)action, arg1);
}
public static void DelayInvoke(this Dispatcher dispatcher, TimeSpan delay, Action action) {
dispatcher.DelayInvoke(delay, (Delegate)action);
}
public static void DelayInvoke(this Dispatcher dispatcher, TimeSpan delay, Delegate del, params object[] args) {
if (dispatcher == null)
throw new NullReferenceException();
if (delay < TimeSpan.Zero)
throw new ArgumentOutOfRangeException("delay");
if (del == null)
throw new ArgumentNullException("del");
var task = new Task(() => { Thread.Sleep(delay); });
task.ContinueWith((t) => { dispatcher.BeginInvoke(del, args); });
task.Start();
}
}
}
このご意見ありがとうございました。スレッド句の代わりに、私は "Action = new Action(delayedRemovePopup); PopUp.Dispatcher.BeginInvoke(a);"を使用しました。それはそれをすべて解決しました。 :-) 本当にありがとう! –
あなたは一時変数を必要としません。単にPopup.Dispatcher.BeginInvoke(delayedRemovePopup)を呼び出してください。 – chakrit