【问题标题】:Using multiple Projects in NotifyIcon Application在 NotifyIcon 应用程序中使用多个项目
【发布时间】:2012-12-22 15:44:05
【问题描述】:

所以我遇到了所有问题中最奇怪的问题。我只是想写一个简单的 c# WinForms 工具供个人使用时遇到它。我将从头开始,真正的问题将在最后。

所以我做了一个 NotifyIcon 应用程序。这个很简单,我就是这么做的:

public class MyApp: ApplicationContext
{
    NotifyIcon icon;
    public MyApp()
    {
        // ...
    }

    // ...
}

并像启动其他所有 WinForms 应用程序一样启动该应用程序:

Application.Run(new MyApp());

现在我添加了一个新的 MyApp.Data 类项目,其中包含一个本地数据库,并使用 edmx 帮助程序来生成我的 EF 模型。我添加了一个名为 Database 的静态类,它应该包含我的所有查询。

public static class DataBase
{
    static MyAppModel db = new MyAppModel();

    public static void AddEntry(SomeThing st) 
    {
        db.SomeThings.AddObject(st);
        db.SaveChanges();
    }

    public static String[] GetSomeThings(int numberOfEntries)
    {
        return db.SomeThings.OrderBy(x => x.Date).Select(x => x.Title).Take(numberOfEntries).ToArray();
    }
}

回到 WinForms NotifyIcon 项目,我添加了 MyApp.Data 程序集(和 EF 程序集),当我调用 DataBase.AddEntry(x) 时一切正常,但当我使用 Database.GetSomeThings(10) 时,一切都中断了。

var x = Database.GetSomeThings(10);
String[] y = Database.GetSomeThings(10);

只是默默地失败而没有错误。 xy 都不会出现在本地人中,也无法观看。有人有想法吗?

编辑:我直接绑定到组合框,它神奇地工作。现在我打开了自动完成模式和源,我收到一条错误消息:

Für den aktuellen Thread muss der STA-Modus (Single Thread Apartment) festgelegt werden, bevor OLE-Aufrufe ausgeführt werden können。 Stellen Sie sicher, dass die Hauptfunktion mit STAThreadAttribute gekennzeichnet ist.

不幸的是德国人,但我想我可以解决这个问题:

http://msdn.microsoft.com/de-de/library/ms182351%28vs.80%29.aspx

【问题讨论】:

    标签: c# entity-framework project-management notifyicon applicationcontext


    【解决方案1】:

    所以我认为这解决了问题(为应用程序创建了一个起点,并且我将其设置为单例,其他一切都是愚蠢的恕我直言)。

        public static class Program
        {
            private static MyApp activeInstance;
    
            [STAThread]
            public static void Main()
            {
                if (activeInstance == null)
                {
                    activeInstance = new MyApp();
                    Application.Run(activeInstance);
                }
            }
        }
    

    不过,如果有人知道我在这里做了什么,我将不胜感激。那些[ tags ] 可能是编程中我不明白的最后一件事。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-04
      • 2023-03-22
      • 2014-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多