独立显示/隐藏窗口的最佳方法

时间:2022-01-06 15:51:18

I am a student and am building a C# WPF application. It has three windows:

我是一名学生,正在构建一个C#WPF应用程序。它有三个窗口:

  1. Sign in window

    登录窗口

  2. Create account window

    创建帐户窗口

  3. and Main application window.

    和主应用程序窗口。

I uploaded a figure to show the type of navigation I am trying to implement:

我上传了一个图来显示我想要实现的导航类型:

独立显示/隐藏窗口的最佳方法

I do not think it is correct to make a window show up/hide inside the close/load event of another window.

我认为在另一个窗口的close / load事件中显示/隐藏窗口是不正确的。

  1. Can someone show me the right way to implement this navigation?

    有人能告诉我实施此导航的正确方法吗?

  2. Also, is it a good practice to make the three windows private properties of the application class?

    另外,使应用程序类的三个窗口私有属性是一个好习惯吗?

  3. The last window has a frame control to support page navigation. Again, is it better to make the three pages private properties of MainWindow application?

    最后一个窗口具有框架控件以支持页面导航。再次,使MainWindow应用程序的三个页面私有属性更好吗?

I am sorry if this is so obvious or easy to do. Thanks

如果这是如此明显或容易做到,我很抱歉。谢谢

4 个解决方案

#1


0  

I would not have the three windows as properties of the application. I'd instanciate a copy of the sign-in window and use that as my central point of control.

我不会将这三个窗口作为应用程序的属性。我将实例化登录窗口的副本并将其用作我的中心控制点。

When the user logs in, hide the sign in window, show a new main window and add a hook on the main windows Closed event.

当用户登录时,隐藏登录窗口,显示新的主窗口并在主窗口Closed事件上添加一个钩子。

e.g

if (logonSuccess)
{
    var mainWindow = new MainWindow();
    mainWindow.Closed += ReshowSignupWindow;
}

I'd also have the sign-in window do the same for the create account window. Thus, I'd have the create account window return to the signup window which would either reshow itself or start the main window if an account was created.

我还有登录窗口对创建帐户窗口执行相同操作。因此,我将创建帐户窗口返回到注册窗口,如果创建了帐户,该窗口将重新显示自身或启动主窗口。

e.g.:

// In sign-in window, handle the create window being closed
private void CreateWindowClosedHandler(object sender, EventArgs e)
{
    if (accountCreatedOK)
    {
        ShowMainWindow();
    }
    else
    {
        ReshowSignupWindow();
    }
}

I'd probably look at having the create account window shown as a dialog window via a call to ShowDialog().

我可能会看到通过调用ShowDialog()将创建帐户窗口显示为对话框窗口。

Hope that helps...

希望有帮助......

#2


0  

Something like this code might do it (untested, I just typed it in visual studio to autoformat the code) The XAML is for the Login Dialog. The RegistrationDialog should be similar, except for the button and handler for the registration Button.

像这样的代码可能会这样做(未经测试,我只是在visual studio中键入它来自动编码代码)XAML用于登录对话框。 RegistrationDialog应该类似,除了注册按钮的按钮和处理程序。

<Window x:Class="WpfApplication1.LoginWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="LoginWindow">
    <StackPanel>
        <Button IsDefault="True" Content="Submit" Click="SubmitButton_Click"/>
        <Button IsCancel="True" Content="Cancel" />
        <Button Content="CreateAccount" Click="CreateAccountButton_Click"/>
    </StackPanel>
</Window>

//Handler of LoginWindow and RegistrationWindow
private void SubmitButton_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = true;
}

//Handler of LoginWindow only
private void CreateAccountButton_Click(object sender, RoutedEventArgs e)
{
    this.IsCreatingAccount = true;
    this.DialogResult = false;
}

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        bool isCanceled;

        while (loginWin.ShowDialog() == false && !isCanceled)
        {
            if (loginWin.IsAccountCreationRequested)
            {
                if (registrationWin.ShowDialog())
                {
                    isCanceled = true;
                }
                else
                {
                    loginWin.IsAccountCreationRequested = false;
                }
            }
            else
            {
                isCanceled = true;
            }
        }

        if (loginWin.DialogResult) MainWindow.Show();
    }
}

#3


0  

I am currently working on a Silverlight Application which is more or less similar to your application. What i feel is your can have 2 xaml controls(one for Login and other for your main application). For create account, you can use a child window which will be called from login control. And use a TabControl in your main application which will hold your other 3 xaml controls(Page1.xaml, Page2.xaml and Page3.xaml). Feel free to ask if you have any issues.

我目前正在开发一个与您的应用程序或多或少类似的Silverlight应用程序。我觉得你可以有2个xaml控件(一个用于登录,另一个用于你的主应用程序)。对于create account,您可以使用将从登录控件调用的子窗口。并在主应用程序中使用TabControl,它将保存其他3个xaml控件(Page1.xaml,Page2.xaml和Page3.xaml)。如果您有任何问题,请随时询问。

Dont forget to mark my reply as answer if it solves your problem.

如果它能解决您的问题,请不要忘记将我的回复标记为答案。

#4


0  

I suggest you to follow the pattern. Your logic looks tightly binding with UI(user Interface) and logics.

我建议你按照这个模式。您的逻辑看起来与UI(用户界面)和逻辑紧密绑定。

The best pattern i like is for WPF or Silverlight is MVVM(Model View View Model). There are lot of Examples available in google for MVVM.

我喜欢的最佳模式是WPF或Silverlight是MVVM(模型视图模型)。在谷歌MVVM中有很多可用的例子。

