WPF Windows窗体出现后运行程序 [英] Run program after wpf windows form appears

查看:54
本文介绍了WPF Windows窗体出现后运行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我对wpf(c#)项目有疑问.这是我的来源

Hello i have problem with wpf (c#) project. this is my source

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            MessageBox.Show("Test");
        }
    }
}

我想运行我的程序

MessageBox.Show("Test");

出现Windows窗体后...但是在我启动程序时的这段代码中,仅首先在消息框中显示测试",然后出现Windows窗体!我应该如何处理第一个Windows窗体,然后打开一个消息框以显示(测试)?我正在使用Visual Studio 2015(WPF)项目

after windows form appears ... but in this code when i start the program, just first show Test in message box and after that windows form appears ! what should i do to first windows form appears and after that a message box open to show the (Test)? i am using visual studio 2015 (WPF) project

推荐答案

您应在 Window_Load 事件中编写代码:

You should write your code in the Window_Load event:

public MainWindow()
{
    InitializeComponent();
    Loaded += MainWindow_Loaded;
}

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Test");
}

编辑:要使用更长的操作(如(使用一个函数,其中包含10个以上的函数),您可以使用

To work with longer operations like (used a function with more than 10 function inside as you wanted) you could use ThreadPool like this:

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 = ""; 
        }));
    });
}

这篇关于WPF Windows窗体出现后运行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