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。。我好几次忘记了。。