您好我的wpf(c#)项目有问题。这是我的来源
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MessageBox.Show("Test");
}
}
}
我想运行我的程序
MessageBox.Show("Test");
在Windows窗体出现之后但是在我启动程序的代码中,只需先在消息框中显示Test,然后出现该窗体!我应该怎么做第一个窗体窗体,然后打开一个消息框来显示(测试)?我正在使用visual studio 2015(WPF)项目
答案 0 :(得分:1)
您应该在Window_Load
事件中编写代码:
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("Test");
}
编辑:要处理较长的操作,例如(根据需要在中使用了一个功能超过10的函数),您可以使用ThreadPool
,如下所示:
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
ThreadPool.QueueUserWorkItem(_ =>
{
//Longer Process (//set the operation in another thread so that the UI thread is kept responding)
Dispatcher.BeginInvoke(new Action(() =>
{
//use the Dispatcher to "return" to the UI thread
//To change the UI elements like label you should put them here like : label1.Text = "";
}));
});
}