简体   繁体   English

在.NET紧凑框架中切换表单(windows mobile 6)

[英]Switching forms in .NET compact framework (windows mobile 6)

I'm new to the .NET compact framework (and pretty much new to C# as well) and I've got a problem with switching forms in my mobile application. 我是.NET紧凑框架的新手(也是C#的新手),我在移动应用程序中切换表单时遇到了问题。 At a high level, my application makes use of multiple forms, with a main "application manager" class which performs the navigation/switching between the forms. 在高级别,我的应用程序使用多个表单,主要的“应用程序管理器”类执行表单之间的导航/切换。 My plan was to create forms on demand, cache them and use a simple hide/show strategy. 我的计划是按需创建表单,缓存它们并使用简单的隐藏/显示策略。

At first I wanted to do something like the following in my main application class: 起初我想在我的主应用程序类中执行以下操作:

public void switchForm(Form newForm)
{
    currentForm.Hide(); // instance member
    newForm.Show();
    currentForm = newForm;
}

However, this did not work out as planned. 但是,这并没有按计划进行。 The new form that I was attempting to show, appeared very temporarily and then disappeared behind the main form of my application - any ideas as to why this happens? 我试图展示的新表格非常暂时出现,然后消失在我的应用程序的主要形式后面 - 任何想法为什么会发生这种情况? I've done quite a bit of reading on form switching, and most places seemed to mention that the above method was the way to do it. 我已经完成了很多关于表单切换的阅读,大多数地方似乎都提到上面的方法就是这样做的。

I then tried out the following: 然后我尝试了以下内容:

public void switchForm(Form newForm)
{
    currentForm.Hide(); // instance member
    currentForm = newForm;
    newForm.ShowDialog();
}

and this seems to do the trick. 这似乎可以解决问题。 I'm wondering, however, is this the right way to go about it? 不过,我想知道这是正确的方法吗? Using the ShowDialog() method, however, poses another problem. 但是,使用ShowDialog()方法会带来另一个问题。 Say I want to dispose of the old form, like so: 假设我要处理旧表单,如下所示:

public void switchForm(Form newForm)
{
    Form oldForm = currentForm;
    currentForm = newForm;
    newForm.ShowDialog();
    oldForm.Dispose();
}

I then found out that the oldForm.Dispose() code does not execute until the newForm.ShowDialog() completes (the form is closed). 然后我发现oldForm.Dispose()代码在newForm.ShowDialog()完成(窗体关闭)之前不会执行。 Therefore, the above can lead to leaks with the forms not being disposed correctly as the method is called over and over again while moving to new forms. 因此,在移动到新形式的同时反复调用该方法时,上述情况可能导致泄漏,形式没有正确设置。 Another way is to dispose the old form first before showing the new one, however, then my application temporarily renders something else (whatever was behind the forms) between the old form being disposed and the new one being rendered :/ How should one go about doing something like this? 另一种方法是在显示新表单之前首先处理旧表单,但是,然后我的应用程序暂时在正在处理的旧表单和正在呈现的新表单之间呈现其他内容(表单后面的任何内容):/应如何处理做这样的事情?

Thanks very much. 非常感谢。

Try this : 试试这个

public void switchForm(Form newForm) 
{ 
    Form oldForm = currentForm
    newForm.Show(); 
    currentForm.Hide();
    currentForm = newForm;
    oldForm.Dispose(); 
} 

Every form you create in the project is a class. 您在项目中创建的每个表单都是一个类。
For example: 例如:
I create a form called frmExample , If you want to call it from another form you have to do the following: 我创建了一个名为frmExample的表单,如果要从另一个表单中调用它,则必须执行以下操作:
-Create a new instance of the class (your form) you need - 创建所需的类(您的表单)的新实例
-Call the new instance (modal or non modal) - 新实例(模态或非模态)

Class frmOther<br>
    Dim frmNewForm as New frmExample()<br>
    frmNewForm.Show()<br>
End Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM