在C#中更新指示器标签

我创建了一个新的指标:
indicator = 
        new ApplicationIndicator (
            "sample-application",       //id of the the indicator icon
            "app-icon",                 //file name of the icon (will look for app-icon.png) 
            Category.ApplicationStatus, 
            ExecutableFolder            //the folder where to look for app-icon.png
        );  

        //Build Popup Menu for ApplicationIndicator
        Menu popupMenu = new Menu ();
        indicator.Label = "init label";
...

从一个定时器回调函数中,我想要更新指示器的标签。
indicator.label = "new label";

新的标签值未应用于指示器。它仍然显示“init label”字符串。

1关闭队列审查者,libappindicator 是 Ubuntu 中的一个上游项目(属于 Unity 用户服务 的一部分)。这应该不算离题,对吧? - user.dz
标签已更改,问题出在用户界面更新上! - Nasreddine
应该是 indicator.Label = "新标签";,其中 L 需要大写。 - user.dz
1个回答

我认为这只是个打字错误,应该是indicator.Label = "new label";,L应该大写。
下面是我完整的工作示范(在Ubuntu 14.04上测试过):
1. indicator_demo.cs
```csharp using Gtk; using AppIndicator; public class IndicatorExample { static Window win; static ApplicationIndicator indicator; static int c;
public static void Main() { Application.Init();
win = new Window("Test"); win.Resize(200, 200);
Label label = new Label(); label.Text = "Hello, world!";
win.Add(label);
indicator = new ApplicationIndicator("my-id", "my-name", Category.ApplicationStatus); indicator.Status = Status.Attention;
Menu menu = new Menu(); // menu.Append(new MenuItem("Foo")); // menu.Append(new MenuItem("Bar"));
indicator.Menu = menu; indicator.Menu.Show(); indicator.Label = "初始标签";
win.ShowAll();
indicator.Label = "label2"; c = 0; GLib.Timeout.Add(1000, new GLib.TimeoutHandler(update));
Application.Run(); }
public static bool update() { c += 1; indicator.Label = c.ToString();
return true; } } ```
这是对Ubuntu Wiki: Application Indicators C#示例的修改。
编译和运行以进行测试:
```bash dmcs -pkg:gtk-sharp-2.0 -pkg:appindicator-sharp-0.1 indicator_demo.cs mono indicator_demo.exe ```

1谢谢,解决方案运行得很完美!似乎使用 System.Timers.Timer 而不是 Glib.Timeout 会在更新指示器标签时出现问题。 - Nasreddine