Can't open new instance of another window in my app, in WPF .NET 8

62 Views Asked by At

Edit: thanks to anyone who tried to help me, I found the problem and the fix of it. The issue was because the name of the project I had was like this "3, Train_AdoNet_3", while the name of the project and the solution should not contain spaces, commas, or several other things, etc, while mine contained space and a comma, and this is what It led to the problem I had. I confirmed this and it worked after I tried changing the name in a new project, and created another project without doing so.

------------------------------------------------------------------

By clicking a button in WPF on .NET 8, I want to create a new instance of another window that I made to enter values and then use these values later. The problem here is that when I want to make an instance of that window in the C# background code of the main window MainWindow.xaml, and when I execute the app, and press that button, this error occurs

System.IO.FileLoadException: 'The given assembly name was invalid.'

This message comes from this code:

public Window_AddNew()
{
    InitializeComponent(); // the error was on this line code
}

By the way, this code is from the C# background code that belongs to the other window that I want to show up and make an instance of it, and it's name is Window_AddNew.xaml.

For the buttons code that it should open the other window Window_AddNew.xaml:

private void btn_new_Click(object sender, RoutedEventArgs e)
{
    Window_AddNew window_AddNew = new Window_AddNew();
    // Window window_AddNew = new Window_AddNew(); // I tried this also but there's no change
    window_AddNew.Show();
}

One more notice: I've noticed that the problem was happening while in the MainWindow.xaml C# code, this line was being executed

Window_AddNew window_AddNew = new Window_AddNew();

I've tried

Window window_AddNew = new Window_AddNew();

instead of

Window_AddNew window_AddNew = new Window_AddNew();

and I also tried this code

window_AddNew.ShowDialog();

instead of

window_AddNew.Show();

but there is no change about the problem.

This is the code in the MainWindow.xaml.cs:

using System.Text;
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.Navigation;
using System.Windows.Shapes;

using System.Data;
using Microsoft.Data.SqlClient;
using System.Configuration;

namespace _3__Train_AdoNet_3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btn_new_Click(object sender, RoutedEventArgs e)
        {
            Window_AddNew window_AddNew = new Window_AddNew();
            //Window window_AddNew = new Window_AddNew();
            //window_AddNew.Show();
            window_AddNew.ShowDialog();
        }
    }
}

And this is the code in Window_AddNew.xaml.cs:

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;

using System.Data;
using Microsoft.Data.SqlClient;
using System.Configuration;

namespace _3__Train_AdoNet_3
{
    /// <summary>
    /// Interaction logic for Window_AddNew.xaml
    /// </summary>
    public partial class Window_AddNew : Window
    {
        static string sqlConnString = ConfigurationManager.ConnectionStrings["sqlConnStr"].ConnectionString;
        SqlConnection sqlConn = new SqlConnection(sqlConnString);

        public Window_AddNew()
        {
            InitializeComponent();
        }

        private void cmb_ProgrammingLanguages_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            DataTable dt_ProgrammingLanguages = new DataTable();

            SqlDataAdapter sql_da_proglang = new SqlDataAdapter("sp_selectProgrammingLanguage", sqlConn);
            sql_da_proglang.Fill(dt_ProgrammingLanguages);

            cmb_ProgrammingLanguages.ItemsSource = dt_ProgrammingLanguages.DefaultView;
        }
    }
}

Edit:

more info:

xaml code of the "MainWindow.xaml":

<Window
    x:Class="_3__Train_AdoNet_3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:_3__Train_AdoNet_3"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    Background="#FF444444"
    mc:Ignorable="d">
    <Grid>
        <TextBlock
            Width="326"
            Height="36"
            Margin="0,0,464,388"
            HorizontalAlignment="Right"
            VerticalAlignment="Bottom"
            FontSize="20"
            FontWeight="DemiBold"
            Foreground="White"
            Text="Training App"
            TextAlignment="Center"
            TextWrapping="Wrap" />
        <TextBox
            x:Name="textbox_search"
            Width="332"
            Height="36"
            Margin="458,0,0,388"
            HorizontalAlignment="Left"
            VerticalAlignment="Bottom"
            HorizontalContentAlignment="Center"
            VerticalContentAlignment="Center"
            FontSize="19"
            FontWeight="DemiBold"
            Text="TextBox_search"
            TextWrapping="Wrap"
            VerticalScrollBarVisibility="Auto" />
        <Button
            x:Name="btn_new"
            Width="133"
            Height="40"
            Margin="0,384,657,0"
            HorizontalAlignment="Right"
            VerticalAlignment="Top"
            Content="New" Click="btn_new_Click" />
        <Button
            x:Name="btn_edit"
            Width="133"
            Height="40"
            Margin="0,384,506,0"
            HorizontalAlignment="Right"
            VerticalAlignment="Top"
            Content="Edit" />
        <Button
            x:Name="btn_delete"
            Width="133"
            Height="40"
            Margin="657,384,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Content="Delete" />
        <DataGrid
            x:Name="dataGrid_ViewData"
            Height="328"
            Margin="0,51,0,0"
            VerticalAlignment="Top"
            d:ItemsSource="{d:SampleData ItemCount=5}" />
    </Grid>
</Window>

and this is the code of the "Window_AddNew.xaml":

