Ars*_*ray 149 c# wpf xaml hyperlink
我已经看到了一些建议,你可以通过Hyperlink
控件添加到WPF应用程序的超链接.
这是我在我的代码中尝试使用它的方式:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="BookmarkWizV2.InfoPanels.Windows.UrlProperties"
Title="UrlProperties" Height="754" Width="576">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Grid>
<ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.RowSpan="2">
<StackPanel >
<DockPanel LastChildFill="True" Margin="0,5">
<TextBlock Text="Url:" Margin="5"
DockPanel.Dock="Left" VerticalAlignment="Center"/>
<TextBox Width="Auto">
<Hyperlink NavigateUri="http://www.google.co.in">
Click here
</Hyperlink>
</TextBox>
</DockPanel >
</StackPanel>
</ScrollViewer>
</Grid>
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal" Margin="0,7,2,7" Grid.Row="1" >
<Button Margin="0,0,10,0">
<TextBlock Text="Accept" Margin="15,3" />
</Button>
<Button Margin="0,0,10,0">
<TextBlock Text="Cancel" Margin="15,3" />
</Button>
</StackPanel>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
属性"文本"不支持"超链接"类型的值.
我究竟做错了什么?
ean*_*son 307
如果您希望应用程序在Web浏览器中打开链接,则需要将具有RequestNavigate事件集的HyperLink添加到以编程方式打开以地址作为参数的Web浏览器的功能.
<TextBlock>
<Hyperlink NavigateUri="http://www.google.com" RequestNavigate="Hyperlink_RequestNavigate">
Click here
</Hyperlink>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
在代码隐藏中,您需要添加类似于此的东西来处理RequestNavigate事件.
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)
此外,您还需要以下导入.
using System.Diagnostics;
using System.Windows.Navigation;
Run Code Online (Sandbox Code Playgroud)
在您的应用程序中看起来像这样.
Art*_*nes 60
除了Fuji的响应之外,我们可以使处理程序可重用,将其转换为附加属性:
public static class HyperlinkExtensions
{
public static bool GetIsExternal(DependencyObject obj)
{
return (bool)obj.GetValue(IsExternalProperty);
}
public static void SetIsExternal(DependencyObject obj, bool value)
{
obj.SetValue(IsExternalProperty, value);
}
public static readonly DependencyProperty IsExternalProperty =
DependencyProperty.RegisterAttached("IsExternal", typeof(bool), typeof(HyperlinkExtensions), new UIPropertyMetadata(false, OnIsExternalChanged));
private static void OnIsExternalChanged(object sender, DependencyPropertyChangedEventArgs args)
{
var hyperlink = sender as Hyperlink;
if ((bool)args.NewValue)
hyperlink.RequestNavigate += Hyperlink_RequestNavigate;
else
hyperlink.RequestNavigate -= Hyperlink_RequestNavigate;
}
private static void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
<TextBlock>
<Hyperlink NavigateUri="http://stackoverflow.com" custom::HyperlinkExtensions.IsExternal="true">
Click here
</Hyperlink>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
Iva*_*čin 23
如果你想稍后本地化字符串,那么这些答案是不够的,我建议如下:
<TextBlock>
<Hyperlink NavigateUri="http://labsii.com/">
<Hyperlink.Inlines>
<Run Text="Click here"/>
</Hyperlink.Inlines>
</Hyperlink>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
may*_*ʎɐɯ 19
我在这个问题中使用了答案,但遇到了问题。
它返回异常: {"The system cannot find the file specified."}
经过一番调查。事实证明,如果你的 WPF 应用程序是 CORE,则需要更改UseShellExecute
为true
.
Microsoft文档中提到了这一点:
如果在启动进程时应该使用 shell,则为 true;如果进程应该直接从可执行文件创建,则为 false。.NET Framework 应用程序的默认值为 true,.NET Core 应用程序的默认值为 false。
因此,要完成这项工作,您需要添加UseShellExecute
并将其设置为true
:
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri){ UseShellExecute = true });
Run Code Online (Sandbox Code Playgroud)
Lu5*_*u55 17
恕我直言,最简单的方法是使用继承自的新控件Hyperlink
:
/// <summary>
/// Opens <see cref="Hyperlink.NavigateUri"/> in a default system browser
/// </summary>
public class ExternalBrowserHyperlink : Hyperlink
{
public ExternalBrowserHyperlink()
{
RequestNavigate += OnRequestNavigate;
}
private void OnRequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
}
Run Code Online (Sandbox Code Playgroud)
Dre*_*kes 15
Hyperlink
另请注意,不必用于导航.您可以将其连接到命令.
例如:
<TextBlock>
<Hyperlink Command="{Binding ClearCommand}">Clear</Hyperlink>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
175560 次 |
最近记录: |