我有一个用c#编写的程序,其中包含一个带有可观察集合的列表框。我想为用户弹出一个气球(在系统托盘中),当集合中添加了一个项目。我有一个类,其中包含一个方法,用于在添加项时通知列表框(如下所示):
void OnFileCreated(object sender, FileSystemEventArgs e)
{
if (!base.Dispatcher.CheckAccess())
{
base.Dispatcher.BeginInvoke(
DispatcherPriority.Normal,
(FileSystemEventHandler)OnFileCreated,
sender, e);
}
else
{
// Ignore new directories.
if (File.Exists(e.FullPath))
{
Debug.WriteLine("File Created: " + e.FullPath);
_files.Add(new ObservableFileInfo(e.FullPath));
//Alert users to new request
string title = "Access Request";
string text = "A new access request has been submitted";
//show balloon with built-in icon
tbi.ShowBalloonTip(title, text, BalloonIcon.Error);
}
}
}除了只有当我在OnFileCreated方法中创建气球的新实例时气球才会显示之外,代码完全按照预期工作(在我取出代码时没有看到上面的气球)。作为参考,我使用以下项目中的系统托盘图标.dll:http://www.codeproject.com/Articles/36468/WPF-NotifyIcon?fid=1540774&select=4624878&fr=51#xx0xx
我的问题是,我已经在MainWindow类中初始化了一个系统托盘图标,但我不能从我的OnFileCreated方法调用它来显示气球。我不确定如何才能在类之间“共享”这些信息。
如果任何人有任何想法,那将是伟大的。
发布于 2014-03-26 06:00:40
最后,我使用FindResource来定位在xaml资源文档中定义的资源。这是在我引用的项目代码中指定的,但是FindResource本身并不工作,我需要添加App.Current:
notifyIcon = (TaskbarIcon)App.Current.FindResource("NotifyIcon");
notifyIcon.ShowBalloonTip(title, text, BalloonIcon.Error); https://stackoverflow.com/questions/22625051
复制相似问题