pin webbrowser实例启动屏幕

时间:2012-02-27 20:14:48

标签: c# windows-phone-7

基本上我要完成的是将webbrowser实例固定到我的Windows Phone开始屏幕,然后当有人想要返回到固定的特定网站时,用户将点击该磁贴并被带到应用程序重新加载时,我的应用程序中的同一个网页。我已经在整个互联网上研究了这个功能,并没有找到任何利用它,但我已经看到这在市场上的一些应用程序上执行。 我试图引用某人使用查询字符串实现类似的东西来获取所需的数据来告诉应用程序如何从辅助磁贴加载,但我可能有一些不正确的东西。另外,我可以告诉辅助磁贴在我的应用程序中加载主页面,其中包含一个webbrowser控件,但我还没弄明白如何发送链接到webbrowser控件(通过查询字符串?)来加载特定的网页。到目前为止我的代码如下

MainPage.xaml.cs中

public partial class MainPage : PhoneApplicationPage
{
    //for 'pin to start' webbrowser instance
    private const string _key = "url";
    private string _url;

    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }


    //OnNavigatedFrom method
    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        //base.OnNavigatedFrom(e);
        try
        {
            if (_url != null)
            {
                this.State[_key] = new MainPage
                {
                    _url = TheBrowser.Url.AbsoluteUri.ToString()
                };
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }


    //OnNavigatedTo method
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        //base.OnNavigatedTo(e);

        // See whether the Tile is pinned
        // (User may have deleted it manually.)
        //ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("DefaultTitle=FromTile"));


        //for "pin to start" webbrowser instances
        //if this page was activated from a tile, launch a request for the current 
        //web address at the location indicated in the query string
        if (NavigationContext.QueryString.ContainsKey(_key))
        {
            string url = NavigationContext.QueryString[_key];

            //if url is absoluteuri, open webbrowser and direct to absoluteuri
            if (!TheBrowser.InitialUri.Equals(TheBrowser.InitialUri))
            {
                TheBrowser.Navigate(url);
            }

            //remove the url from the querystring (important!!)
            NavigationContext.QueryString.Remove(_key);
        }

        //otherwise check to see if the app needs to be untombstoned
        //and restore it to its pretombstoned state if it does
        //else if (_url == null)
        else if (_url == null && this.State.ContainsKey(_key))            
        {
            MainPage mainPage = this.State[_key] as MainPage;
            //TheBrowser.Navigate(TheBrowser.InitialUri);
            _url = (string)mainPage.State[_key];
            TheBrowser.Navigate(_url);
        }
    }    


    //Application Tile
    private void SetApplicationTile(object sender, EventArgs e)
    {
        int newCount = 0;
        string appName = "Quest";

        // Application Tile is always the first Tile, even if it is not pinned to Start.
        ShellTile TileToFind = ShellTile.ActiveTiles.First();

        // Application should always be found
        if (TileToFind != null)
        {
            // Set the properties to update for the Application Tile.
            // Empty strings for the text values and URIs will result in the property being cleared.
            StandardTileData NewTileData = new StandardTileData
            {
                Title = appName,
                BackgroundImage = new Uri("/Background.png", UriKind.Relative),
                Count = newCount,
                BackTitle = appName,
                BackBackgroundImage = new Uri("", UriKind.Relative),
                BackContent = "flipside"
            };

            // Update the Application Tile
            TileToFind.Update(NewTileData);
        }

    }

    //Secondary Tile(s)
    private void PinToStart_Click(object sender, EventArgs e)
    {             
        //Look to see whether the Tile already exists and if so, don't try to create it again.
        // if the Tile doesn't exist, create it

        //if (!String.IsNullOrEmpty(_url))
        //{
            // Look to see whether the Tile already exists and if so, don't try to create it again.
            ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("url=" + _url));
            //ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("_url"));

            // Create the Tile if we didn't find that it already exists.
            if (TileToFind == null)
            {
                StandardTileData NewTileData = new StandardTileData
                {
                    BackgroundImage = new Uri("Background.png", UriKind.Relative),
                    Title = "link",
                    Count = 1,
                    BackTitle = "Quest",
                    BackContent = (string)_url,
                    BackBackgroundImage = new Uri("", UriKind.Relative)
                };

                // Create the Tile and pin it to Start. This will cause a navigation to Start and a deactivation of our application.
                ShellTile.Create(new Uri("/MainPage.xaml?" + _key + "=" + _url, UriKind.Relative), NewTileData);
                //ShellTile.Create(new Uri("/MainPage.xaml?_url", UriKind.Relative), NewTileData);
            }                  
        //}
    }
}

