首页 > 解决方案 > Xamarin 表单中的 SqlDependency

问题描述

我是 Xamarin Forms 的新手,我在 Wpf 中使用 SqlDependency,效果很好。现在我想在 xamarin Forms 中使用 SqlDependency,它向我显示以下错误。

System.TypeLoadException:无法加载字段'MegaMartMobile.UnitEntrySN:_newMessage'(2)的类型,原因是:无法从typeref解析类型为0100004f的类型​​(程序集'System.Data中的预期类'System.Data.SqlClient.SqlNotificationEventArgs' .SqlClient,版本=4.5.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a') 程序集:System.Data.SqlClient,版本=4.5.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a 类型:System.Data.SqlClient.SqlNotificationEventArgs 成员: (无效的)

Bellow 是我的 Sql Notifier 代码

public class UnitEntrySN
{
    public SqlCommand CurrentCommand { get; set; }
    private SqlConnection mConnection;
    public SqlConnection CurrentConnection
    {
        get
        {
            mConnection = mConnection ?? new SqlConnection(SqlCustomConnection.ConnectionString);
            return mConnection;
        }
    }
    public UnitEntrySN()
    {


        SqlDependency.Start(SqlCustomConnection.ConnectionString);
    }
    private event EventHandler<SqlNotificationEventArgs> _newMessage;

    public event EventHandler<SqlNotificationEventArgs> NewMessage
    {
        add
        {
            _newMessage += value;
        }
        remove
        {
            _newMessage -= value;
        }
    }

    public virtual void OnNewMessage(SqlNotificationEventArgs notification)
    {
        _newMessage?.Invoke(this, notification);
    }
    public DataTable RegisterDependency()
    {

        CurrentCommand = new SqlCommand("Select [UnitEntry_ID], [UnitEntry_Title], [UnitEntry_Description] from dbo.UnitEntrys where [UnitEntry_ID] != '1'", CurrentConnection)
        {
            Notification = null
        };


        var dependency = new SqlDependency(CurrentCommand);
        dependency.OnChange += Dependency_OnChange;


        if (CurrentConnection.State == ConnectionState.Closed)
            CurrentConnection.Open();
        try
        {

            DataTable dt = new DataTable();
            dt.Load(CurrentCommand.ExecuteReader(CommandBehavior.CloseConnection));
            return dt;
        }
        catch (Exception ex) { }
        return null;
    }
    private void Dependency_OnChange(object sender, SqlNotificationEventArgs e)
    {
        var dependency = sender as SqlDependency;
        dependency.OnChange -= new OnChangeEventHandler(Dependency_OnChange);
        OnNewMessage(e);



    }



    #region IDisposable Members

    public void Dispose()
    {
        SqlDependency.Stop(SqlCustomConnection.ConnectionString);
    }

    #endregion
}

和视图模型中的代码

public UnitEntrySN Notifier { get; set; }
    private ObservableCollection<UnitEntryModel> mUnitEntrySource;
    public ObservableCollection<UnitEntryModel> UnitEntrySource
    {
        get
        {
            mUnitEntrySource = mUnitEntrySource ?? new ObservableCollection<UnitEntryModel>();
            return mUnitEntrySource;
        }
    }
    private void Notifier_NewMessageAsync(object sender, SqlNotificationEventArgs e)
    {

        this.LoadMessage(Notifier.RegisterDependency());


    }
    private void LoadMessage(DataTable dt)
    {
        if (dt != null)
        {
            UnitEntrySource.Clear();

            foreach (DataRow drow in dt.Rows)
            {
                var msg = new UnitEntryModel
                {

                    UnitEntry_Title = Convert.ToString(drow["UnitEntry_Title"])

                };
                UnitEntrySource.Add(msg);
            }
        }

    }

标签: c#xamarin.formssqldependency

解决方案


推荐阅读