【问题标题】:I'm not sure if my game is working yet我不确定我的游戏是否正常运行
【发布时间】:2013-06-01 15:31:33
【问题描述】:

我和我的朋友一直在开发一款游戏,我们设置了一个跟踪监听器,下面是一些代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms; 
using System.Diagnostics;

namespace GuiGame {

/// <summary>
/// A type of Trace Listener that sends its output to a ListBox.
/// </summary>
public class ListBoxTraceListener : TraceListener {

    private ListBox listBox;  // A reference to the listbox that we're writing to.

    private string stringToAddToListBox = "";

    private const char NEW_LINE = '\n';

    /// <summary>
    /// Parameterless constructor.
    /// Do not want the generic default constructor to be used
    /// as there is no way to set the ListBoxTraceListener's data.
    /// This replaces the compiler's generic default constructor.
    /// Pre:  none
    /// Post: ALWAYS throws an ArgumentException.
    /// </summary>
    /// <remarks>NOT TO BE USED!</remarks>
    public ListBoxTraceListener() {
        throw new ArgumentException("Parameterless constructor invalid.");
    } // end ListBoxTraceListener constructor

    /// <summary>
    /// Constructor with initialising parameters.
    /// Pre:  the existence of a ListBox on a GUI form.
    /// Post: initialised object.
    /// </summary>
    /// <param name="listBox">The ListBox that we're writing to.</param>
    public ListBoxTraceListener(ListBox listBox) {
        this.listBox = listBox;
    }

    /// <summary>
    /// Automatically collects the outputs from all Trace.WriteLine statements.
    /// Pre:  none.
    /// Post: the string s is displayed in the listBox.
    /// </summary>
    /// <param name="s"></param>
    public override void WriteLine(string s) {
        Write(s + NEW_LINE);
    } //end WriteLine

    /// <summary>
    /// Automatically collects the outputs from all Trace.Write statements.
    /// Pre:  none.
    /// Post: the string s is displayed in the listBox, once we receive a NEW_LINE.
    /// </summary>
    /// <param name="s"></param>
    public override void Write(string s) {
        stringToAddToListBox += s;

        // If we have one or more complete lines
        if (stringToAddToListBox.Contains (NEW_LINE)) {

            // Split the string into multiple lines. 
            // If NEW_LINE is found at the beginning or end of the string, 
            // then the corresponding array element contains an empty string. 
            string[] lines = stringToAddToListBox.Split(NEW_LINE);

            // Add all the lines to the listbox, except for the last one.
            // When stringToAddToListBox has a new-line at the end, 
            // the last element in lines[] will be an empty string.
            int highestLineNumber = lines.Length - 1;
            for (int i = 0; i < highestLineNumber; i++) {
                AddToListBox(lines[i]);
            }

            // Reset stringToAddToListBox to what remains. (May be an empty string).
            stringToAddToListBox = lines[highestLineNumber];
        }
    } // end Write

    /// <summary>
    /// Adds a complete output-line to the ListBox.
    /// Pre:  none.
    /// Post: the string listBoxLine is displayed in the listBox    .
    /// </summary>
    /// <param name="listBoxLine"></param>
    private void AddToListBox(string listBoxLine) {
        Debug.Assert(listBox != null, "listBox != null");
        listBox.Items.Add(listBoxLine);
    } // end AddToListBox
}

}

在这个阶段,我们只是尝试使用跟踪侦听器在ListBox 上输出一些文本,因此我们知道它正在工作,因此我们设置了一个事件处理程序:

private void RollDiceButton_Click(object sender, EventArgs e)
{

}

我们无法从跟踪侦听器获得任何输出。 Add 方法作为跟踪侦听器没有为此设置。任何人都可以提供一些建议吗?我想也许我们正在做一些我们错过的非常愚蠢和明显的事情。

【问题讨论】:

  • 为无参数构造函数抛出异常对我来说真的没有意义。实现一个带参数的构造函数,如果有人试图创建一个带有 0 个参数的新实例,让编译器指出存在错误。

标签: c# winforms tracelistener


【解决方案1】:

问题的最可能原因是您的应用程序是单线程的(就像大多数简单的 Windows 应用程序一样)。这意味着尽管您正在向列表视图发送消息以将新元素附加到列表中,但消息尚未处理只是因为您的原始处理程序尚未返回(RollDiceButton_Click)。

要解决这个问题,您应该强制列表在当前处理程序中自行刷新:

private void AddToListBox(string listBoxLine) {
    Debug.Assert(listBox != null, "listBox != null");
    listBox.Items.Add(listBoxLine);
    // this would help? 
    listBox.Refresh();
} // end 

如果这没有帮助,请尝试暂时切换到无条件处理所有待处理的事件

private void AddToListBox(string listBoxLine) {
    Debug.Assert(listBox != null, "listBox != null");
    listBox.Items.Add(listBoxLine);
    // this would help? 
    Application.DoEvents();
} // end 

然后报告。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-01
    • 2017-07-27
    • 1970-01-01
    • 2014-01-23
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多