C# 将webbrowser实例固定到开始屏幕

C# 将webbrowser实例固定到开始屏幕,c#,windows-phone-7,C#,Windows Phone 7,基本上,我正在尝试完成的是将webbrowser实例固定到我的windows phone开始屏幕上,然后当有人想要返回到固定的特定站点时,用户将单击互动程序,并在应用程序重新加载时被带到我应用程序中的同一网页。我在互联网上研究过这项功能,但没有发现任何利用这项功能的情况,但我在市场上的一些应用程序上看到了这项功能的应用。 我试图引用某人的类似实现,使用querystring获取所需的数据来告诉应用程序如何从辅助磁贴加载,但我可能有不正确的地方。此外,我可以告诉辅助互动程序在我的应用程序中加载包含

基本上,我正在尝试完成的是将webbrowser实例固定到我的windows phone开始屏幕上,然后当有人想要返回到固定的特定站点时,用户将单击互动程序,并在应用程序重新加载时被带到我应用程序中的同一网页。我在互联网上研究过这项功能,但没有发现任何利用这项功能的情况,但我在市场上的一些应用程序上看到了这项功能的应用。 我试图引用某人的类似实现,使用querystring获取所需的数据来告诉应用程序如何从辅助磁贴加载,但我可能有不正确的地方。此外,我可以告诉辅助互动程序在我的应用程序中加载包含webbrowser控件的主页,但我还没有弄清楚如何发送指向webbrowser控件的链接(通过querystring?)以加载特定网页。到目前为止,我的代码如下

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);
            }                  
        //}
    }
}
如您所见,我不熟悉辅助磁贴的创建和实现。我一直在使用正确的结构,我正在尝试使用querystring传递带有辅助互动程序的url,以便在MainPage.xaml上加载带有正确网站的webbrowser。到目前为止,我所拥有的实际上是创建了一个辅助磁贴,并将我带回MainPage.xaml,但我的webbrowser的一个新实例设置为的initialuri。
在此方面的任何帮助都将不胜感激。我已经为此工作了一段时间,并且已经看到了几种为某些xaml页面创建辅助磁贴的方法,但是不需要加载带有特定url的webbrowser控件。我需要快速实施此解决方案!请您也包括在代码中的更改,因为我肯定是wp7的新手!提前感谢您的帮助。

我是MegaTile上的开发人员之一(这正是您上面描述的)

我们有两页:

  • Main.xaml-这将管理磁贴并设置操作
  • CallbackHandler.xaml-处理从辅助磁贴启动的操作
当我们创建一个新的互动程序(您的PinToStart_单击)时,我们将回调创建为:

ShellTile.Create(new Uri("/CallbackHandler.xaml?Action=" + _action, UriKind.Relative), NewTileData);
然后在加载的CallbackHandler.xaml.cs PhoneApplicationPage_中执行适当的操作:

try
{
    UriBuilder b = new UriBuilder(extraData);
    new WebBrowserTask { Uri = b.Uri }.Show();
}
catch (Exception ex)
{
   // something useful
}
编辑:多花一点时间,所以我让您的示例生效:

XAML:


我可能忽略了声明我的webbrowsercontrol位于父项目之外的UserControl中,并且被引用并放置在父项目MainPage.xaml中,这就是为什么在从辅助磁贴导航到MainPage.xaml时,它会加载带有初始uri的webbrowser的新实例。我不完全确定我是否理解您想要做什么,但您可以从一个应用程序动态创建多个磁贴,它可以固定在开始屏幕上,但我不确定你是否可以让它们自动固定在开始屏幕上!我用一个测试应用程序测试了您的实现,结果很成功。谢谢你的辛勤工作和帮助@Matthew您可以将您的pinning web浏览器实例源代码作为示例项目上传到dropbox中,还是通过电子邮件发送给我?”raghavswaminathan@live.in"
<!--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>
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");
        }
    }
}