简体   繁体   中英

C#, WPF, Having trouble opening a new window

I've looked through the list of similar questions for this topic and couldn't find any examples which deal with my particular problem.

I'd like to start with the disclaimer that I'm not far off an absolute beginner.

Opening up a window in WPF is quite easy, I've done it before in a previous project and it worked fine.

However, I am struggling to do it again in this new project (login form). I have two classes, MainWindow and CreateAccount.

MainWindow has the event trigger for opening up the CreateAccount window.

private void Button_Click(object sender, RoutedEventArgs e)
    {

      var account = new CreateAccount(); 
        account.Show();
        this.Close(); 
    }

Researching how to open up a new window in WPF gave me snippets very much like this one.

What I want to happen is for, upon triggering the button event, the window I've designed with an account creation form to appear. What instead happens is a blank window pops up with what I can only assume are default dimensions and no border text.

I don't understand how this can be as I specified exactly what I wanted. I don't get any errors either.

The CreateAccount class is mostly just some if statements (I don't want to hunker down with it until I sort out the current issue) and I can't find anything that would cause issue.

Both classes inherit from Window. I took a guess at what might be wrong, thinking 'maybe its an inheritance problem' and so tried to make CreateAccount inherit from MainWindow, but that threw an error which I now understand. Right now I'm lost as to what the problem is and since I don't know that, I can't find out the solution.

Is there anything wrong with the code? Someone suggested that it might be a DataContext issue, but even after looking that up I'm struggling to understand it.

Thank you.

EDIT: Because a lot of people were asking for more code with CreateAccount.xaml.cs(I thought we were only allowed to post snippets):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Login
{
/// <summary>
/// Interaction logic for CreateAccount.xaml
/// </summary>

public partial class CreateAccount : Window
{


    public bool canProceedPass = false;
    public bool canProceedUser = false; 
    public void MakeAccount()
    {

        InitializeComponent();
    }

    public void CheckTextInput()
    {
        if (NewUsername.Text != null && NewPassword.Text != null) {

            canProceedUser = true; 
        }
        else
        {
            canProceedUser = false;
            MessageBox.Show("You haven't filled out all the required  fields.");

        }

    }

    public void CheckPassInput()
    {
        if (NewPassword.Text == ConfirmNewPassword.Text)
        {

            canProceedPass = true;

        }else
        {

            return; 

        }

    }

    private void CreateAccountButton_Click(object sender, RoutedEventArgs e)
    {
        if (canProceedUser == true && canProceedPass == true)
        { 

            //Add username and password to my SqlDb. 

        }
    }
  }
}

A constructor should have the same name as your Class with no return type, in your example the constructor should be:

public CreateAccount()
{
   InitializeComponent();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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