标签:ProcessStartInfo openForgetPWD Hyperlink url cmd command 超链接 new WPF
看的其他大佬的代码,根据自己需要实现的WPF 用command打开超链接Hyperlink的代码,仅用于交流学习,不要搬运(me vegetable,u know?),不要做小垃圾的搬运工!!!
xmal代码,binding控件自身,并赋值NavigateUri参数的值为目标网站地址(注意网站格式,可以在浏览器手动测试能否打开)
<TextBlock Grid.Row="5" Margin="0,3" HorizontalAlignment="Right" VerticalAlignment="Top" Focusable="False" FontSize="12"> <Hyperlink Command="{Binding OpenForgetPWD_Hyperlink_Command}" CommandParameter="{Binding RelativeSource={x:Static RelativeSource.Self}}" NavigateUri="https://www.cnblogs.com/sanmannn/"> 忘记密码 </Hyperlink> </TextBlock>
定义一个打开网页的函数,无返回值
/// <summary> /// 用浏览器打开网页 /// </summary> /// <param name="url"></param> public static void OpenInBrowser(string url) { //判断操作系统是否为 Windows if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { //修正格式 url = url.Replace("&", "^&"); // ProcessStartInfo的使用是必需的,因为我们需要用cmd命令打开目标网站 // 设置它的 CreateNoWindow = true以防止 cmd 窗口出现 //ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true }; Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true }); } }
最后在后端的Command命令,OpenInBrowser()函数定义在FuncHelper.cs里
#region<忘记密码 Hyperlink Command> private CommandBase _openForgetPWD_Hyperlink; public CommandBase OpenForgetPWD_Hyperlink_Command { get { if (_openForgetPWD_Hyperlink == null) { _openForgetPWD_Hyperlink = new CommandBase(); _openForgetPWD_Hyperlink.DoExecute = new Action<object>(obj => { //调用下就ok啦 FuncHelper.OpenInBrowser((obj as Hyperlink).NavigateUri.ToString()); }); } return _openForgetPWD_Hyperlink; } } #endregion
标签:ProcessStartInfo,openForgetPWD,Hyperlink,url,cmd,command,超链接,new,WPF 来源: https://www.cnblogs.com/sanmannn/p/16456257.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。