我正在挂钩usercontrol(RegisterClassHandler)的所有事件,因为在我的上下文(Excel加载项)中我需要使用" en-US"在代码背后的文化,然后在事件处理后恢复当地文化。
我知道添加一个处理程序是一个链式机制(即一旦我的处理程序代码完成,将调用前一个处理程序的代码)。
我怎样才能打破这个链并在结束前调用前一个处理程序而不再触发事件?
例如,如果我在2 Thread.CurrentThead.CurrentCulture
:Dispatcher.Invoke(new Action<object, RoutedEventArgs>(NewHandler), sender, e);
之间使用,则会一次又一次地触发事件,直到我收到堆栈溢出异常。
这是代码
public class CultureEnUsUserControl : UserControl
{
private readonly CultureInfo _previousCulture;
public CultureEnUsUserControl()
{
Loaded += SafeCultureEnUsUserControlLoaded;
_previousCulture = Thread.CurrentThread.CurrentCulture;
}
void SafeCultureEnUsUserControlLoaded(object sender, RoutedEventArgs e)
{
var events = EventManager.GetRoutedEvents();
foreach (var routedEvent in events)
EventManager.RegisterClassHandler(typeof(CultureEnUsUserControl), routedEvent, new RoutedEventHandler(NewHandler));
}
internal void NewHandler(object sender, RoutedEventArgs e)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
//Here I want to call the previous event handler without triggering again the event
Thread.CurrentThread.CurrentCulture = _previousCulture;
}
}