Windows phone 7 如何在Windows Phone 7.x中打开网络视图?
我希望我的应用程序只有一个视图 我需要这个视图是一个外部URL 我尝试按照microsoft上的示例使用webBrowser任务 我穿上我的衣服:Windows phone 7 如何在Windows Phone 7.x中打开网络视图?,windows-phone-7,url,webview,external,Windows Phone 7,Url,Webview,External,我希望我的应用程序只有一个视图 我需要这个视图是一个外部URL 我尝试按照microsoft上的示例使用webBrowser任务 我穿上我的衣服: WebBrowserTask webBrowserTask = new WebBrowserTask(); webBrowserTask.Uri = new Uri("http://msdn.microsoft.com", UriKind.Absolute); webBrowserTask.Show(); 但是,当我按下后退按钮而不是在应用程序
WebBrowserTask webBrowserTask = new WebBrowserTask();
webBrowserTask.Uri = new Uri("http://msdn.microsoft.com", UriKind.Absolute);
webBrowserTask.Show();
但是,当我按下后退按钮而不是在应用程序外导航时,我导航到的第一页是空的
我的代码如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
//added this for push
using Microsoft.Phone.Notification;
using System.Text;
//added this to open an external URL using the WebBrowser Task
using Microsoft.Phone.Tasks;
namespace WindowsPush
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
/// 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()));
}
object uniqueID;
if (Microsoft.Phone.Info.DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out uniqueID) == true)
{
byte[] bID = (byte[])uniqueID;
string deviceID = Convert.ToBase64String(bID); // There you go
System.Diagnostics.Debug.WriteLine("Device Unique Id is: {0}", deviceID);
}
//opening the external URL using webBrowserTask
WebBrowserTask webBrowserTask = new WebBrowserTask();
webBrowserTask.Uri = new Uri("http://msdn.microsoft.com", UriKind.Absolute);
webBrowserTask.Show();
}
void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
// Display the new URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
System.Diagnostics.Debug.WriteLine(e.ChannelUri.ToString());
MessageBox.Show(String.Format("Channel Uri is {0}", e.ChannelUri.ToString()));
});
}
void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
{
// Error handling logic for your particular application would be here.
Dispatcher.BeginInvoke(() =>
MessageBox.Show(String.Format("A push notification {0} error occurred. {1} ({2}) {3}", e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData)));
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Animation;
使用System.Windows.Shapes;
使用Microsoft.Phone.Controls;
//为推送添加了这个
使用Microsoft.Phone.Notification;
使用系统文本;
//添加此项以使用WebBrowser任务打开外部URL
使用Microsoft.Phone.Tasks;
命名空间窗口推送
{
公共部分类主页:PhoneApplicationPage
{
//建造师
公共主页()
{
///保存已创建或找到的推送通道。
HttpNotificationChannel推送通道;
//我们推送频道的名称。
字符串channelName=“ToastSampleChannel”;
初始化组件();
//试着找到推送通道。
pushChannel=HttpNotificationChannel.Find(channelName);
//如果未找到通道,则创建到推送服务的新连接。
if(pushChannel==null)
{
pushChannel=新的HttpNotificationChannel(通道名称);
//在尝试打开通道之前注册所有事件。
pushChannel.ChannelUriUpdated+=新事件处理程序(pushChannel\u ChannelUriUpdated);
pushChannel.ErrorOccursed+=新的事件处理程序(pushChannel\u ErrorOccursed);
//仅当您需要在应用程序运行时接收通知时,才注册此通知。
//pushChannel.ShellToastNotificationReceived+=新事件处理程序(pushChannel\u ShellToastNotificationReceived);
pushChannel.Open();
//为toast事件绑定此新频道。
pushChannel.BindToShellToast();
}
其他的
{
//频道已经打开,所以只需注册所有事件。
pushChannel.ChannelUriUpdated+=新事件处理程序(pushChannel\u ChannelUriUpdated);
pushChannel.ErrorOccursed+=新的事件处理程序(pushChannel\u ErrorOccursed);
//仅当您需要在应用程序运行时接收通知时,才注册此通知。
//pushChannel.ShellToastNotificationReceived+=新事件处理程序(pushChannel\u ShellToastNotificationReceived);
//出于测试目的显示URI。通常,URI会在此时传递回web服务。
System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
//Show(String.Format(“通道Uri为{0}”,pushChannel.ChannelUri.ToString());
}
对象唯一性;
if(Microsoft.Phone.Info.DeviceExtendedProperties.TryGetValue(“DeviceUniqueId”,out uniqueID)==true)
{
字节[]bID=(字节[])唯一ID;
string deviceID=Convert.ToBase64String(bID);//好了
System.Diagnostics.Debug.WriteLine(“设备唯一Id为:{0}”,deviceID);
}
//使用webBrowserTask打开外部URL
WebBrowserTask WebBrowserTask=新建WebBrowserTask();
webBrowserTask.Uri=新Uri(“http://msdn.microsoft.com“,乌里金。绝对);
webBrowserTask.Show();
}
void PushChannel\u ChannelUriUpdated(对象发送方,NotificationChannelUriEventArgs e)
{
Dispatcher.BeginInvoke(()=>
{
//出于测试目的显示新的URI。通常,URI会在此时传递回web服务。
System.Diagnostics.Debug.WriteLine(例如ChannelUri.ToString());
Show(String.Format(“通道Uri为{0}”,e.ChannelUri.ToString());
});
}
出现无效PushChannel_错误(对象发送方,NotificationChannelErrorEventArgs e)
{
//这里将显示特定应用程序的错误处理逻辑。
Dispatcher.BeginInvoke(()=>
Show(String.Format(“发生推送通知{0}错误。{1}({2}){3}”,e.ErrorType,e.Message,e.ErrorCode,e.ErrorAdditionalData));
}
}
}
我需要的是在我的应用程序的第一个也是唯一的视图上加载一个外部URL
我怎么能这样做呢?你在找那个。将其添加到主应用程序页面,并像处理
WebBrowserTask
一样处理它。如果要使用WebBrowserTask
,则可以通过引发未处理的异常在返回应用程序时关闭应用程序:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back)
{
throw new Exception("deliberately doing this to force the app to close");
}
}
如果在Windows Phone 8中执行此操作,则可以调用Application.Current.Terminate()代码>而不是引发异常
请确保在提交之前检查仅启动外部网站的应用程序的市场认证要求。我正在尝试使用WB1.Navigate(新Uri(“));但我得到一个错误,即WB1在该上下文中不存在。考虑到这一点,在加载URL之前,我想运行代码,以便在我的服务器上启用推送通知并注册令牌。您需要将WebBrowser
组件添加到您工作页面的可视化树中。您能告诉我具体操作方法吗?这是我的第一次C#project.在主页.XAML
中添加
(假设您使用的是开箱即用的命名)。我得到: