出于未知原因,单击按钮将关闭我的窗口。我怎样才能解决这个问题?

时间:2019-05-12 19:27:43

标签: monodevelop gtk#

我试图在Monodevelop中使用按钮复制一个简单的2个条目的添加(显示了如何逐步进行操作),但是以某种方式在按下按钮后2秒关闭了窗口,而实际上并未进行任何更改。

按钮的代码:

using Gtk;
using Frontend.ChatService;

public partial class MainWindow : Gtk.Window
{
    public MainWindow() : base(Gtk.WindowType.Toplevel)
    {
        Build();
    }

    protected void OnDeleteEvent(object sender, DeleteEventArgs a)
    {
        Application.Quit();
        a.RetVal = true;
    }

    protected void OnButton1Clicked(object sender, EventArgs e)
    {
        ChatService client = new ChatService();
        int x = Int32.Parse(entry1.Text);
        int y = Int32.Parse(entry2.Text);
        int sum = client.Add(x, y);
        entry1.Text = sum.ToString();
    }
}

总和(我测试并认为可行)

using System;
using System.Web;
using System.Web.Services;

namespace Backend
{
    public class ChatService : System.Web.Services.WebService
    {
        [WebMethod]
        public int Add(int x, int y)
        {
            return x + y;
        }
    }
}

我将主文件program.cs保留为已生成,并且是:

using System;
using Gtk;

namespace Frontend
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Application.Init();
            MainWindow win = new MainWindow();
            win.Show();
            Application.Run();
        }
    }
}

该窗口确实会弹出,直到按下该按钮才显示问题。

编辑: 我忘了运行后端/服务器部分,这就是为什么找不到该功能的原因……(我想是初学者的错误) 现在可以使用

1 个答案:

答案 0 :(得分:0)

问题可能是您的代码引发了您不知道的异常。问题出在处理被单击按钮的代码中。

protected void OnButton1Clicked(object sender, EventArgs e)
{
    ChatService client = new ChatService();
    int x = Int32.Parse(entry1.Text);
    int y = Int32.Parse(entry2.Text);
    int sum = client.Add(x, y);
    entry1.Text = sum.ToString();
}

我们逐行走吧:

ChatService client = new ChatService();

在这里,您正在创建似乎是系统服务或Web服务的新实例。如果不知道该服务(在前一种情况下),或者在后一种情况下,如果连接中断或未到达目的地等,则可能抛出该错误。

这些行也很微妙:

int x = Int32.Parse(entry1.Text);
int y = Int32.Parse(entry2.Text);

如果字段entry1entry2为空,或包含字母...,它们将抛出。

为了管理这些情况,您需要在适当的位置添加try... catch块。或者,您可以使用Int32.TryParse。

例如,假设服务在网络中:

protected void OnButton1Clicked(object sender, EventArgs e)
{
    ChatService client;
    int x;
    int y;

    try {
        client = new ChatService();
    } catch(HttpRequestException exc) {
        client = null;

        var dlg = new Gtk.MessageDialog(
                            this,
                            Gtk.DialogFlags.Modal,
                            Gtk.MessageType.Error,
                            Gtk.ButtonsType.Ok,
                            "Connection error"
         );

        dlg.Text = exc.Message;
        dlg.Run();
        dlg.Destroy();
    }

    if ( client != null ) {
        if ( !int.TryParse( entry1.Text, out x) {
            entry1.Text = "0";
            x = 0;
        }

        if ( !int.TryParse( entry2.Text, out y) {
            entry2.Text = "0";
            y = 0;
        }

        int sum = client.Add(x, y);
        entry1.Text = sum.ToString();
    }
}

当然,获取正确处理错误的代码总是很难。 希望这会有所帮助。

相关问题