SqlDependency в формах Xamarin - PullRequest
       12

SqlDependency в формах Xamarin

0 голосов
/ 21 декабря 2018

Я новичок в Xamarin Forms, я использую SqlDependency в Wpf, который отлично работает.Теперь я хочу использовать SqlDependency в формах xamarin, которые показывают мне следующую ошибку:

System.TypeLoadException: Не удалось загрузить тип поля 'MegaMartMobile.UnitEntrySN: _newMessage' (2) из-за: Не удалосьразрешить тип с токеном 0100004f из typeref (ожидаемый класс 'System.Data.SqlClient.SqlNotificationEventArgs' в сборке 'System.Data.SqlClient, версия = 4.5.0.0, культура = нейтральная, PublicKeyToken = b03f5f7f11d50a3a'): сборка System.Data.SqlClient, Версия = 4.5.0.0, Культура = нейтральная, PublicKeyToken = b03f5f7f11d50a3a тип: System.Data.SqlClient.SqlNotificationEventArgs member: (null)

Ниже мой код 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);
            }
        }

    }
...