public void SubscribeToNewsHeadlineStream() { string searchKeywords = SearchTextBox.Text; App.ctx.BeginLogIn(App.USERNAME, App.PASSWORD, a => { App.ctx.EndLogIn(a); //Next we create a connection to the streaming api, using the authenticated session streamingClient = App.StreamingClient; streamingClient.Connect(); //And instantiate a listener for news headlines on the appropriate topic //You can have multiple listeners on one connection newsListener = streamingClient.BuildNewsHeadlinesListener(searchKeywords); newsListener.Start(); //The MessageRecieved event will be triggered every time a new News headline is available, //so attach a handler for that event, and wait until something comes through NewsDTO recievedNewsHeadline = null; newsListener.MessageReceived += (s, e) => { recievedNewsHeadline = e.Data; //Add this new news headline to the main items collection. ItemViewModel item = new ItemViewModel(); item.Headline = recievedNewsHeadline.Headline; item.PublishDate = recievedNewsHeadline.PublishDate.ToString(); recievedNewsHeadline.StoryId = recievedNewsHeadline.StoryId; App.ViewModel.LoadData(item); }; }, null); }
/// <summary> /// Adds given item to news item collection /// </summary> /// <param name="item"></param> public void LoadData(ItemViewModel item) { Deployment.Current.Dispatcher.BeginInvoke(() => { Items.Add(item); }); IsDataLoaded = true; }