Just put a glance in anyone MVVM example you will be clear in developing WPF or Silverlight app.

只需看一眼MVVM示例,您就可以清楚地开发WPF或Silverlight应用了。

some links are below, http://msdn.microsoft.com/en-us/magazine/dd419663.aspx http://www.c-sharpcorner.com/UploadFile/raj1979/simple-mvvm-pattern-in-wpf/ http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute

下面是一些链接,http://msdn.microsoft.com/en-us/magazine/dd419663.aspx http://www.c-sharpcorner.com/UploadFile/raj1979/simple-mvvm-pattern-in-wpf/ http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute

#1


0  

I would not have the three windows as properties of the application. I'd instanciate a copy of the sign-in window and use that as my central point of control.

我不会将这三个窗口作为应用程序的属性。我将实例化登录窗口的副本并将其用作我的中心控制点。

When the user logs in, hide the sign in window, show a new main window and add a hook on the main windows Closed event.

当用户登录时,隐藏登录窗口,显示新的主窗口并在主窗口Closed事件上添加一个钩子。

e.g

if (logonSuccess)
{
    var mainWindow = new MainWindow();
    mainWindow.Closed += ReshowSignupWindow;
}

I'd also have the sign-in window do the same for the create account window. Thus, I'd have the create account window return to the signup window which would either reshow itself or start the main window if an account was created.

我还有登录窗口对创建帐户窗口执行相同操作。因此,我将创建帐户窗口返回到注册窗口,如果创建了帐户,该窗口将重新显示自身或启动主窗口。

e.g.:

// In sign-in window, handle the create window being closed
private void CreateWindowClosedHandler(object sender, EventArgs e)
{
    if (accountCreatedOK)
    {
        ShowMainWindow();
    }
    else
    {
        ReshowSignupWindow();
    }
}

I'd probably look at having the create account window shown as a dialog window via a call to ShowDialog().

我可能会看到通过调用ShowDialog()将创建帐户窗口显示为对话框窗口。

Hope that helps...

希望有帮助......

#2


0  

Something like this code might do it (untested, I just typed it in visual studio to autoformat the code) The XAML is for the Login Dialog. The RegistrationDialog should be similar, except for the button and handler for the registration Button.

像这样的代码可能会这样做(未经测试,我只是在visual studio中键入它来自动编码代码)XAML用于登录对话框。 RegistrationDialog应该类似,除了注册按钮的按钮和处理程序。

<Window x:Class="WpfApplication1.LoginWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="LoginWindow">
    <StackPanel>
        <Button IsDefault="True" Content="Submit" Click="SubmitButton_Click"/>
        <Button IsCancel="True" Content="Cancel" />
        <Button Content="CreateAccount" Click="CreateAccountButton_Click"/>
    </StackPanel>
</Window>

//Handler of LoginWindow and RegistrationWindow
private void SubmitButton_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = true;
}

//Handler of LoginWindow only
private void CreateAccountButton_Click(object sender, RoutedEventArgs e)
{
    this.IsCreatingAccount = true;
    this.DialogResult = false;
}

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        bool isCanceled;

        while (loginWin.ShowDialog() == false && !isCanceled)
        {
            if (loginWin.IsAccountCreationRequested)
            {
                if (registrationWin.ShowDialog())
                {
                    isCanceled = true;
                }
                else
                {
                    loginWin.IsAccountCreationRequested = false;
                }
            }
            else
            {
                isCanceled = true;
            }
        }

        if (loginWin.DialogResult) MainWindow.Show();
    }
}

#3


0  

I am currently working on a Silverlight Application which is more or less similar to your application. What i feel is your can have 2 xaml controls(one for Login and other for your main application). For create account, you can use a child window which will be called from login control. And use a TabControl in your main application which will hold your other 3 xaml controls(Page1.xaml, Page2.xaml and Page3.xaml). Feel free to ask if you have any issues.

我目前正在开发一个与您的应用程序或多或少类似的Silverlight应用程序。我觉得你可以有2个xaml控件(一个用于登录,另一个用于你的主应用程序)。对于create account,您可以使用将从登录控件调用的子窗口。并在主应用程序中使用TabControl,它将保存其他3个xaml控件(Page1.xaml,Page2.xaml和Page3.xaml)。如果您有任何问题,请随时询问。

Dont forget to mark my reply as answer if it solves your problem.

如果它能解决您的问题,请不要忘记将我的回复标记为答案。

#4


0  

I suggest you to follow the pattern. Your logic looks tightly binding with UI(user Interface) and logics.

我建议你按照这个模式。您的逻辑看起来与UI(用户界面)和逻辑紧密绑定。

The best pattern i like is for WPF or Silverlight is MVVM(Model View View Model). There are lot of Examples available in google for MVVM.

我喜欢的最佳模式是WPF或Silverlight是MVVM(模型视图模型)。在谷歌MVVM中有很多可用的例子。

Just put a glance in anyone MVVM example you will be clear in developing WPF or Silverlight app.

只需看一眼MVVM示例,您就可以清楚地开发WPF或Silverlight应用了。

some links are below, http://msdn.microsoft.com/en-us/magazine/dd419663.aspx http://www.c-sharpcorner.com/UploadFile/raj1979/simple-mvvm-pattern-in-wpf/ http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute

下面是一些链接,http://msdn.microsoft.com/en-us/magazine/dd419663.aspx http://www.c-sharpcorner.com/UploadFile/raj1979/simple-mvvm-pattern-in-wpf/ http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute