按下按钮并将.exe文件放到另一台计算机上后,程序会崩溃

时间:2019-02-14 12:03:14

标签: c# wpf windows visual-studio

我正在研究一个项目,并编写了代码。这段代码在我的笔记本电脑上工作正常,但是当我尝试在其他台式机上设置.exe文件时,它将无法正常工作。按下按钮后,它刚刚崩溃并自行关闭。我该如何解决?

我尝试了几种想法。就像复制整个解决方案文件夹一样。我也尝试为该程序制作安装程序。我还尝试使用桌面上使用的正确框架。但是所有这些努力根本没有用。我已经检查了,在解决方案中的Resources文件夹中,所有图像都很好。对不起所有代码,也许对一个大代码来说是一个小错误。 (也许我正在使用什么系统可能是个问题)

MainWindow 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.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Drawing;

namespace Escapehaarlem
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    string hint = "";
    string verwijder = "";
    Window1 win1 = new Window1();


    bool FlagBeer1 = true;
    bool FlagBeer2 = true;
    bool FlagBeer3 = true;
    bool FlagBeer4 = true;
    bool FlagBeer5 = true;
    bool FlagBeer6 = true;

    int index = 1;


    public MainWindow()
    {
        InitializeComponent();
        if (System.Windows.Forms.Screen.AllScreens.Length <= index)
        {
            System.Windows.MessageBox.Show("Tweede scherm is niet aangesloten");
            System.Environment.Exit(1);
        }
        else
        {
            win1.WindowStartupLocation = WindowStartupLocation.Manual;
            System.Windows.Forms.Screen s = System.Windows.Forms.Screen.AllScreens[1];

            System.Drawing.Rectangle r = s.WorkingArea;
            win1.WindowState = System.Windows.WindowState.Normal;
            win1.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual;
            win1.Top = r.Top;
            win1.Left = r.Left;

            win1.Show();
            win1.WindowState = System.Windows.WindowState.Maximized;
        }

    }


    private void Verstuur_Click(object sender, RoutedEventArgs e)
    {
        hint = Textbox.Text;
        win1.UpdateLabel(hint);
        win1.achtergrondfoto();

        if (FlagBeer1 && FlagBeer2 && FlagBeer3 && FlagBeer4 && FlagBeer5 && FlagBeer6)
        {
            FlagBeer1 = false;
            win1.RemoveImage();
        }
        else if (FlagBeer2 && FlagBeer3 && FlagBeer4 && FlagBeer5 && FlagBeer6)
        {
            FlagBeer2 = false;
            win1.RemoveImage();
        }
        else if (FlagBeer3 && FlagBeer4 && FlagBeer5 && FlagBeer6)
        {
            FlagBeer3 = false;
            win1.RemoveImage();
        }
        else if (FlagBeer4 && FlagBeer5 && FlagBeer6)
        {
            FlagBeer4 = false;
            win1.RemoveImage();
        }
        else if (FlagBeer5 && FlagBeer6)
        {
            FlagBeer5 = false;
            win1.RemoveImage();
        }
        else if (FlagBeer6)
        {
            FlagBeer6 = false;
            win1.RemoveImage();
        }

    }



    private void Verwijder_Click(object sender, RoutedEventArgs e)
    {
        Textbox.Text = verwijder;
        win1.UpdateLabel(verwijder);
        win1.Achtergrondfotoplaatsten();
    }

    private void Herstel_Click(object sender, RoutedEventArgs e)
    {           

            if (FlagBeer6 == false && FlagBeer5 == false && FlagBeer4 == false && FlagBeer3 == false && FlagBeer2 == false && FlagBeer1 == false)
            {
                FlagBeer6 = true;
                win1.HerstelImage();
            }
            else if (FlagBeer5 == false && FlagBeer4 == false && FlagBeer3 == false && FlagBeer2 == false && FlagBeer1 == false)
            {
                FlagBeer5 = true;
                win1.HerstelImage();
            }
            else if (FlagBeer4 == false && FlagBeer3 == false && FlagBeer2 == false && FlagBeer1 == false)
            {
                FlagBeer4 = true;
                win1.HerstelImage();                    
            }
            else if (FlagBeer3 == false && FlagBeer2 == false && FlagBeer1 == false)
            {
                FlagBeer3 = true;
                win1.HerstelImage();
            }
            else if (FlagBeer2 == false && FlagBeer1 == false)
            {
                FlagBeer2 = true;
                win1.HerstelImage();
            }
            else if (FlagBeer1 == false)
            {
                FlagBeer1 = true;
                win1.HerstelImage();
            }         

    }
    protected override void OnClosed(EventArgs e)
    {
        base.OnClosed(e);

        App.Current.Shutdown();
    }
}
}

第二个窗口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.Windows.Forms;
using System.Timers;
using System.Threading;
using Timer = System.Threading.Timer;


namespace Escapehaarlem
{
public partial class Window1 : Window
{

    bool FlagBeer1 = true;
    bool FlagBeer2 = true;
    bool FlagBeer3 = true;
    bool FlagBeer4 = true;
    bool FlagBeer5 = true;
    bool FlagBeer6 = true;


    bool Beer1 = true;
    bool Beer2 = true;
    bool Beer3 = true;
    bool Beer4 = true;
    bool Beer5 = true;
    bool Beer6 = true;



    private static System.Timers.Timer aTimer;

    public Window1()
    {
        InitializeComponent();
    }

    public void RemoveImage()
    {


        if (FlagBeer1 && FlagBeer2 && FlagBeer3 && FlagBeer4 && FlagBeer5 && FlagBeer6)
        {
            FlagBeer1 = false;
            Image.Source = new BitmapImage(new Uri("./Resources/Bierglashalfvol.png"));
            SetTimer();
        }
        else if (FlagBeer2 && FlagBeer3 && FlagBeer4 && FlagBeer5 && FlagBeer6)
        {
            FlagBeer2 = false;
            Image1.Source = new BitmapImage(new Uri("./Resources/Bierglashalfvol.png"));
            SetTimer();
        }
        else if (FlagBeer3 && FlagBeer4 && FlagBeer5 && FlagBeer6)
        {
            FlagBeer3 = false;
            Image2.Source = new BitmapImage(new Uri("./Resources/Bierglashalfvol.png"));
            SetTimer();
        }
        else if (FlagBeer4 && FlagBeer5 && FlagBeer6)
        {
            FlagBeer4 = false;
            Image3.Source = new BitmapImage(new Uri("./Resources/Bierglashalfvol.png"));
            SetTimer();
        }
        else if (FlagBeer5 && FlagBeer6)
        {
            FlagBeer5 = false;
            Image4.Source = new BitmapImage(new Uri("./Resources/Bierglashalfvol.png"));
            SetTimer();
        }
        else if (FlagBeer6)
        {
            FlagBeer6 = false;
            Image5.Source = new BitmapImage(new Uri("./Resources/Bierglashalfvol.png"));
            SetTimer();
        }
    }

    private void SetTimer()
    {
        // Create a timer with a two second interval.
        aTimer = new System.Timers.Timer(1000);
        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;
        aTimer.AutoReset = false;
        aTimer.Enabled = true;
    }


    private void OnTimedEvent(Object source, ElapsedEventArgs e)
    {

        if (Beer1 && Beer2 && Beer3 && Beer4 && Beer5 && Beer6)
        {
            Beer1 = false;
            this.Dispatcher.Invoke(() =>
            {
                Image.Source = new BitmapImage(new Uri(@"./Resources/bierglasleeg.png"));
            });
        }
        else if (Beer2 && Beer3 && Beer4 && Beer5 && Beer6)
        {
            Beer2 = false;
            this.Dispatcher.Invoke(() =>
            {
                Image1.Source = new BitmapImage(new Uri(@"./Resources/bierglasleeg.png"));
            });
        }
        else if (Beer3 && Beer4 && Beer5 && Beer6)
        {
            Beer3 = false;
            this.Dispatcher.Invoke(() =>
            {
                Image2.Source = new BitmapImage(new Uri(@"./Resources/bierglasleeg.png"));
            });
        }
        else if (Beer4 && Beer5 && Beer6)
        {
            Beer4 = false;
            this.Dispatcher.Invoke(() =>
            {
                Image3.Source = new BitmapImage(new Uri(@"./Resources/bierglasleeg.png"));
            });
        }
        else if (Beer5 && Beer6)
        {
            Beer5 = false;
            this.Dispatcher.Invoke(() =>
            {
                Image4.Source = new BitmapImage(new Uri(@"./Resources/bierglasleeg.png"));
            });
        }
        else if (Beer6)
        {
            Beer6 = false;
            this.Dispatcher.Invoke(() =>
            {
                Image5.Source = new BitmapImage(new Uri(@"./Resources/bierglasleeg.png"));
            });
        }


    }

