孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

在上一篇WPF显示SQLITE数据(一)中,关于链接的字段,只是显示了下划线,单击后连接到浏览器的对应页面并没有实现,下面给出单击事件的实现

1.在xaml中写入事件定义

1 <DataGridHyperlinkColumn Header="URL地址" Width="940" Binding="{Binding url, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
2     <DataGridHyperlinkColumn.ElementStyle>
3         <Style>
4             <EventSetter Event="Hyperlink.Click" Handler="Hyperlink_Click"/>
5         </Style>
6     </DataGridHyperlinkColumn.ElementStyle>
7 </DataGridHyperlinkColumn>

2.在MainWindow.xaml.cs中写入

 1 private void Hyperlink_Click(object sender, RoutedEventArgs e)
 2 {
 3     System.Windows.Documents.Hyperlink link = (System.Windows.Documents.Hyperlink)e.OriginalSource;
 4     //Process.Start("IEXPLORE.EXE",link.NavigateUri.AbsoluteUri);
 5     string url = link.NavigateUri.AbsoluteUri;
 6     Process p = new Process();
 7     p.StartInfo.FileName = "cmd.exe";
 8     p.StartInfo.UseShellExecute = false; //不使用shell启动
 9     p.StartInfo.RedirectStandardInput = true;//喊cmd接受标准输入
10     p.StartInfo.RedirectStandardOutput = false;//不想听cmd讲话所以不要他输出
11     p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
12     p.StartInfo.CreateNoWindow = true;//不显示窗口
13     p.Start();
14 
15     //向cmd窗口发送输入信息 后面的&exit告诉cmd运行好之后就退出
16     p.StandardInput.WriteLine("start " + url + "&exit");
17     p.StandardInput.AutoFlush = true;
18     p.WaitForExit();//等待程序执行完退出进程
19     p.Close();
20 }

即可实现。

开始按照网上一些资料写Process.Start("IEXPLORE.EXE",link.NavigateUri.AbsoluteUri);,试了后发现会报错,查阅资料后,改为了模拟控制台的

start + url +&exit形式打开网页链接,测试成功!

posted on 2021-10-23 22:43  孤独的猫  阅读(234)  评论(0编辑  收藏  举报