【问题标题】:Dialogbox doesn't show orginal form对话框不显示原始形式
【发布时间】:2015-01-29 15:14:51
【问题描述】:

我有一个正在开发的 Windows Mobile 6.5 应用程序。当用户打开应用程序时,会出现一个带有登录表单的对话框。用户登录,然后在 30 秒后(生产中的一小段时间),当计时器在没有活动的情况下用完时,我使用事件再次显示登录对话框:

static private void _TimerTick(object state)
    {
        // the user has been inactive for 30 secs; log him out
        MainForm.timer = null;
        using (LoginForm LoginForm = new LoginForm())
        {

            if (LoginForm.ShowDialog() == DialogResult.OK)
            {
               MainForm.timer = new System.Threading.Timer(_TimerTick, null, 1000 * 30 * 1, Timeout.Infinite);
            }
            else
            {
                Application.Exit();
            }
        } 


    }

但是一旦我按下登录并从登录表单返回一个确定按钮,原始表单就会显示。虽然它还在任务管理器中。我试过了: .TopMost =真;但是我无法评估应用程序底部栏中的 windows 按钮,并且没有其他应用程序可以运行,因为我的应用程序中的表单总是在它前面。

【问题讨论】:

  • 很抱歉,我的描述不清楚。你能更详细地解释一下吗?只要有对表单的引用,Compact Framework 就不会处理表单。只要应用程序创建了正在运行的线程,它也不会关闭应用程序。
  • 由于某种原因,当对话框关闭时,程序被隐藏了。它进入手机最后打开的程序或主屏幕。该程序仍在任务管理器中,但在我切换回任务之前不可见。我玩过并更改了一些代码,但现在不行!奇怪的!无论如何,谢谢!
  • 事实上,我认为是表单继承问题。对话框继承与原始表单相同的表单类。所以这可能是问题所在!
  • 好的,我又遇到了同样的问题!所以也许我没有解决这个问题。我有一个名为:userActivityEvent 的事件,每次有人单击按钮或执行某些操作时,计时器都会重置,当计时器用完时,就会调用一个函数来注销。收到响应后,对话框完美运行,关闭并按预期显示原始表单。但是我的主表单上有一个注销按钮,我调用了相同的函数并显示了对话框,但是一旦我返回“确定”,原始表单就不可见了。它仍在任务管理器中,但我必须手动切换到它才能使其可见。
  • 看起来您需要管理表单堆栈并跟踪和控制何时显示/激活哪个表单。

标签: c# windows mobile dialog windows-mobile-6.5


【解决方案1】:

一个简单的解决方案,因为这仅适用于登录和一个主要表单:

LoginForm.cs(带有两个文本框和带登录和退出的主菜单):

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LoginFormTest
{

    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        public void doShow(bool bShow)
        {
            if(bShow)
                Invoke(new Action(() => this.Show()));
            else
                Invoke(new Action(() => this.Hide()));
        }

        public void doClose()
        {
            Invoke(new Action(() => this.Close()));
        }

        private void mnuLogin_Click(object sender, EventArgs e)
        {
            MainForm mainForm = new MainForm();
            mainForm.Show();
            System.Diagnostics.Debug.WriteLine("mainForm started");
        }

    }
}

那里没什么特别的。 MainForm 的代码会在没有活动时关闭表单:

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;

namespace LoginFormTest
{
    public partial class MainForm : Form
    {
        int countDown = 3;  //number of seconds for timeout

        System.Threading.Timer timer;
        object lockCounter = new object(); //to sync access to counter var

        public MainForm()
        {
            InitializeComponent();
            //start a independent timer after 1000ms and with a 1000ms interval
            timer = new System.Threading.Timer(new TimerCallback(this.timerCallback), null, 1000, 1000);
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            doClose();
        }

        private void mnuLogout_Click(object sender, EventArgs e)
        {
            doClose();
        }

        private void doClose()
        {
            System.Diagnostics.Debug.WriteLine("mainForm closing");
            try
            {
                timer.Dispose(); //else timer thread will continue running!
                Invoke(new Action(() => this.Close()));
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception in doClose(): " + ex.Message);
            }
        }

        private void MainForm_MouseMove(object sender, MouseEventArgs e)
        {
            resetTimeout();
        }

        private void MainForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            resetTimeout();
        }

        private void MainForm_Click(object sender, EventArgs e)
        {
            resetTimeout();
        }

        public void resetTimeout()
        {
            System.Diagnostics.Debug.WriteLine("resetTimeout()");
            lock(lockCounter)
                countDown = 3;
        }

        public void timerCallback(object stateInfo)
        {
            lock (lockCounter)
                countDown--;
            if (countDown == 0)
            {
                System.Diagnostics.Debug.WriteLine("timeout->doClose()");
                doClose();
            }
        }

        private void MainForm_Closed(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("mainForm CLOSED");
        }

    }
}

特价:

  • 用于同步访问计数器变量的锁定对象
  • 独立于消息泵运行的线程计时器
  • 从 TimerCallback 函数调用的委托

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多