    public void HerstelImage()
    {


        if (FlagBeer6 == false && FlagBeer5 == false  && FlagBeer4 == false  && FlagBeer3 == false  && FlagBeer2 == false  && FlagBeer1 == false)
        {
            FlagBeer6 = true;
            Image5.Source = new BitmapImage(new Uri(@"./Resources/Bierglashalfvol.png"));
            SetTimer1();
        }
        else if (FlagBeer5 == false && FlagBeer4 == false && FlagBeer3 == false && FlagBeer2 == false && FlagBeer1 == false)
        {
            FlagBeer5 = true;
            Image4.Source = new BitmapImage(new Uri(@"./Resources/Bierglashalfvol.png"));
            SetTimer1();
        }
        else if (FlagBeer4 == false && FlagBeer3 == false && FlagBeer2 == false && FlagBeer1 == false)
        {
            FlagBeer4 = true;
            Image3.Source = new BitmapImage(new Uri(@"./Resources/Bierglashalfvol.png"));
            SetTimer1();
        }
        else if (FlagBeer3 == false && FlagBeer2 == false && FlagBeer1 == false)
        {
            FlagBeer3 = true;
            Image2.Source = new BitmapImage(new Uri(@"./Resources/Bierglashalfvol.png"));
            SetTimer1();
        }
        else if (FlagBeer2 == false && FlagBeer1 == false)
        {
            FlagBeer2 = true;
            Image1.Source = new BitmapImage(new Uri(@"./Resources/Bierglashalfvol.png"));
            SetTimer1();
        }
        else if (FlagBeer1 == false)
        {
            FlagBeer1 = true;
            Image.Source = new BitmapImage(new Uri(@"./Resources/Bierglashalfvol.png"));
            SetTimer1();
        }
    }

    private void SetTimer1()
    {
        // Create a timer with a two second interval.
        aTimer = new System.Timers.Timer(1000);
        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += Verlopenevent;
        aTimer.AutoReset = false;
        aTimer.Enabled = true;
    }

    private void Verlopenevent(Object source, ElapsedEventArgs e)
    {
        if (Beer6 == false && Beer5 == false && Beer4 == false && Beer3 == false && Beer2 == false && Beer1 == false)
        {
            Beer6 = true;
            this.Dispatcher.Invoke(() =>
            {
                Image5.Source = new BitmapImage(new Uri(@"./Resources/Bierglasvol.png"));
            });
        }
        else if (Beer5 == false && Beer4 == false && Beer3 == false && Beer2 == false && Beer1 == false)
        {
            Beer5 = true;
            this.Dispatcher.Invoke(() =>
            {
                Image4.Source = new BitmapImage(new Uri(@"./Resources/Bierglasvol.png"));
            });
        }
        else if (Beer4 == false && Beer3 == false && Beer2 == false && Beer1 == false)
        {
            Beer4 = true;
            this.Dispatcher.Invoke(() =>
            {
                Image3.Source = new BitmapImage(new Uri(@"./Resources/Bierglasvol.png"));
            });
        }
        else if (Beer3 == false && Beer2 == false && Beer1 == false)
        {
            Beer3 = true;
            this.Dispatcher.Invoke(() =>
            {
                Image2.Source = new BitmapImage(new Uri(@"./Resources/Bierglasvol.png"));
            });
        }
        else if (Beer2 == false && Beer1 == false)
        {
            Beer2 = true;
            this.Dispatcher.Invoke(() =>
            {
                Image1.Source = new BitmapImage(new Uri(@"./Resources/Bierglasvol.png"));
            });
        }
        else if (Beer1 == false)
        {
            Beer1 = true;
            this.Dispatcher.Invoke(() =>
            {
                Image.Source = new BitmapImage(new Uri(@"./Resources/Bierglasvol.png"));
            });
        }
    }