<Window
    x:Class="_3__Train_AdoNet_3.Window_AddNew"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:_3__Train_AdoNet_3"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="Window_AddNew"
    Width="544"
    Height="665"
    Background="#FF565656"
    ResizeMode="CanMinimize"
    mc:Ignorable="d">
    <Grid>
        <TextBlock
            Width="326"
            Height="36"
            Margin="0,10,0,0"
            HorizontalAlignment="Center"
            VerticalAlignment="Top"
            FontSize="20"
            FontWeight="DemiBold"
            Foreground="White"
            TextAlignment="Center"
            TextWrapping="Wrap"><Run Text="Add New" /><LineBreak /><Run /></TextBlock>
        <TextBox
            x:Name="textbox_code_id"
            Width="253"
            Height="36"
            Margin="14,0,0,532"
            HorizontalAlignment="Left"
            VerticalAlignment="Bottom"
            HorizontalContentAlignment="Center"
            VerticalContentAlignment="Center"
            FontSize="19"
            FontWeight="DemiBold"
            Text="TextBox_Code_Id"
            TextWrapping="Wrap"
            VerticalScrollBarVisibility="Auto" />
        <TextBlock
            Width="164"
            Height="28"
            Margin="14,0,0,570"
            Padding="5,0,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Bottom"
            FontSize="20"
            FontWeight="DemiBold"
            Foreground="White"
            TextAlignment="Left"
            TextWrapping="Wrap"><Run Text="Code Id" /><LineBreak /><Run /></TextBlock>
        <TextBox
            x:Name="textbox_NameOfTheError"
            Width="253"
            Height="36"
            Margin="14,0,0,461"
            HorizontalAlignment="Left"
            VerticalAlignment="Bottom"
            HorizontalContentAlignment="Center"
            VerticalContentAlignment="Center"
            FontSize="19"
            FontWeight="DemiBold"
            Text="TextBox_NameOfError"
            TextWrapping="Wrap"
            VerticalScrollBarVisibility="Auto" />
        <TextBlock
            Width="257"
            Height="28"
            Margin="14,0,0,499"
            Padding="5,0,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Bottom"
            FontSize="20"
            FontWeight="DemiBold"
            Foreground="White"
            Text="Name of the Error/Problem"
            TextAlignment="Left"
            TextWrapping="Wrap" />
        <TextBlock
            Width="237"
            Height="28"
            Margin="14,0,0,428"
            Padding="5,0,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Bottom"
            FontSize="20"
            FontWeight="DemiBold"
            Foreground="White"
            Text="Programming Language"
            TextAlignment="Left"
            TextWrapping="Wrap" />
        <ComboBox
            x:Name="cmb_ProgrammingLanguages"
            Width="253"
            Height="36"
            Margin="14,0,0,392"
            HorizontalAlignment="Left"
            VerticalAlignment="Bottom" ContextMenuOpening="cmb_ProgrammingLanguages_ContextMenuOpening" />
        <TextBox
            x:Name="textbox_Description"
            Width="516"
            Height="36"
            Margin="18,0,0,305"
            HorizontalAlignment="Left"
            VerticalAlignment="Bottom"
            HorizontalContentAlignment="Center"
            VerticalContentAlignment="Center"
            FontSize="19"
            FontWeight="DemiBold"
            Text="TextBox_Description"
            TextWrapping="Wrap"
            VerticalScrollBarVisibility="Auto" />
        <TextBlock
            Width="164"
            Height="28"
            Margin="18,0,0,346"
            Padding="5,0,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Bottom"
            FontSize="20"
            FontWeight="DemiBold"
            Foreground="White"
            Text="Description"
            TextAlignment="Left"
            TextWrapping="Wrap" />
        <TextBox
            x:Name="textbox_Error_Solving_Process"
            Width="516"
            Height="36"
            Margin="18,0,0,234"
            HorizontalAlignment="Left"
            VerticalAlignment="Bottom"
            HorizontalContentAlignment="Center"
            VerticalContentAlignment="Center"
            FontSize="19"
            FontWeight="DemiBold"
            Text="TextBox_Error_Solving_Process"
            TextWrapping="Wrap"
            VerticalScrollBarVisibility="Auto" />
        <TextBlock
            Width="319"
            Height="28"
            Margin="0,0,207,272"
            Padding="5,0,0,0"
            HorizontalAlignment="Right"
            VerticalAlignment="Bottom"
            FontSize="20"
            FontWeight="DemiBold"
            Foreground="White"
            Text="Error/Problem Solving Process"
            TextAlignment="Left"
            TextWrapping="Wrap" />
        <RichTextBox
            x:Name="richtextbox_Error_Solving_Process"
            Height="125"
            Margin="18,0,10,104"
            VerticalAlignment="Bottom">
            <FlowDocument>
                <Paragraph>
                    <Run Text="richtextbox_Error_Solving_Process" />
                </Paragraph>
            </FlowDocument>
        </RichTextBox>
        <Image
            Width="257"
            Height="197"
            Margin="276,0,0,401"
            HorizontalAlignment="Left"
            VerticalAlignment="Bottom" />
        <Button
            x:Name="btn_addwin_UploadImage"
            Width="187"
            Height="34"
            Margin="312,0,0,362"
            HorizontalAlignment="Left"
            VerticalAlignment="Bottom"
            Content="Upload Image" />
        <Button
            x:Name="btn_addwin_UploadImage_Copy"
            Width="187"
            Height="34"
            Margin="0,0,347,10"
            HorizontalAlignment="Right"
            VerticalAlignment="Bottom"
            Content="Upload Image" />
        <Button
            x:Name="btn_addwin_UploadImage_Copy1"
            Width="187"
            Height="34"
            Margin="0,0,10,10"
            HorizontalAlignment="Right"
            VerticalAlignment="Bottom"
            Content="Upload Image" />

    </Grid>
</Window>

i hope these info helps, thank you all.

0

There are 0 best solutions below