windows phone 8.0中使用c#的TextBlock中的超链接
本文关键字:TextBlock 超链接 phone windows | 更新日期: 2023-09-27 18:13:29
如何在windows phone 8.0中使用c#生成从文本框到文本块的超链接
ex:- I enter
www.google.com在文本框中点击一个又一个按钮,点击
结果应该是
www.google.com
textblock
您可以轻松地将Hyperlink放入RichTextBlock(在WP8.1 Runtime中)。我也把Run
在超链接,所以它更容易管理其内容。例子:
<StackPanel>
<TextBox Name="myTextBox" Width="200"/>
<RichTextBlock TextWrapping="Wrap" VerticalAlignment="Center" TextAlignment="Center">
<Paragraph>
<Run Text="This is a link to google:"/>
<LineBreak/>
<Hyperlink x:Name="myhyperlink" Click="myhyperlink_Click">
<Run x:Name="hyperText" Text="textInside"/>
</Hyperlink>
<LineBreak/>
<Run Text="you can click it to invoke doEvent in your code."/>
</Paragraph>
</RichTextBlock>
</StackPanel>
后面的代码中有一些逻辑示例:
public MainPage()
{
this.InitializeComponent();
myTextBox.TextChanged += (sender, e) => hyperText.Text = myTextBox.Text;
}
private async void myhyperlink_Click(Windows.UI.Xaml.Documents.Hyperlink sender, Windows.UI.Xaml.Documents.HyperlinkClickEventArgs args)
{
await Windows.System.Launcher.LaunchUriAsync(new Uri(@"http://" + myTextBox.Text));
}
注意,在WP8.0和WP8.1 Silverlight你将不得不使用RichTextBox与IsReadOnly = true
使用HyperlinkButton
控件
<HyperlinkButton NavigateUri="http://www.google.com">
<HyperlinkButton.Content>
<TextBlock Text="google.com" />
</HyperlinkButton.Content>
</HyperlinkButton>
try this:
XAML:<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0">
<StackPanel x:Name="stack">
<TextBox x:Name="txtInput"></TextBox>
<Button Content="Create Link" Click="Button_Click"/>
</StackPanel>
</Grid>
CS: private void Button_Click(object sender, RoutedEventArgs e)
{
if (txtInput.Text != "")
{
HyperlinkButton obj = new HyperlinkButton();
obj.NavigateUri = new Uri(txtInput.Text,UriKind.RelativeOrAbsolute);
obj.Content = txtInput.Text;
obj.TargetName = "_blank";
this.stack.Children.Add(obj);
}
}
例如,在文本框
http://google.com
试试这个
xaml
<StackPanel x:Name="stack">
<TextBlock Text="{Binding LineThree}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBox x:Name="txtInput"></TextBox>
<Button Content="Create Link" Click="Button_Click"/>
<RichTextBox x:Name="textBox" ></RichTextBox>
</StackPanel>
和cs文件中的按钮点击
private void Button_Click(object sender, RoutedEventArgs e)
{
Hyperlink hyperlink = new Hyperlink();
hyperlink.Inlines.Add(txtInput.Text);
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(hyperlink);
textBox.Blocks.Add(myParagraph);
}