C/C++教程

WPF 用command打开超链接Hyperlink

本文主要是介绍WPF 用command打开超链接Hyperlink,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

看的其他大佬的代码,根据自己需要实现的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

 

这篇关于WPF 用command打开超链接Hyperlink的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!