Just starting out with ReactiveUI. I am basically populating a grid with the results of a query. My viewmodel currently looks like this.
public class QueryViewModel : ViewModelBase
{
private readonly ResultSet _resultSet = null;
public ReactiveCollection<GridColumn> Columns { get; private set; }
public ReactiveCollection<GridRow> Rows { get; private set; }
public QueryViewModel()
{
_resultSet = ... run query
Columns = new ReactiveCollection<GridColumn>(from c in _resultSet.Columns select new GridColumn(c));
Rows = new ReactiveCollection<GridRow>(from r in _resultSet.Rows select new GridRow(r));
//Rows = new ReactiveCollection<GridRow>();
//_resultSet.ObservableRows
// .Subscribe(r => Rows.Add(new GridRow(r)));
}
}
As you can see I can get it working when populating using an IEnumerable, but you can see from the commented out bit that I’m not quite sure of the way to populate from an IObservable.
Any chance someone could point me in the right direction, please.
EDIT:
For full disclosure, I’ve included the implementation of ObservableRows in case I’m making a mistake here.
Return Observable.Create<Row>((o) => {
var cn = New SqlConnection(connection);
var cmd = New SqlCommand(sql, cn);
var asyncReader = Observable.FromAsyncPattern<SqlDataReader>(cmd.BeginExecuteReader, cmd.EndExecuteReader);
IDisposable subscription;
Try {
cn.Open();
subscription = asyncReader().Subscribe(rdr => PublishRows(rdr, o, columns));
}
Catch Exception ex {
o.OnError(ex);
}
Return Disposable.Create(() => {
subscription.Dispose();
cmd.Dispose();
cn.Close();
cn.Dispose();
});
}
Many thx
S
How about: