2

这是我的代码。我第一次运行它时一切正常,即 HttpNotificationChannel.Find() 返回 null。

但是我第二次运行它时,Find() 返回了正确的东西,但是当我调用 Open() 时,它抛出了一个异常。这真的很奇怪,因为 Open() 不接受任何参数。

我究竟做错了什么?

  public string ChannelName = "MyAppChannel";
  ...
  NotificationChannel = HttpNotificationChannel.Find(ChannelName);
  if (NotificationChannel == null)
  {
    NotificationChannel = new HttpNotificationChannel(ChannelName);
  }
  NotificationChannel.ChannelUriUpdated += new EventHandler(Channel_ChannelUriUpdated);
  NotificationChannel.HttpNotificationReceived += new EventHandler(NotificationChannel_HttpNotificationReceived);
  NotificationChannel.ErrorOccurred += new EventHandler(Channel_ErrorOccurred);
  NotificationChannel.Open();         // <-- Kaboom here, the 2nd time 

这是全文和堆栈:

System.ArgumentException: E_INVALIDARG
   at Microsoft.Phone.Notification.SafeNativeMethods.ThrowExceptionFromHResult(Int32 hr, Exception defaultException, NotificationType type)
   at Microsoft.Phone.Notification.HttpNotificationChannel.Open()
   at LiveShare.NotificationManager.Initialize()
   at LiveShare.App..ctor()
   at System.Reflection.RuntimeConstructorInfo.InternalInvoke(RuntimeConstructorInfo rtci, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
   at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
   at MS.Internal.TypeProxy.<>c__DisplayClass30.b__2a()
   at MS.Internal.TypeProxy.CreateInstance(UInt32 customTypeId)
   at MS.Internal.FrameworkCallbacks.CreateKnownObject(IntPtr nativeRootPeer, UInt32 customTypeId, String initializationString, IntPtr& nativePeer, UInt32 isCreatedByParser)
   at MS.Internal.FrameworkCallbacks.CreateUnknownObject(String assemblyName, String typeName, IntPtr nativeRootPeer, String initializationString, UInt32& customTypeId, UInt32& coreTypeId, UInt32& typeFlags, IntPtr& nativePeer)
4

2 回答 2

2

Open()正确的解决方案是如果Find()成功 就不要调用。

于 2011-02-15T16:13:20.790 回答
1

这看起来与Nick Harris去年 4 月描述的 CTP 记录问题非常相似。

解决方案:如果您在模拟器启动时点击调试后几乎立即打开频道,则会出现此问题。解决方案很简单——在拨打电话前给模拟器两分钟。

如果不是这样,Silverlight Show上有一篇很好的文章,其中涵盖了 WP7 股票报价应用程序中的推送通知。

于 2011-02-04T18:26:19.503 回答