出现wpf windows窗体后运行程序
您好我有wpf(c#)项目的问题。 这是我的来源
namespace WpfApplication1 { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); MessageBox.Show("Test"); } } }
我想运行我的程序
MessageBox.Show("Test");
在Windows窗体出现之后……但是在我启动程序的代码中,只需先在消息框中显示Test,然后出现该窗体! 我应该怎么做第一个窗体窗体,然后打开一个消息框来显示(测试)? 我正在使用visual studio 2015(WPF)项目
您应该在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 = ""; })); }); }