Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I trying to create WPF Application. Everything worked well before I tried to switch the project to .NET 5.0 . Then an error appears at startup: "Program does not contain a static 'Main' method suitable for an entry point”. I have VS 2019. I have the main method as the main window. I have three windows.
First:
C#
using System.Windows;


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

        

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (scitani.IsChecked.HasValue && scitani.IsChecked.Value)
            {
                uroven lvl = new uroven();
                lvl.Show();
                this.Close();
            }
            else if (odcitani.IsChecked.HasValue && odcitani.IsChecked.Value)
            {
                uroven lvl = new uroven();
                lvl.Show();
                this.Close();
            }
            else if (nasobeni.IsChecked.HasValue && nasobeni.IsChecked.Value)
            {
                uroven lvl = new uroven();
                lvl.Show();
                this.Close();
            }
            else if (deleni.IsChecked.HasValue && deleni.IsChecked.Value)
            {
                uroven lvl = new uroven();
                lvl.Show();
                this.Close();
            }
            else
            {
                MessageBox.Show("Musíš zvolit, jednu z početních operací!");
                
            }
        }
        
    }
}


Second:
C#
using System.Windows;


namespace MathGame
{
    /// <summary>
    /// Interakční logika pro uroven.xaml
    /// </summary>
    public partial class uroven : Window
    {
        public uroven()
        {
            InitializeComponent();
        }

        private void start_Click(object sender, RoutedEventArgs e)
        {
            if (nizka.IsChecked.HasValue && nizka.IsChecked.Value)
            {
                hra game = new hra();
                game.Show();
                this.Close();
            }
            else if (vysoka.IsChecked.HasValue && vysoka.IsChecked.Value)
            {
                hra game = new hra();
                game.Show();
                this.Close();
            }
            else 
            {
                MessageBox.Show("Musíš zvolit, jednu z úrovní!");
            }

        }
    }
}


Third:
C#
using System;

using System.Diagnostics;
using System.IO;
using System.Linq;

using System.Windows;

using System.Windows.Threading;

namespace MathGame
{
    /// <summary>
    /// Interakční logika pro hra.xaml
    /// </summary>
    public partial class hra : Window
    {
        Stopwatch mt = new Stopwatch();
        DispatcherTimer posunTimer = new DispatcherTimer();
        decimal trvani = new decimal();
        int suma = new int();
        
        public hra()
        {
            InitializeComponent();
            
            posunTimer.Tick += posunTimer_Tick;
            posunTimer.Interval = TimeSpan.FromSeconds(1);
            
        }
        
        public void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Main window1 = Application.Current.Windows.OfType<Main>().First();
            var scitani = window1.scitani ;
            var odcitani = window1.odcitani;
            var nasobeni = window1.nasobeni;
            vysledekhrac.Focus();
            suma++;
            pocetprikladu.Text = suma.ToString();
            mt.Start();
            timer.Value = 0;
            posunTimer.Start();
            Random prvni = new Random();
            Random druhy = new Random();
            int maxprvni = 10;
            int maxdruhy = 10;
            int prvnic = prvni.Next(1, maxprvni);
            int druhyc = druhy.Next(2, maxdruhy);
            if (scitani.IsChecked.HasValue && scitani.IsChecked.Value)
            {
                znamenko.Text = "-";

            }
            if (odcitani.IsChecked.HasValue && odcitani.IsChecked.Value)
            {
                znamenko.Text = "-";

            }
            if (nasobeni.IsChecked.HasValue && nasobeni.IsChecked.Value)
            {
                znamenko.Text = "*";

            }
            if (znamenko.Text=="+")
            {
                int total = (prvnic+druhyc);
                prvnit.Text = prvnic.ToString();
                druhyt.Text = druhyc.ToString();
                vysledek.Text = total.ToString();

            }
            if (znamenko.Text == "-")
            {
                int total = (prvnic - druhyc);
                prvnit.Text = prvnic.ToString();
                druhyt.Text = druhyc.ToString();
                vysledek.Text = total.ToString();

            }
            if (znamenko.Text == "*")
            {
                int total = (prvnic * druhyc);
                prvnit.Text = prvnic.ToString();
                druhyt.Text = druhyc.ToString();
                vysledek.Text = total.ToString();

            }
            

        }

