使Silverlight 2程序在Silverlight 4中运行

时间:2011-08-08 17:58:36

标签: silverlight

我正在尝试使用Silverlight 2(http://www.jeff.wilcox.name/2008/03/silverlight2-unit-testing/)编写Jeff的测试程序,使用Silverlight 4和VS2010工作,我得到了此行的NullReferenceException:     chatSession.ConnectWithRemoteUser( “ScottGu”); 有什么想法吗?这是代码:

public Page()
{
    // commented out because it doesn't exist in the current context 
    // according to the compiler
    // InitializeComponent();

    // Retrieve ChatSession instance from XAML resource declaration
    chatSession = new ChatSession();
    chatSession = (ChatSession)Resources["ChatSessionDS"];

    // Connect with Chat Server to chat with "ScottGu"
    chatSession.ConnectWithRemoteUser("ScottGu");
}

1 个答案:

答案 0 :(得分:0)

首先,如果您必须注释掉InitializeComponent(),我对您的应用程序感到不满:在解析页面的XAML文件时,VS工具会生成InitializeComponent。

如果它不存在,则可能意味着在xaml文件中声明的类与c#文件中的类(不同的名称或不同的命名空间)不同。否则,可能是VS属性选项卡中的XAML文件操作类型未设置为“页面”。

第二

chatSession = new ChatSession();
chatSession = (ChatSession)Resources["ChatSessionDS"];

这看起来很奇怪。你创建一个新的ChatSession,然后用保存在页面资源中的对象替换它。如果你将实例放在下一行的范围之外,为什么要先创建它呢?

相关问题