Troubleshooting xaml - initialize window

<Window x:Class="Othello.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Othello Game" Height="75" Width="500">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<StackPanel Orientation="Horizontal" Margin="10" HorizontalAlignment="Center">
<Button Content="Human vs Human" Width="150" Margin="5" Click="HumanVsHuman_Click"/>
<Button Content="Human vs Computer" Width="150" Margin="5" Click="HumanVsComputer_Click"/>
<Button Content="Computer vs Computer" Width="150" Margin="5" Click="ComputerVsComputer_Click"/>
</StackPanel>

<Grid Name="GameBoard" Grid.Row="1" Margin="10"
HorizontalAlignment="Center" VerticalAlignment="Center">
</Grid>
</Grid>
</Window>
<Window x:Class="Othello.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Othello Game" Height="75" Width="500">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<StackPanel Orientation="Horizontal" Margin="10" HorizontalAlignment="Center">
<Button Content="Human vs Human" Width="150" Margin="5" Click="HumanVsHuman_Click"/>
<Button Content="Human vs Computer" Width="150" Margin="5" Click="HumanVsComputer_Click"/>
<Button Content="Computer vs Computer" Width="150" Margin="5" Click="ComputerVsComputer_Click"/>
</StackPanel>

<Grid Name="GameBoard" Grid.Row="1" Margin="10"
HorizontalAlignment="Center" VerticalAlignment="Center">
</Grid>
</Grid>
</Window>
No description
86 Replies
Merineth πŸ‡ΈπŸ‡ͺ
What am i missing? Why can't it initializeComponent?
leowest
leowestβ€’5w ago
build your solution if it builds its just $vsdrunk
MODiX
MODiXβ€’5w ago
* close VS * remove the hidden folder .vs * remove all bin and obj folder next to each csproj (DO NOT TOUCH THE .git FOLDER OR WHAT'S INSIDE) * restart vs
leowest
leowestβ€’5w ago
also is that wpf?winui3? or what
Merineth πŸ‡ΈπŸ‡ͺ
I have no idea what those are I did manage to fix it but i'm unsure about
leowest
leowestβ€’5w ago
what kind of project u create
Merineth πŸ‡ΈπŸ‡ͺ
<Window x:Class="Othello.MainWindow"
<Window x:Class="Othello.MainWindow"
What should be written here if my .cs file is called SetupGameDialog.xaml.cs` Othello game
leowest
leowestβ€’5w ago
no double click your project in the solution explorer then paste it here and we will know what kind of project it is
Merineth πŸ‡ΈπŸ‡ͺ
Doubleclick my project?
leowest
leowestβ€’5w ago
ok so yeah that is wrong indeed
Merineth πŸ‡ΈπŸ‡ͺ
Severity Code Description Project File Line Suppression State Error (active) CS1061 'SetupGameDialog' does not contain a definition for 'HumanVsHuman_Click' and no accessible extension method 'HumanVsHuman_Click' accepting a first argument of type 'SetupGameDialog' could be found (are you missing a using directive or an assembly reference?) Assignment2 C:\Users\aljom\Documents\Plugg Programmering filer\OOP\Assignment2\Assignment2Repository\Assignment2\Assignment2\View\SetupGameDialog.xaml 12
leowest
leowestβ€’5w ago
should have been Othello.View.MainWindow because u moved it
leowest
leowestβ€’5w ago
No description
leowest
leowestβ€’5w ago
you double click that it opens the csproj and with the contents of it u can tell what kind of projec it is
Merineth πŸ‡ΈπŸ‡ͺ
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>

</Project>
leowest
leowestβ€’5w ago
ok so its WPF πŸ™‚ 'SetupGameDialog' does not contain a definition for 'HumanVsHuman_Click' you have a click event in your xaml but not in the .xaml.cs file
Merineth πŸ‡ΈπŸ‡ͺ
using System.Windows;

namespace Othello
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void HumanVsHuman_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Human vs Human button clicked!");
}

private void HumanVsComputer_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("HumanVsComputer_Click");
}

private void ComputerVsComputer_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("ComputerVsComputer_Click");
}
}
}
using System.Windows;

namespace Othello
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void HumanVsHuman_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Human vs Human button clicked!");
}

private void HumanVsComputer_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("HumanVsComputer_Click");
}

private void ComputerVsComputer_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("ComputerVsComputer_Click");
}
}
}
I did define it Is it because my namespace is wrong?
leowest
leowestβ€’5w ago
yes Othello.View is what u had you only have 1 MainWindow.xaml and MainWindow.xaml.cs correct?
Merineth πŸ‡ΈπŸ‡ͺ
Yes i think so
Merineth πŸ‡ΈπŸ‡ͺ
I've made definitions for the buttons and changed the namespace
leowest
leowestβ€’5w ago
ok you changed the file name but not the class name hehe its ok so yeah they should both have the .View once u change that do a build to ensure its ok or what other errors u have
Merineth πŸ‡ΈπŸ‡ͺ
I'm not sure i understand. I changed the namespace to Assignment2.view Is it
public MainWindow()
{
InitializeComponent();
}
public MainWindow()
{
InitializeComponent();
}
That's the problem? aaaah
public partial class SetupGameDialog : Window
{
public SetupGameDialog()
{
InitializeComponent();
}
public partial class SetupGameDialog : Window
{
public SetupGameDialog()
{
InitializeComponent();
}
That seems to solve it
leowest
leowestβ€’5w ago
u might have changed something esle too glad u figured out πŸ˜‰
Merineth πŸ‡ΈπŸ‡ͺ
Do you know if it's possible to move xaml files? like i want WinnerDialog.xaml to be ontop, then DrawDialog etc drag and drop doesn't seem to work lol
leowest
leowestβ€’5w ago
yes it is but if u want to also have the right structure u would have to rename the namespace drag and drop should work yes
Merineth πŸ‡ΈπŸ‡ͺ
I'm a little bit stuck
using System.Windows;

namespace Assignment2.View
{
public partial class SetupGameDialog : Window
{
public SetupGameDialog()
{
InitializeComponent();
}

private void HumanVsHuman_Click(object sender, RoutedEventArgs e)
{

}

private void HumanVsComputer_Click(object sender, RoutedEventArgs e)
{

}

private void ComputerVsComputer_Click(object sender, RoutedEventArgs e)
{

}
}
}
using System.Windows;

namespace Assignment2.View
{
public partial class SetupGameDialog : Window
{
public SetupGameDialog()
{
InitializeComponent();
}

private void HumanVsHuman_Click(object sender, RoutedEventArgs e)
{

}

private void HumanVsComputer_Click(object sender, RoutedEventArgs e)
{

}

private void ComputerVsComputer_Click(object sender, RoutedEventArgs e)
{

}
}
}
So this is the code behind my three buttons. They aren't implemented yet However when the user presses the HumanVsHuman button i want it to start a game
leowest
leowestβ€’5w ago
sure then u would have to write the logic for that to happen what part of that are u stuck with
Merineth πŸ‡ΈπŸ‡ͺ
Honestly this assignment is so complicated that i'm even unsure on what i'm unsure about
Merineth πŸ‡ΈπŸ‡ͺ
These 3 are what i'm trying to make right now I already make the 5.1 SetUpGameDialog fk i realiezd i don't take their names
leowest
leowestβ€’5w ago
ok so SetupGameDialog should be a window with two textbox and either a checkbox under each textbox indication if its a computer or not, or a combobox and a button as I understood so you fill in the name on textbox 1 then you fill in the name on textbox 2 then u click the checkbox if player 1 or 2 are computer then u hit the button so for now I would just implement that and try to print the output when u click the button then u work out how it passes it back to the GameWindow
Merineth πŸ‡ΈπŸ‡ͺ
damn so there is no comptuer vs computer good But right so when they click human vs human a new window comes up? and asks for names
leowest
leowestβ€’5w ago
there is just 1 button not 3 so you click start or register or something it would go back to the previous window GameWindow is apparently the main window that should open from what is written there as setupgamedialog is a child of it
Merineth πŸ‡ΈπŸ‡ͺ
πŸ˜•
Merineth πŸ‡ΈπŸ‡ͺ
This is what i had in mind? Human vs Human -> enter names -> confirm window
leowest
leowestβ€’5w ago
pretty close now have a checkbox that says "Is Computer" for example and if check then its a computer if not a player
Merineth πŸ‡ΈπŸ‡ͺ
:harold:
leowest
leowestβ€’5w ago
self explanatory you're trying to give value to a dialog result before its create
Merineth πŸ‡ΈπŸ‡ͺ
I don't get it
leowest
leowestβ€’5w ago
what you don't get, show code etc might be easier for me to explain
Merineth πŸ‡ΈπŸ‡ͺ
When i press ok the window has already been up
namespace Assignment2.View
{
public partial class SetUpGameDialog : Window
{
// Properties to hold the player names and computer flag
public string Player1Name { get; private set; }
public string Player2Name { get; private set; }
public bool IsPlayer2Computer { get; private set; }

public SetUpGameDialog()
{
InitializeComponent();
}

// Event handler for OK button click
private void OKButton_Click(object sender, RoutedEventArgs e)
{
// Capture the player names and computer flag
Player1Name = Player1NameTextBox.Text;

// Check if Player 2 is a computer
IsPlayer2Computer = IsPlayer2ComputerCheckBox.IsChecked == true;

// If Player 2 is a computer, automatically set the name to "Computer"
if (IsPlayer2Computer)
{
Player2Name = "Computer";
}
else
{
// Otherwise, get Player 2's name from the TextBox
Player2Name = Player2NameTextBox.Text;
}

// Validation: Ensure both player names are provided (except Player 2 if it's a computer)
if (string.IsNullOrEmpty(Player1Name) || (string.IsNullOrEmpty(Player2Name) && !IsPlayer2Computer))
{
MessageBox.Show("Please enter names for both players.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}

// Set the DialogResult to true and close the dialog if everything is valid
this.DialogResult = true;
this.Close();
}


// Event handler for Cancel button click
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false; // Indicate that the dialog was canceled
this.Close();
}
namespace Assignment2.View
{
public partial class SetUpGameDialog : Window
{
// Properties to hold the player names and computer flag
public string Player1Name { get; private set; }
public string Player2Name { get; private set; }
public bool IsPlayer2Computer { get; private set; }

public SetUpGameDialog()
{
InitializeComponent();
}

// Event handler for OK button click
private void OKButton_Click(object sender, RoutedEventArgs e)
{
// Capture the player names and computer flag
Player1Name = Player1NameTextBox.Text;

// Check if Player 2 is a computer
IsPlayer2Computer = IsPlayer2ComputerCheckBox.IsChecked == true;

// If Player 2 is a computer, automatically set the name to "Computer"
if (IsPlayer2Computer)
{
Player2Name = "Computer";
}
else
{
// Otherwise, get Player 2's name from the TextBox
Player2Name = Player2NameTextBox.Text;
}

// Validation: Ensure both player names are provided (except Player 2 if it's a computer)
if (string.IsNullOrEmpty(Player1Name) || (string.IsNullOrEmpty(Player2Name) && !IsPlayer2Computer))
{
MessageBox.Show("Please enter names for both players.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}

// Set the DialogResult to true and close the dialog if everything is valid
this.DialogResult = true;
this.Close();
}


// Event handler for Cancel button click
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false; // Indicate that the dialog was canceled
this.Close();
}
// Event handler for when the checkbox is checked
private void IsPlayer2ComputerCheckBox_Checked(object sender, RoutedEventArgs e)
{
// If Player 2 is a computer, automatically set Player 2's name to "Computer"
Player2NameTextBox.IsEnabled = false; // Disable Player 2's text box
Player2NameTextBox.Text = "Computer"; // Set the name to "Computer"
}

// Event handler for when the checkbox is unchecked
private void IsPlayer2ComputerCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
// Enable Player 2's text box for manual input
Player2NameTextBox.IsEnabled = true;
Player2NameTextBox.Clear(); // Clear the name field
}
}
}
}
// Event handler for when the checkbox is checked
private void IsPlayer2ComputerCheckBox_Checked(object sender, RoutedEventArgs e)
{
// If Player 2 is a computer, automatically set Player 2's name to "Computer"
Player2NameTextBox.IsEnabled = false; // Disable Player 2's text box
Player2NameTextBox.Text = "Computer"; // Set the name to "Computer"
}

// Event handler for when the checkbox is unchecked
private void IsPlayer2ComputerCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
// Enable Player 2's text box for manual input
Player2NameTextBox.IsEnabled = true;
Player2NameTextBox.Clear(); // Clear the name field
}
}
}
}
`
leowest
leowestβ€’5w ago
IsPlayer2Computer = IsPlayer2ComputerCheckBox.IsChecked == true;
IsPlayer2Computer = IsPlayer2ComputerCheckBox.IsChecked == true;
You can resume that to
IsPlayer2Computer = IsPlayer2ComputerCheckBox.IsChecked;
IsPlayer2Computer = IsPlayer2ComputerCheckBox.IsChecked;
IsChecked already returns true or false
Merineth πŸ‡ΈπŸ‡ͺ
As far as i can see it looks fine?
leowest
leowestβ€’5w ago
looks fine yes
Merineth πŸ‡ΈπŸ‡ͺ
Can't implicitly convert type bool? to bool
leowest
leowestβ€’5w ago
you can 1 extra } at the end
Merineth πŸ‡ΈπŸ‡ͺ
IsPlayer2Computer = (bool)IsPlayer2ComputerCheckBox.IsChecked;
IsPlayer2Computer = (bool)IsPlayer2ComputerCheckBox.IsChecked;
This seemed to fix it however i'm still getting the same error?
leowest
leowestβ€’5w ago
ah its nullable I see ok, just IsChecked.Value anyway so the problem is when u click ok it does no close?
Merineth πŸ‡ΈπŸ‡ͺ
exactly
leowest
leowestβ€’5w ago
what u do here is you set a breakpoint then u walk thru the method to see what is wrong
leowest
leowestβ€’5w ago
No description
leowest
leowestβ€’5w ago
you see the red circle? you go to the line where u have Player1Name = Player1NameTextBox.Text; and click on the gray are near the number and it will set that red circle then you run your project and fill in the information and press the OK then it will stop at that line then you press slowly F11 and it will walk down your code
Merineth πŸ‡ΈπŸ‡ͺ
Player1Name "a" string yeah it does get set and player2 name gets set to computer
leowest
leowestβ€’5w ago
yes then u check everything and see where its skipping
Merineth πŸ‡ΈπŸ‡ͺ
It's not skipping anywhere? Both player1 and player2 name gets set appropriately
leowest
leowestβ€’5w ago
ok it reached the result? so it should close does it reach the this.Close() if u keep hitting F11?
Merineth πŸ‡ΈπŸ‡ͺ
No. We get the exception on this.DialogResult = true;
leowest
leowestβ€’5w ago
what exception?
Merineth πŸ‡ΈπŸ‡ͺ
I clicked the button "OK" so it should then close same exception
Merineth πŸ‡ΈπŸ‡ͺ
the window has been created and shown as a dialog
leowest
leowestβ€’5w ago
weird
Merineth πŸ‡ΈπŸ‡ͺ
son idk why it's casting an exception
leowest
leowestβ€’5w ago
click view details and its pointing at the DialogResult from that files? SetupGameDialog?
Merineth πŸ‡ΈπŸ‡ͺ
Can't i just remove it? seems to work lol
leowest
leowestβ€’5w ago
technically yes you can but you lose the ability to tell from the ShowDialog result if it worked or not
Merineth πŸ‡ΈπŸ‡ͺ
I only care to finish the assignment xd
leowest
leowestβ€’5w ago
for example
var result = setup.ShowDialog();
if (result == true)
{
// I got the values and can do something with it
}
var result = setup.ShowDialog();
if (result == true)
{
// I got the values and can do something with it
}
sure can u show me this thou I did like to see why its not working for u
Merineth πŸ‡ΈπŸ‡ͺ
i'm never gonna finish this teachers are psychotic for making an assignment this hard
leowest
leowestβ€’5w ago
its not hard you're just overthinking it πŸ™‚ like u were thinking u had to do a computer vs computer that u needed 3 buttons
leowest
leowestβ€’5w ago
Merineth πŸ‡ΈπŸ‡ͺ
Well the setupgamewindow works
leowest
leowestβ€’5w ago
you're luck that your material explains and even tells u what to use to make the stuff some teachers are way more vague than that
Merineth πŸ‡ΈπŸ‡ͺ
ok i got a problem now because i removed that line
private void NewGameButton_Click(object sender, RoutedEventArgs e)
{
// Show the SetUpGameDialog to collect player names and types
SetUpGameDialog setUpDialog = new SetUpGameDialog();
bool? result = setUpDialog.ShowDialog(); // Show dialog and wait for result

if (result == true) // If OK was clicked (DialogResult = true)
{
// Retrieve player names and player types
string player1Name = setUpDialog.Player1Name;
string player2Name = setUpDialog.Player2Name;
bool isPlayer2Computer = setUpDialog.IsPlayer2Computer;

// Update the status section with the player names
Player1Label.Text = "Player 1: " + player1Name;
Player2Label.Text = "Player 2: " + player2Name;

// You can set the scores or other initial data
Player1Score.Text = "Black Disks: 0";
Player2Score.Text = "White Disks: 0";

// Initialize the game with the provided player data
// For example:
// GameManager.StartGame(player1Name, player2Name, isPlayer2Computer);
}
else
{
// If the user cancels the setup
MessageBox.Show("Game setup was canceled.", "Canceled");
}
}
private void NewGameButton_Click(object sender, RoutedEventArgs e)
{
// Show the SetUpGameDialog to collect player names and types
SetUpGameDialog setUpDialog = new SetUpGameDialog();
bool? result = setUpDialog.ShowDialog(); // Show dialog and wait for result

if (result == true) // If OK was clicked (DialogResult = true)
{
// Retrieve player names and player types
string player1Name = setUpDialog.Player1Name;
string player2Name = setUpDialog.Player2Name;
bool isPlayer2Computer = setUpDialog.IsPlayer2Computer;

// Update the status section with the player names
Player1Label.Text = "Player 1: " + player1Name;
Player2Label.Text = "Player 2: " + player2Name;

// You can set the scores or other initial data
Player1Score.Text = "Black Disks: 0";
Player2Score.Text = "White Disks: 0";

// Initialize the game with the provided player data
// For example:
// GameManager.StartGame(player1Name, player2Name, isPlayer2Computer);
}
else
{
// If the user cancels the setup
MessageBox.Show("Game setup was canceled.", "Canceled");
}
}
It's not returning true form the setup game dialog ok i added it back and it WORKS?! :aaaa:
leowest
leowestβ€’5w ago
hahahaha I thou so that was such a random issue
Merineth πŸ‡ΈπŸ‡ͺ
for sure
Merineth πŸ‡ΈπŸ‡ͺ
it does add the names at the bottom now
MODiX
MODiXβ€’5w ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server