        private void posunTimer_Tick(object sender, object e)
        {
            timer.Value += 1;
            if (timer.Value >= timer.Maximum)

            {
                posunTimer.Stop();
                hlaska.Text = "Špatně";
                poch++;
                suma++;
                pocetchyb.Text = poch.ToString();
                pocetprikladu.Text = suma.ToString();
                timer.Value = 0;
                posunTimer.Start();
                Random prvni = new Random();
                Random druhy = new Random();
                int maxprvni = 10;
                int maxdruhy = 10;
                int prvnic = prvni.Next(1, maxprvni);
                int druhyc = druhy.Next(2, maxdruhy);
                if (znamenko.Text == "+")
                {
                    int total = (prvnic + druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }
                if (znamenko.Text == "-")
                {
                    int total = (prvnic - druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }
                if (znamenko.Text == "*")
                {
                    int total = (prvnic * druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }
                
               
                vysledekhrac.Text = String.Empty;

                if (poch == 4)
                {
                    mt.Stop();
                    timer.Value = 0;
                    posunTimer.Stop();
                    MessageBox.Show("Konec hry! Čas vypršel. ");
                    TimeSpan trvani = TimeSpan.FromMilliseconds(mt.ElapsedMilliseconds);
                    MessageBox.Show("Tvůj čas je " + trvani.ToString("mm\\:ss\\.ff")+"\n" + "Počet příkladů:" + pocetprikladu.Text + "\n" + "Průměrný čas:" + trvani / suma);
                    if (!File.Exists(path))
                    {
                        StreamWriter str = File.CreateText(path);
                        str.WriteLine(trvani);
                    }
                    else if (File.Exists(path))
                    {
                        var str = new StreamWriter(path);
                        str.WriteLine(trvani);
                    }
                    this.Close();
                }
            }
        }

        
        private int poch;

        private void kontrola_Click(object sender, RoutedEventArgs e)
        {
            
            timer.Value += 1;
            posunTimer.Start();
            if (vysledekhrac.Text == vysledek.Text )
            {
                posunTimer.Stop();
                hlaska.Text = "Správně";
                suma++;
                pocetprikladu.Text = suma.ToString();
                Random prvni = new Random();
                Random druhy = new Random();
                int maxprvni = 10;
                int maxdruhy = 10;
                int prvnic = prvni.Next(1, maxprvni);
                int druhyc = druhy.Next(2, maxdruhy);
                if (znamenko.Text == "+")
                {
                    int total = (prvnic + druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }
                if (znamenko.Text == "-")
                {
                    int total = (prvnic - druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }
                if (znamenko.Text == "*")
                {
                    int total = (prvnic * druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }
                
                vysledekhrac.Text = String.Empty;
                timer.Value = 0;
                posunTimer.Start();
            }
            
            else
            {
                posunTimer.Stop();
                hlaska.Text = "Špatně";
                poch++;
                suma++;
                pocetchyb.Text = poch.ToString();
                pocetprikladu.Text = suma.ToString();
                Random prvni = new Random();
                Random druhy = new Random();
                int maxprvni = 10;
                int maxdruhy = 10;
                int prvnic = prvni.Next(1, maxprvni);
                int druhyc = druhy.Next(2, maxdruhy);
                if (znamenko.Text == "+")
                {
                    int total = (prvnic + druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }
                if (znamenko.Text == "-")
                {
                    int total = (prvnic - druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }
                if (znamenko.Text == "*")
                {
                    int total = (prvnic * druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }
                
                vysledekhrac.Text = String.Empty;
                timer.Value = 0;
                posunTimer.Start();
                if (poch == 4)
                {
                    mt.Stop();
                    timer.Value = 0;
                    posunTimer.Stop();
                    MessageBox.Show("Konec hry!");
                    konec_Click(sender, e);

                }
            }
      
        }
        string path = @"C:\\Žebříček.txt";
        private void konec_Click(object sender, RoutedEventArgs e)
        {
            timer.Value = 0;
            posunTimer.Stop();
            if (mt.IsRunning==true)
            {
                mt.Stop();
            }
            TimeSpan trvani = TimeSpan.FromMilliseconds(mt.ElapsedMilliseconds);
            MessageBox.Show("Tvůj čas je "+ trvani.ToString("mm\\:ss\\.ff")+"\n"+ "Počet příkladů: " +pocetprikladu.Text+"\n"+"Průměrný čas: "+Math.Round(trvani.TotalSeconds/suma,4) + " sekund");
            if (!File.Exists(path))
            {
                StreamWriter str = File.CreateText(path);
                str.WriteLine(trvani);
            }
            else if (File.Exists(path))
            {
                var str = new StreamWriter(path);
                str.WriteLine(trvani);
            }
                
            this.Close();
        }
    }
}


What I have tried:

I apologize for the combination of multiple languages. Perhaps it is visible even without xaml.

Thank you for advice.
Posted
Updated 22-Apr-21 0:56am
Comments
Richard MacCutchan 22-Apr-21 5:16am    
Do what the message tells you, and add a proper main method in the class that controls the application.
dejf111 22-Apr-21 5:18am    
but I have the main method there
Richard MacCutchan 22-Apr-21 5:27am    
You have:
public Main()

which is not
public static main()

as mentioned in the message.
dejf111 22-Apr-21 5:35am    
when I rewrite it, even more errors arise
dejf111 22-Apr-21 6:01am    
I thought this was needed in the input window :
public partial class MainWindow : Window
    {
        
        
            public  MainWindow()
        {
                InitializeComponent();

            }

1 solution

This is a Windows application, not a console - start again, this time selecting a WPF project instead of a console. That will provide you with a "basics" from which a full GUI application is supported.

You only supply a main method in Console based apps, it's handled for you and the start up code is different in WPF ones.
 
Share this answer
 
v2
Comments
dejf111 22-Apr-21 7:20am    
you were right i had to recreate it, thank you!
OriginalGriff 22-Apr-21 7:41am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900