2012-04-10 81 views
2

我一直在嘗試將單個WP7應用程序頁面分隔爲兩個獨立的頁面,以便我可以將功能保留在一個頁面中,並將視圖保存在另一個頁面中。基本上,我創建了一個標籤式瀏覽器,其中有不同的標籤,可以根據用戶請求查看,只有當前點擊的標籤是顯示在視圖中的標籤。到目前爲止,我有一個解決方案,它可以在一個頁面上工作,但我想將它分離成一個TabsPage(它將控制標籤實例)和一個MainPage(它會向用戶顯示當前選擇的標籤)。我不確定如何正確分離這兩個頁面,以便我可以完成此功能。我有如下(其中迄今作品)正確地在頁面之間傳遞數據

MainPage.xaml中

<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <TextBox x:Name="UrlTextBox" KeyDown="UrlTextBox_KeyDown" InputScope="url"/> 
    <Grid x:Name="BrowserHost" Grid.Row="1" /> 
    <!--<my:FullWebBrowser Name="TheBrowser" Grid.Row="1"/>--> 

</Grid> 

<phone:PhoneApplicationPage.ApplicationBar> 
    <shell:ApplicationBar> 
     <shell:ApplicationBarIconButton x:Name="Tabs" IconUri="/Images/appbar.tab.rest.png" Text="tabs" Click="TabsPage_Click"/> 
     <shell:ApplicationBarIconButton IconUri="/Images/appbar.next.rest.png" IsEnabled="True" Text="forward" x:Name="ForwardBtn" Click="ForwardBtn_Click"/> 
     <shell:ApplicationBar.MenuItems> 
      <shell:ApplicationBarMenuItem Text="1" Click="TabMenuItem_Click" /> 
      <shell:ApplicationBarMenuItem Text="2" Click="TabMenuItem_Click" /> 
      <shell:ApplicationBarMenuItem Text="3" Click="TabMenuItem_Click" /> 
      <shell:ApplicationBarMenuItem Text="4" Click="TabMenuItem_Click" /> 
     </shell:ApplicationBar.MenuItems> 
    </shell:ApplicationBar> 
</phone:PhoneApplicationPage.ApplicationBar> 

MainPage.xaml.cs中

private const int NumTabs = 4; 
    private int currentIndex; 

    private string[] urls = new string[NumTabs]; 
    ////private WebBrowser[] browsers = new WebBrowser[NumTabs]; 
    private FullWebBrowser[] browsers = new FullWebBrowser[NumTabs]; 

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

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

    //} 

    private void ShowTab(int index) 
    { 
     this.currentIndex = index; 

     UrlTextBox.Text = this.urls[this.currentIndex] ?? ""; 

     if (this.browsers[this.currentIndex] == null) 
     { 
      //WebBrowser browser = new WebBrowser(); 
      FullWebBrowser browser = new FullWebBrowser(); 

      this.browsers[this.currentIndex] = browser; 

      BrowserHost.Children.Add(browser); 
     } 

     for (int i = 0; i < NumTabs; i++) 
     { 
      if (this.browsers[i] != null) 
      { 
       this.browsers[i].Visibility = i == this.currentIndex ? Visibility.Visible : Visibility.Collapsed; 
      } 
     } 
    } 

    private void UrlTextBox_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.Key == Key.Enter) 
     { 
      Uri url; 

      //string _url; 

      if (Uri.TryCreate(UrlTextBox.Text, UriKind.Absolute, out url)) 
      { 
       this.urls[this.currentIndex] = UrlTextBox.Text; 
       //_url = url.ToString(); 

       string _url = url.ToString(); 

       //this.browsers[this.currentIndex].Navigate(url); 
       this.browsers[this.currentIndex].Navigate(_url); 
      } 
      else 
       MessageBox.Show("Invalid url"); 
     } 
    } 

    private void TabMenuItem_Click(object sender, EventArgs e) 
    { 
     int index = Int32.Parse(((ApplicationBarMenuItem)sender).Text) - 1; 
     ShowTab(index); 
    } 

    //********************************************************************** 

    private void TabsPage_Click(object sender, EventArgs e) 
    { 
     this.NavigationService.Navigate(new Uri("/TabsPage.xaml", UriKind.Relative)); 
    } 

注:我使用一個名爲TheWebBrowser WebBrowser控件。 任何關於如何正確地將MainPage分隔成TabsPage的幫助都將不勝感激!我一直在研究這一段時間,似乎無法在頁面之間正確傳遞數據(命名爲搜索欄和在MainPage上查看當前Webbrowser實例)。

回答

1

頁面之間傳遞的價值觀,仍然堅持正確的WP7的設計可以通過添加一個保存點在App.xaml.cs文件來完成,然後您可以訪問使用

(App.Current as App).SomeField 

現在我有信息沒有使用您正在使用的瀏覽器控件的經驗,但我假設您可以使用MVVM模式將數據從視圖中分離出來。 This就是一個很好的例子。使用MVVM還將允許您使用您的應用程序better support tombstoning。希望這可以回答你的問題。

+0

謝謝@singelabs,你能告訴我如何使用,我從來沒有使用App.Current作爲應用程序之前?另外,如果只通過查詢字符串在兩頁之間傳遞數據來實現這一點,會不會有更簡單的方法? – Matthew 2012-04-11 23:42:44

1

我只是使用Caliburn Micro在頁面/屏幕之間傳遞數據。 http://caliburnmicro.codeplex.com/它也有一個很好的文檔,讓你開始學習如何正確傳遞數據,而不是做(App.Current as App).SomeField。

0

以防萬一,this can be helpful

編輯:我的意思是,你可以使用this component,它通過的NuGet可作爲

Install-Package SmartNavigation. 

可能是它有點生,但完全值得花費一目瞭然。

enter image description here

+0

儘管這個鏈接可能回答這個問題,但最好在這裏包含答案的基本部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – LittleBobbyTables 2013-05-10 16:13:24

+0

@LittleBobbyTables Roger-roger :) – 2013-05-10 16:30:18

0

這是我要做的事,當我想跨越例如頁面共享數據。定義你的財產在App.xaml.cs(不要忘了初始化你需要的地方):

private List<Contact> Contacts 
{ 
    get 
    { 
     return (App.Current as App).Contacts; 
    } 
} 

public List<Contact> Contacts 
{ 
    get 
    { 
     return _contacts; 
    } 
} 
您要使用此數據,定義它像這樣的頁面

現在