【问题标题】:How to open a webView in Windows Phone 7.x?如何在 Windows Phone 7.x 中打开 webView?
【发布时间】:2023-03-08 16:23:01
【问题描述】:

我希望我的应用只有一个视图。

我需要这个视图是一个外部 URL。

我尝试按照 microsoft 上的示例使用 webBrowser Task。

我穿上我的承包商:

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)));
        }


    }
}

我需要的是在我的应用程序的第一个也是唯一一个视图上加载一个外部 URL。

我该怎么做?

【问题讨论】:

    标签: windows-phone-7 url webview external


    【解决方案1】:

    您正在寻找WebBrowser control。将其添加到主应用程序页面并像处理 WebBrowserTask 一样处理它。

    【讨论】:

    • 我正在尝试使用 WB1.Navigate(new Uri("msdn.microsoft.com")); 但我收到一个错误,即 WB1 在该上下文中不存在。考虑到在我加载 URL 之前想要运行代码以启用推送通知并在我的服务器上注册令牌。
    • 您需要将WebBrowser 组件添加到工作页面的可视化树中。
    • 你能告诉我具体怎么做吗?这是我的第一个 C# 项目。
    • MainPage.xaml 中的 XAML 中添加 &lt;WebBrowser x:Name="WB1" /&gt;(假设您使用的是开箱即​​用的命名)。
    • 我得到:“类型 WebBrowser 未找到验证您没有丢失任何程序集引用并且所有引用程序集都已构建”
    【解决方案2】:

    如果您要使用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(); 而不是引发异常。

    在提交之前,请务必检查刚刚启动外部网站的应用的市场认证要求。

    【讨论】:

    • 如果您的应用只有一页,则无需这样做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多