2015-06-26 65 views
0

我有一個MasterDetail UI設置。在左邊是一個ListView。 ListView中設置這樣通過Xamarin表單在頁面之間傳遞URL

public class MasterPageList 
{ 
    public string Text { get; set; } 

    public string Url { get; set; } 

    public override string ToString() 
    { 
     return Text; 
    } 
} 

     var listOptions = new List<MasterPageList> 
     { 
      new MasterPageList{ Text = "Packt website", Url = "http://www.packt.com" }, 
      new MasterPageList{ Text = "Farmtrack Live", Url = "http://www.farmapps.co.uk" }, 
      new MasterPageList{ Text = "Henry Crunn", Url = "" } 
     }; 

     // create the ListView - this is explained later in the book more fully in the What A Bind chapter 

     var listView = new ListView 
     { 
      ItemsSource = listOptions.ToArray() 
     }; 

     // create the master page 
     Master = new ContentPage 
     { 
      Title = "Webviewer", 
      Content = new StackLayout 
      { 
       Children = 
       { 
        header, listView 
       } 
      } 
     }; 

的ItemSelected事件觸發關閉收件人頁

void LaunchPage(object s, SelectedItemChangedEventArgs e) 
    { 
     var binding = e.SelectedItem as MasterPageList; 

     var displayPage = new Page(); 

     if (string.IsNullOrEmpty(binding.Url)) 
     { 
      displayPage = (Page)Activator.CreateInstance(typeof(WebviewGenerated)); 
     } 
     else 
     { 
      displayPage.BindingContext = e.SelectedItem; 
      displayPage = (Page)Activator.CreateInstance(typeof(Webview)); 
     } 

     Detail = new NavigationPage(displayPage); 
     IsPresented = false; 
    } 

當任前兩個listItems中被選擇,則接收ContentPage被實例化,但對的BindingContext頁面爲空。我已經嘗試了許多其他方法來從發送頁面獲取URL。

從我能從其他例子中看到的,我所做的是正確的,但結果顯示不然。

有什麼我需要做的將URL傳遞到網頁瀏覽頁面?

+0

您正在設置結合上下文,則指定頁面的一個完全新的實例。設置頁面實例FIRST,然後設置綁定上下文 – Jason

+0

如果我有Detail.BindingContext = e.SelectedItem;接收ContentPage中的BindingContext仍然爲空(我也嘗試在if/else條件中交換displayPage行的順序 - 沒有任何區別)。它看起來好像(頁面)Activator.CreateInstance正在開始新的頁面(displayPage =(頁面)從未命中後的斷點) – Nodoid

+0

if(string.IsNullOrEmpty(binding.Url)) Detail = new NavigationPage(新的WebviewGenerated()); else Detail = new NavigationPage(new Webview()) { BindingContext = e.SelectedItem }; - 也給出了BindingContext = null – Nodoid

回答

0

嘗試以下操作:

 var params = new Params { 
      url = "https://www.google.com", 
      querystring = "" 
     }; 

     var secondPage = new SecondPage(); 
     secondPage.BindingContext = params; 
     await Navigation.PushAsync (secondPage);