我遇到了一个奇怪的问题,因为通常这就像Windows Phone 7上的魅力一样。我刚刚将项目升级到Windows Phone 8以获取XAP文件,它崩溃了。
这是“失败”代码的一部分:
/// Holds the push channel that is created or found.
HttpNotificationChannel pushChannel;
// The name of our push channel.
string channelName = "ToastSampleChannel";
InitializeComponent();
// Try to find the push channel.
pushChannel = HttpNotificationChannel.Find(channelName);
// If the channel was not found, then create a new connection to the push service.
if (pushChannel == null)
{
pushChannel = new HttpNotificationChannel(channelName);
// Register for all the events before attempting to open the channel.
pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
// Register for this notification only if you need to receive the notifications while your application is running.
//pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
pushChannel.Open();
// Bind this new channel for toast events.
pushChannel.BindToShellToast();
}
else
{
// The channel was already open, so just register for all the events.
pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
// Register for this notification only if you need to receive the notifications while your application is running.
//pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
// Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
// MessageBox.Show(String.Format("Channel Uri is {0}", pushChannel.ChannelUri.ToString()));
}
string token = pushChannel.ChannelUri.ToString();
//System.Diagnostics.Debug.WriteLine("token");
//System.Diagnostics.Debug.WriteLine(token);
//int active = 1;
object uniqueID;
string deviceID;
if (Microsoft.Phone.Info.DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out uniqueID) == true)
{
WB1.Navigated += WB1_Navigated;
//getting udid
byte[] bID = (byte[])uniqueID;
deviceID = Convert.ToBase64String(bID); // There you go
}
当它崩溃时,执行的下一行将是WB1.Navigated += WB1_Navigated;
所以我认为错误是在之前的if条件中,但是我无法看到错误,因为在项目属性中升级目标之前,它运行正常。
有什么想法吗?
我添加了WB1_Navigated和其他相关方法:
void WB1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
visitedUrls.Push(e.Uri);
}
private void WebBrowser_OnLoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
this.CoverImage.Visibility = Visibility.Collapsed;
}
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
if (visitedUrls.Count > 1)
{
visitedUrls.Pop();
WB1.Navigate(visitedUrls.Pop());
e.Cancel = true;
}
}
作为请愿,我显示我的xml代码:
<phone:PhoneApplicationPage
x:Class="WindowsPush.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True" d:DesignHeight="768" d:DesignWidth="480">
<!--LayoutRoot is the root grid where all page content is placed-->
<!--Sample code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<phone:WebBrowser Name="WB1" LoadCompleted="WebBrowser_OnLoadCompleted"/>
<Image x:Name="CoverImage" Width="Auto" Height="Auto" Source="\SplashScreenImage.jpg"></Image>
</Grid>
</phone:PhoneApplicationPage>
答案 0 :(得分:3)
你正在(可能)你的异常排队:
string token = pushChannel.ChannelUri.ToString();
一段时间后,你得到了你的ChanelUri,在你得到它之前它就是空的。将您的令牌移至PushChannel_ChannelUriUpdated:
private void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
string token = pushChannel.ChannelUri.ToString();
}
编辑 - 来源信息
根据{{3}}:
The Open channel (a few lines of code later) is an asynchronous operation (therefore the need to attach an event handler that gets triggered once the open operation completes). If it is successful, the ChannelUriUpdate event is raised, if it fails, most probably HttpNotificationChannel will raise an ExceptionOccurred event.