正如您所看到的,我是次要磁贴创建和实现的新手。我一直在玩正确的结构,我正在尝试使用查询字符串来传递带有辅助磁贴的url,以便在MainPage.xaml上使用正确的网站加载webbrowser。到目前为止,我实际上创建了一个辅助磁贴并确实将我带回到MainPage.xaml,但是我的webbrowser的一个新实例被设置为初始值http://www.bing.com。 任何有关这方面的帮助都将非常感激。我已经研究了一段时间,并且已经看到了为某些xaml页面创建辅助磁贴的几种方法,但没有任何东西需要使用某个URL加载webbrowser控件。我需要快速实施这个解决方案!你能不能还包括代码的变化,因为我绝对是wp7的新手!在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

我是MegaTile的开发者之一(完全按照你的描述完成)。

我们有两页:

  • Main.xaml - 这可以管理磁贴并设置操作
  • CallbackHandler.xaml - 它处理从辅助磁贴启动。

当我们创建一个新的磁贴(你的PinToStart_Click)时,我们创建了一个回调:

ShellTile.Create(new Uri("/CallbackHandler.xaml?Action=" + _action, UriKind.Relative), NewTileData);

然后在CallbackHandler.xaml.cs PhoneApplicationPage_Loaded中我们做了相应的操作:

try
{
    UriBuilder b = new UriBuilder(extraData);
    new WebBrowserTask { Uri = b.Uri }.Show();
}
catch (Exception ex)
{
   // something useful
}

修改:多一点时间让我的示例有效:

XAML:

<!--ContentPanel - place additional content here-->
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" >
     <TextBox x:Name="url" Text="http://www.linknode.co.uk" />
     <Button Content="Create Tile" Click="PinToStart_Click" />
     <phone:WebBrowser x:Name="TheBrowser" Height="400" />
</StackPanel>

C#

public partial class MainPage : PhoneApplicationPage
{

    private const string _key = "url";

    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    //OnNavigatedTo method
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        //if this page was activated from a tile it will contain a Querystring value of _key
        // launch a request for the current web address at the location indicated in the query string
        if (NavigationContext.QueryString.ContainsKey(_key))
        {
            string url = NavigationContext.QueryString[_key];
            TheBrowser.Navigate(new Uri(url));
        }
    }


    private void PinToStart_Click(object sender, RoutedEventArgs e)
    {
        string _url = url.Text;

        ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("url=" + _url));

        // Create the Tile if we didn't find that it already exists.
        if (TileToFind == null)
        {
            StandardTileData NewTileData = new StandardTileData
            {
                BackgroundImage = new Uri("Background.png", UriKind.Relative),
                Title = string.Format("link - {0}", _url),
                Count = 1,
                BackTitle = "Quest",
                BackContent = (string)_url,
                BackBackgroundImage = new Uri("", UriKind.Relative)
            };

            // Create the Tile and pin it to Start. This will cause a navigation to Start and a deactivation of our application.
            ShellTile.Create(new Uri("/MainPage.xaml?" + _key + "=" + _url, UriKind.Relative), NewTileData);
        }
        else
        {
            MessageBox.Show("Tile already exists");
        }
    }
}