[英]Sharing objects between classes
I have a program written in c# that contains a listbox with an observable collection. 我有一个用c#编写的程序,其中包含带有可观察集合的列表框。 I want a balloon to pop-up for the user (in the system tray) when the collection has an item added.
我希望在集合中添加项目时为用户弹出气球(在系统托盘中)。 I have a class that contains a method for notifying the listbox when an item is added (see below):
我有一个类,其中包含一种用于在添加项目时通知列表框的方法(请参见下文):
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);
}
}
}
The code works exactly as intended apart from the fact that the balloon will only show if I create a new instance of the balloon within the OnFileCreated method (not seen above as I took the code out). 除了仅在我在OnFileCreated方法中创建气球的新实例时才显示气球的事实之外,代码完全按预期的方式工作(在上面取出代码时未在上方看到)。 For reference, I am using the system tray icon .dll from the following project: http://www.codeproject.com/Articles/36468/WPF-NotifyIcon?fid=1540774&select=4624878&fr=51#xx0xx
作为参考,我使用以下项目中的系统托盘图标.dll: http : //www.codeproject.com/Articles/36468/WPF-NotifyIcon? fid=1540774&select=4624878&fr=51#xx0xx
My issue is that I already have a system tray icon initiated in the MainWindow class, but I cannot call on it to show a balloon from my OnFileCreated method. 我的问题是,我已经在MainWindow类中启动了系统任务栏图标,但是无法调用它来显示OnFileCreated方法中的气球。 I am not sure how I can "share" this information across classes.
我不确定如何在各个班级之间“共享”此信息。
If anyone has any ideas that would be great. 如果有人有什么好主意。
I ended up using FindResource to locate the resource defined in the xaml resource document. 我最终使用FindResource来定位xaml资源文档中定义的资源。 This was specified in the project code I was referencing to, however FindResource was not working by itself, I needed to add App.Current:
这是在我所引用的项目代码中指定的,但是FindResource本身无法正常工作,我需要添加App.Current:
notifyIcon = (TaskbarIcon)App.Current.FindResource("NotifyIcon");
notifyIcon.ShowBalloonTip(title, text, BalloonIcon.Error);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.