Я пытаюсь создать ненавязчивый способ отображения незначительных сообщений об ошибках пользователю. Поэтому я добавил статусную строку в свою форму,
<StatusBar Margin="0,288,0,0" Name="statusBar" Height="23" VerticalAlignment="Bottom">
<TextBlock Name="statusText">Ready.</TextBlock>
</StatusBar>
Затем, когда они нажимают кнопку "Добавить", он должен делать что-то или отображать сообщение об ошибке:
private void DownloadButton_Click(object sender, RoutedEventArgs e)
{
addressBar.Focus();
var url = addressBar.Text.Trim();
if (string.IsNullOrEmpty(url))
{
statusText.Text = "Nothing to add.";
return;
}
if (!url.Contains('.'))
{
statusText.Text = "Invalid URL format.";
return;
}
if (!Regex.IsMatch(url, @"^\w://")) url = "http://" + url;
addressBar.Text = "";
Но сообщение просто сидит там для жизни приложения... Я думаю, что я должен reset после примерно 5 секунд... как я могу установить таймер для этого?
Бонус: Как я могу придать ему отличный эффект постепенного исчезновения?
Я создал System.Timers.Timer
,
private Timer _resetStatusTimer = new Timer(5000);
void _resetStatusTimer_Elapsed(object sender, ElapsedEventArgs e)
{
statusText.Text = "Ready";
}
Но событие Elapsed
работает в другом потоке, чем пользовательский интерфейс, который ему не нравится... как мне обойти это?