    public void achtergrondfoto()
    {
        achtergrondfoto1.Source = null;
    }

    public void Achtergrondfotoplaatsten()
    {
        achtergrondfoto1.Source = new BitmapImage(new Uri(@"./Resources/escapehaarlem_logo1.png"));
    }

    public void UpdateLabel(string text)
    {
        lbl_Tekst.Text = text;
    }
}
}

主窗口xaml代码

<Window x:Class="Escapehaarlem.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Escapehaarlem"
    mc:Ignorable="d"
    Title="MainWindow" 
    Height="350" 
    Width="560" 
    ResizeMode="CanResizeWithGrip" 
    Topmost="False" WindowState="Normal" 
    WindowStartupLocation="CenterOwner" 
    Icon="./Resources/escapehaarlem_logo.ico"
    MinWidth="300"
    MinHeight="200">

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="3*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="0.3*" />
        <RowDefinition Height="0.3*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="0.3*" />
    </Grid.RowDefinitions>
    <Button Name="Verstuur" Grid.Column="1" Margin="2" Click="Verstuur_Click">Verstuur</Button>
    <Button Name="Verwijder" Grid.Row="1" Grid.Column="2" Margin="2" Click="Verwijder_Click">Verwijder</Button>
    <Button Name="Herstel" Grid.Row="3" Grid.Column="2" Margin="2" Click="Herstel_Click">Herstel</Button>
    <TextBox MaxLength="185" FontSize="20"  TextWrapping="Wrap" Grid.RowSpan="4" Name="Textbox" Margin="2"></TextBox>
</Grid>

</Window>

第二个窗口XAML代码

<Window x:Class="Escapehaarlem.Window1"
    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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Escapehaarlem"
    mc:Ignorable="d"
    Title="SecondWindow" Height="1024" Width="1280" ResizeMode="NoResize" Topmost="True" WindowState="Maximized" Icon="./Resources/escapehaarlem_logo.ico" WindowStartupLocation="Manual" WindowStyle="None">

<Grid Background="Black">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>


    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="0.3*" />
    </Grid.RowDefinitions>
    <Image Name="achtergrondfoto1" Grid.ColumnSpan="6" Source="./Resources/escapehaarlem_logo1.png" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
    <TextBlock Grid.ColumnSpan="6"  Name="lbl_Tekst" VerticalAlignment="Top" HorizontalAlignment="Center" Foreground="White" Height="1010" TextWrapping="Wrap" FontSize="130" FontFamily="Broken Planewing"/>
    <Image Source="./Resources/Bierglasvol.png" Grid.Row="1" Grid.Column="5" Name="Image" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
    <Image Source="./Resources/Bierglasvol.png" Grid.Row="1" Grid.Column="4" Name="Image1" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
    <Image Source="./Resources/Bierglasvol.png" Grid.Row="1" Grid.Column="3" Name="Image2" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
    <Image Source="./Resources/Bierglasvol.png" Grid.Row="1" Grid.Column="2" Name="Image3" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
    <Image Source="./Resources/Bierglasvol.png" Grid.Row="1" Grid.Column="1" Name="Image4" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
    <Image Source="./Resources/Bierglasvol.png" Grid.Row="1" Grid.Column="0" Name="Image5" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
</Grid>
</Window>

对不起所有代码,但是我觉得它可能有点小。我希望它能够在其他桌面上运行。

请让我知道是否需要任何信息

谢谢。

1 个答案:

答案 0 :(得分:0)

您应该从bin / {YourBuildConfiguration}文件夹中复制所有二进制文件,因为您的应用程序仅复制需要的文件,而不仅仅是.exe文件。

根据您的评论进行更新: 您不必从解决方案中创建一个zip。解决方案是开发代码。构建解决方案将产生一个应用程序作为输出,其输出默认为{SolutionFolder} / {ProjectName} / bin / Debug或Release。您必须压缩该文件夹的内容,将其复制到所需的位置(也许是另一台计算机),然后解压缩并运行exe。所有必需的DLL都应该在您要运行的.exe文件旁边。

尝试重现this questions答案中提到的步骤。