这段代码中的错误是什么?视觉工作室的错误是“预期的 [英] what is the error in this code? the Error in visual studio was " } expected "
本文介绍了这段代码中的错误是什么?视觉工作室的错误是“预期的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
命名空间 timer33
{
/ // < 摘要 >
/// 交互逻辑for MainWindow.xaml
/// < ; / summary >
public partial class MainWindow:Window
{
public MainWindow()
{
InitializeComponent();
}
public static 类 DelayedExecutionService
{
public static void DelayedExecute(操作操作, int delay = 3 )
{
var dispatcherTimer = new System.Windows.Threading.DispatcherTimer( );
EventHandler handler = null ;
handler =(sender,e)= >
{
// 停止计时器,使其不会每隔X秒执行一次
// 并且还避免将处理程序保留在内存中。
dispatcherTimer.Tick - = handler;
dispatcherTimer.Stop();
// 执行操作。
action();
};
dispatcherTimer.Tick + = handler;
dispatcherTimer.Interval = TimeSpan.FromSeconds(delay);
private void button1_Click( object sender,RoutedEventArgs e)
{
var dispatcherTimer = new System.Windows .Threading.DispatcherTimer();
dispatcherTimer.Start();
DelayedExecutionService.DelayedExecute(()= >
MessageBox.Show( Hello!));
}
}
}
}
解决方案
< blockquote>简单:你不能嵌套方法:你的button1_Click处理程序在你的DelayedExecute方法体内,这是不允许的。
namespace timer33
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static class DelayedExecutionService
{
public static void DelayedExecute(Action action, int delay = 3)
{
var dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
EventHandler handler = null;
handler = (sender, e) =>
{
// Stop the timer so it won't keep executing every X seconds
// and also avoid keeping the handler in memory.
dispatcherTimer.Tick -= handler;
dispatcherTimer.Stop();
// Perform the action.
action();
};
dispatcherTimer.Tick += handler;
dispatcherTimer.Interval = TimeSpan.FromSeconds(delay);
private void button1_Click(object sender, RoutedEventArgs e)
{
var dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Start();
DelayedExecutionService.DelayedExecute(() =>
MessageBox.Show("Hello!"));
}
}
}
}
解决方案
Simple: you can't nest methods: Your button1_Click handler is inside the body of your DelayedExecute method, which is not allowed.
这篇关于这段代码中的错误是什么?视觉工作室的错误是“预期的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文