2012-02-27 2 views
1

基本的に私が達成しようとしているのは、Windowsの電話の開始画面にWebブラウザインスタンスを固定しておき、固定された特定のサイトに戻る場合、そのタイルは、アプリケーションがリロードされたときに、そのアプリケーション内の同じWebページに移動されます。私はこの機能をインターネット上で研究しており、これを利用していませんでしたが、これは市場のいくつかのアプリで実行されています。 私は、querystringを使って他の人の実装を参照しようとしましたが、アプリケーションに二次タイルからのロード方法を伝えるのに必要なデータを取得しましたが、何かが間違っている可能性があります。また、セカンダリタイルに、Webブラウザコントロールを含むメインページをロードするように指示できますが、特定のWebページを読み込むためにウェブブラウザコントロールへのリンクをどのように送ることができません。ピンのWebブラウザインスタンスを起動する画面

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

あなたが見ることができるように、私は二タイルの作成と実装に新しいですが、次のように私のコードは、これまでのところです。私は正しい構造で遊んでいましたが、私は正しいウェブサイトでMainPage.xaml上のWebブラウザをロードするために、クエリーストリングを使用してセカンダリタイルでURLを渡そうとしています。私がこれまで行ってきたことは、実際にはセカンダリタイルを作成して、MainPage.xamlに戻しますが、初期ウェブブラウザhttp://www.bing.comに設定されているWebブラウザの新しいインスタンスを返します。 これに関するお手伝いがあれば幸いです。私はしばらくの間この作業をしてきましたが、特定のxamlページのセカンダリタイルを作成する方法はいくつかありましたが、Webブラウザコントロールに特定のURLをロードする必要はありません。私はすぐにこのソリューションを実装する必要があります!私はあなたがwp7の初心者なので、コードの変更も含めてください。あなたの非常に感謝の助けを前にありがとう。

+0

私は私のwebbrowsercontrolは、親プロジェクトの外でのUserControlであることを述べることを怠ってきたこと、そして私の親プロジェクトMainPage.xamlをで参照して配置され、そのMainPage.xamlがセカンダリタイルからナビゲートされるとすぐに、最初のuriを持つウェブブラウザの新しいインスタンスを読み込んでいる理由です。 – Matthew

+0

私はあなたが何をしたいのかよく理解していませんが、1つのアプリケーションから複数のタイルを動的に作成することができます。これは開始画面に固定することができます。http://msdn.microsoft.com/en-us/library/hh202979 (v = vs.92).aspx私はあなたが自動的にスタート画面にピンすることができるかどうかはわかりません。 – ameer

答えて

3

私はデベロッパーのMegaTile(これはあなたが上で説明したものとまったく同じです)の1つです。これは、タイルの管理を行い、アクション

  • CallbackHandler.xaml設定 - - これは、二次のタイルから起動を処理し

    • Main.xaml:

      は、我々は2つのページがあります。

      try 
      { 
          UriBuilder b = new UriBuilder(extraData); 
          new WebBrowserTask { Uri = b.Uri }.Show(); 
      } 
      catch (Exception ex) 
      { 
          // something useful 
      } 
      
      :我々は適切な処置を行うCallbackHandler.xaml.cs PhoneApplicationPage_Loadedで次に

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

    我々は新しいタイルを作成している(あなたのPinToStart_Clickは)我々はとしてコールバックを作成します

    を編集してください:私はあなたの事例を作ったので、もう一度:

    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"); 
         } 
        } 
    } 
    
  • +0

    はうまくいきます!私はあなたの実装をテストアプリケーションでテストしましたが、それは成功でした。あなたの努力と助けてくれてありがとう! – Matthew

    +0

    @Matthewドロップボックスに**サンプルプロジェクト**としてあなたの固定ウェブブラウザのインス​​タンスソースコードをアップロードすることができますか、それとも私に "[email protected]" –

    関連する問題