c# - How Can I Unfold This Event into an Observable Sequence? -
well, when think i've got handle on things, thrown curve.
i'm trying create observable sequence instead of doing this:
((outlook.mapifolderevents_12_event)calendarfolder).beforeitemmove += calendar_beforeitemmove; private void calendar_beforeitemmove( object item, outlook.mapifolder destfolder, ref bool cancel){ /*...*/ }
i'm trying use observable.fromeventpattern<tdelegate, teventargs> (func<eventhandler<teventargs>, tdelegate>, action<tdelegate>, action<tdelegate>)
method, because of required parameters i'm meeting little success. closest have been able come is:
var itembeforemovedobservable = observable .fromevent<outlook.mapifolderevents_12_beforeitemmoveeventhandler, object>(handler => { outlook.mapifolderevents_12_beforeitemmoveeventhandler bimeventhandler = (obj, f, ref cx) => // <-- cannot resolve symbol 'cx'; identifier expected { handler(obj); // <-- shouldn't have other parameters well? }; return bimeventhandler; }, h => ((outlook.mapifolderevents_12_event)calendarfolder).beforeitemmove += h, h => ((outlook.mapifolderevents_12_event)calendarfolder).beforeitemmove -= h);
thanks in advance guidance.
i don't have access way test would best off wrapping operator:
//because naming ridiculous using itemmoveeventhandler = outlook.mapifolderevents_12_beforeitemmoveeventhandler; using itemmoveevent = outlook.mapifolderevents_12_event; //used wrap multiple arguments single object public class mymoveeventargs { public mymoveeventargs(object item, mapifolder folder) { this.item = item; this.folder = folder; } public object item {get; private set;} public mapifolder folder {get; private set;} } //create extension method public static iobservable<mymoveeventargs> beforemoveobservable( itemmoveevent folder, func<object, mapifolder, bool> shouldcancel) { return observable.fromevent<itemmoveeventhandler, moveeventargs> //use conversion overload (emit => new itemmoveeventhandler( //the compiler seems complain cancel //being implicit, explicitly declare params (object item, mapifolder f, out bool cancel) => { //cancel needs set somewhere in body if (!(cancel = shouldcancel(item, f)) { emit(new mymoveeventargs(item, f)); } } }, h => folder.beforeitemmove += h, h => folder.beforeitemmove -= h); }
then should able use (untested):
calendarfolder.beforemoveobservable((obj, folder => /*return if event should cancelled or not*/) .subscribe(x => /*only reach here if move successful*/);
Comments
Post a Comment