Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用App.xaml.cs创建类库的静态实例_C#_Wpf - Fatal编程技术网

C# 使用App.xaml.cs创建类库的静态实例

C# 使用App.xaml.cs创建类库的静态实例,c#,wpf,C#,Wpf,我有一个名为SGDataLibrary的类库,其中包含一个类SGMemberDataOp.cs,该类保存所有数据操作,并通过app.config获取到数据库的连接字符串 这是连接字符串 <add name="SimpleGym.Properties.Settings.SGYMConnectionString" connectionString="Data Source=owner\sqlexpress;Initial Catalog=SGYM;Integrated Sec

我有一个名为
SGDataLibrary
的类库,其中包含一个类
SGMemberDataOp.cs
,该类保存所有数据操作,并通过
app.config
获取到数据库的连接字符串

这是连接字符串

 <add name="SimpleGym.Properties.Settings.SGYMConnectionString"
        connectionString="Data Source=owner\sqlexpress;Initial Catalog=SGYM;Integrated Security=True"
        providerName="System.Data.SqlClient" />
下面是我在
App.xaml.cs
文件中编写的代码

 public partial class App : Application
 {
     ---->private static SGMemberDataOp sgMemberDataOp = new SGMemberDataOp();
     public static SGMemberDataOp SGMemberDataOp
     {
        get { return sgMemberDataOp; }
     }
 }
带箭头的行正在引发异常

对象引用未设置为对象的实例


我只是看了一些代码,我这样做,但

private static SGMemberDataOp sgMemberDataOp;

 public static SGMemberDataOp SGMemberDataOp
 {
    get { return sgMemberDataOp; }
 }

public App()
{
    try
    {
        sgMemberDataOp = new SGMemberDataOp();
    }
    catch (Exception Ex)
    {
        MessageBox.Show(Ex.Message, "Startup failed", MessageBoxButton.OK);
    }
}

将其作为私有文件更新不起作用,也不确定原因。

几乎所有NullReferenceException的情况都是相同的。请参阅“”,以获得一些提示。感谢将您的帮助标记为正确,但我是个傻瓜,在引用中添加了错误的dll…讨厌我这样做的时候…再次感谢您的帮助
private static SGMemberDataOp sgMemberDataOp;

 public static SGMemberDataOp SGMemberDataOp
 {
    get { return sgMemberDataOp; }
 }

public App()
{
    try
    {
        sgMemberDataOp = new SGMemberDataOp();
    }
    catch (Exception Ex)
    {
        MessageBox.Show(Ex.Message, "Startup failed", MessageBoxButton.OK);
    }
}