【问题标题】:Windows Phone 7 Event Invoke?Windows Phone 7 事件调用?
【发布时间】:2011-06-04 16:51:42
【问题描述】:

我正在尝试使用 Visual Studio C# 处理事件,就像使用 Windows 窗体一样,例如:

在我的 Windows Phone 7 上,我的代码如下所示:

//ASDF.CS CLASS  

public delegate void SignedOn(string Screenname);

public event SignedOn SO;

public void dataIncoming(string packet)
{
     switch (packet)
 {
    case 0:
        if (SO != null)
                                    SO(m_Screenname);
        break;
}
}

//MainWindow.xaml.cs

m_A.SignedOn += new ASDF.SignedOn(m_A_LoggedIn);


void m_OSC_LoggedIn(string Screenname, string FormattedSN, string Email)
    {
        //Works all the way to this sub then the code in here don't get ran because there is no invoke with WP7

        MessageBox.Show("hello!");
}

我的 Windows 窗体代码看起来像这样:

//ASDF.CS CLASS  

public delegate void SignedOn(string Screenname);

public event SignedOn SO;

public void dataIncoming(string packet)
{
     switch (packet)
 {
    case 0:
        if (SO != null)
                                    SO(m_Screenname);
        break;
}
}

//MainWindow.cs

m_A.SignedOn += new ASDF.SignedOn(m_A_LoggedIn);


void m_OSC_LoggedIn(string Screenname, string FormattedSN, string Email)
    {
         this.Invoke(new MethodInvoker(delegate
        {
            MessageBox.Show("hello!");
    }));
}

那么无论如何我可以得到一个替代调用来使用 Windows Phone 7 吗?

还有其他方法可以让它在 WP7 上运行吗?

谢谢

【问题讨论】:

标签: c# windows-phone-7


【解决方案1】:

尝试使用:

void m_OSC_LoggedIn(string Screenname, string FormattedSN, string Email)
{

        Dispatcher.BeginInvoke(() =>
        {
              MessageBox.Show("hello!");
        });
}

【讨论】:

  • @Eric - 没有问题 :) 每当您想将数据从后台线程转移到 UI 线程(在 WP7 中)时,您都必须使用 Dispatcher 的 BeginInvoke 方法。
猜你喜欢
  • 2011-05-13
  • 1970-01-01
  • 2011-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多