写一下如何提高代码的复用性

namespace JusMeeting.Src.CustomControls
{
    /// <summary>
    /// MessageWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MessageWindow : Window
    {
        
        public MessageWindow()
        {
            InitializeComponent();
        }

        public static void Show(string text)
        {
            Show(null, text, 0, 0);
        }

        public static void Show(string imagepath, string text)
        {
            Show(imagepath, text, 0, 0);
        }

        public static void Show(string text, int hidetime, int screenindex)
        {
            Show(null, text, hidetime, screenindex);
        }

        public static void Show(string imagepath, string text, int hidetime, int screenindex)
        {
            //参数分别为图片路径,显示文字,显示后几秒后消失(0及以下为不消失),显示在哪一个屏幕(下标)
            if (screenindex >= Screen.AllScreens.Count())
            {
                /*数组越界,返回默认屏幕*/
                screenindex = 0;
            }
            MessageWindow window = new MessageWindow();
            window.MessageText.Text = text;
            if (!string.IsNullOrEmpty(imagepath))
            {
                Uri uri = new Uri(imagepath);
                window.MessageImage.Source = new BitmapImage(uri);
            }
            else
            {
                window.MessageImage.Visibility = Visibility.Collapsed;
                window.TextGrid.Margin = new Thickness(16, 0, 16, 0);//将图片空间隐藏,文字提前
            }
            if (hidetime > 0)
            {
                window.starttimer(hidetime);
            }
            double pW = SystemParameters.PrimaryScreenWidth; //放大后的像素值
            double pW1 = WindowsApi.PrimaryScreenWidth(); //主显示器实际分辨率     
            System.Drawing.Rectangle sBounds = Screen.AllScreens[screenindex].Bounds;
            window.Left = sBounds.X * pW / pW1 + (sBounds.Width * pW / pW1 - window.Width) / 2;
            window.Top = sBounds.Top * pW / pW1 + 70;
            window.Topmost = true;
            window.ShowInTaskbar = false;
            window.Show();
        }
        private void starttimer(int time)
        {
            DispatcherTimer hidetimer = new DispatcherTimer();
            hidetimer.Interval = TimeSpan.FromSeconds(time);
            hidetimer.Tick += (s, e) =>
            {
                hidetimer?.Stop();
                this.Close();
            };
            hidetimer.Start();
        } 

    }
}

简单来说,就是先写一个参数最多最复杂的,然后写参数少的,在参数少的里面调用参数多的,缺少的参数补null或者0

同时要记得在参数最多的里面写好每种参数为0和null时候的处理逻辑

这上面还有一个定时器时间被一个隐式函数订阅了,隐式函数就是没有函数名,只有参数,s和e就是参数,然后代码写在=>后面

哦对了,定时器创建好之后记得start。。我好几次忘记了。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值