0

我对 ASP.NET 网站使用模型视图演示器方法。演示部分在单独的类库中编译,其中定义了视图契约和演示者:

MyPresentation assembly
IMyView
{
     DisplayText(string text);
}
MyPresenter
{
     public IMyView View { get; set; }
     public DisplayText()
     {
         string text = Generate();
         View.DisplayText(text);   
     } 
}

MyWebApplication assembly
public partial class MyForm : System.Web.UI.Page, IMyView
{
    public void DisplayText(string text)
    {
        this.myLabel.Text = text;  
    }
    ...
    protected void myButton_Click(object sender, EventArgs e) 
    {
        Presenter.DisplayText(); // calls this.DisplayText()     
    }        
}

但是,在退出Presenter.DisplayText()Text 属性后,myLabel再次变为 null,就好像没有完成任何分配一样。myButton无论如何,如果您将单击事件中的唯一行替换为直接设置myLabelText 属性,则所有内容都将保持分配状态。

4

1 回答 1

1

Generate方法是否返回非空字符串?在这种情况下,这似乎是唯一的罪魁祸首。它也可以解释为什么设置myLabel直接起作用。

要进行诊断,请将文字字符串传递给View.DisplayText而不是调用Generate.

于 2012-10-09T20:23:15.973 回答