2011-11-24 64 views
1

我正在嘗試構建一個請求服務器發送項目列表的應用程序;它們在windows phone頁面上顯示爲鏈接。我使用WebClient.UploadStringAsync來執行POST並獲取這些項目。 POST工作正常,我能夠得到迴應。在Windows Phone Silverlight應用程序中生成動態鏈接(超鏈接按鈕)

在UploadStringCompleted事件中創建鏈接並添加動態鏈接到堆棧面板的選項/可能性。HyperLinkBut​​ton是用來創建鏈接的選項;我也希望在鏈接被點擊。

我嘗試使用超鏈接中的HyperLinkBut​​ton,TextBlock,但沒有任何效果。

有什麼建議?


非常感謝您的建議;我做的錯誤是沒有設置HyperLinkBut​​ton的TargetName屬性。我得到它的工作後,我將它設置正確,

for (int i = 0; i <= itemList.GetUpperBound(0); i++) 
     { 

      if (!String.IsNullOrEmpty(itemList[i, 1])) 
       { 
        HyperlinkButton btn = new HyperlinkButton(); 
        btn.Content = itemList[i, 1]; 
        btn.TargetName = "_blank"; 
        ItemPanel.Children.Add(btn); 
       } 
} 

KRZ

回答

0

出了什麼問題只是使用HyperLink內聯元素?

<RichTextBox IsReadOnly="True"> 
    <Paragraph> 
     Displaying text with <Hyperlink NavigateUri="http://www.msdn.com">hyperlink</Hyperlink>. 
    </Paragraph> 
</RichTextBox> 

更多示例on MSDN

您可以在導航上附加事件處理程序,並在那裏執行POST請求。

0

您可以使用帶有數據綁定的ListBox而不是StackPanel。列表中的每一行將是一個HyperlinkButton

列表框與項模板:

<ListBox HorizontalAlignment="Stretch" Margin="12,6,0,0" Name="listBox1" VerticalAlignment="Top" VerticalContentAlignment="Stretch"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <HyperlinkButton Content="{Binding}" Click="HyperlinkButton_Click" /> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

後面的代碼:

// handler is executed when a HyperlinkButton was pressed by user 
private void HyperlinkButton_Click(object sender, RoutedEventArgs e) 
{ 
    // get the clicked button and show its content (which is the URL) 
    // (you would do your POST here) 
    MessageBox.Show("Clicked URL " + ((HyperlinkButton)sender).Content); 
} 

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    // prepare some demo data 
    List<string> urls = new List<string>(); 
    urls.Add("http://www.stackoverflow.com"); 
    urls.Add("http://www.google.com"); 

    // set list or URLs as item source of list box; each URL will have its own button 
    listBox1.ItemsSource = urls; 
} 
+0

MEEP。已經停止使用'HyperlinkBut​​ton'。芒果有一個元素,不需要密集的造型看起來可讀。 –

+0

我會嘗試這兩個選項以查看行爲和他們的表現。 @克勞斯你的意思是HyperLink是比Button更好的選擇。 – srcKode

+0

是的,非常非常。因爲它實際上是爲此目的而設計的。 HyperLinkBut​​ton是Silverlight的一個剩餘部分,它需要大量的樣式才能看清楚。 –