how ot make background agent to check for new feeds - c#

I want to make the background agent in my Windows Phone app check for new feeds in the background, i use webclient to download them and i display them in an listbox i use an webbrowser control to display the selected feed to the page it comes from via url i got from the syndicationitem, now i want to save lets say the title or the publishdate to isolated storage and that the background agent checks every 30 min. for feeds and checks if some new feed are available with comparing the the last feed Title or the last publishdate saved already and the newsest on the page, then if a newer feed is there it should send an toast notification with the title of the feed and open my app.
i have nothing done to save the feeds before, i dont know how to do this and i dont know how to use background agents and do thid what i wrote above. I use Microsofts example of an rss reader as background for my app logic. downloading, displaying all that like the sample here - http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh487167(v=vs.105).aspx
here is some code:
i use this to download the feeds
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new System.Uri("http://wpnovosti.com/feeds/posts/default?alt=rss"));
this i use to show them on my listbox and there is some logic of the live tiles too:
public void UpdateFeedList(string feedXML)
{
StringReader stringReader = new StringReader(feedXML);
XmlReader xmlReader = XmlReader.Create(stringReader);
SyndicationFeed feed = SyndicationFeed.Load(xmlReader);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// Bind the list of SyndicationItems to our ListBox.
feedListBox.ItemsSource = feed.Items;
SystemTray.SetProgressIndicator(this, null);
//Live Tiles
ShellTile appTile = ShellTile.ActiveTiles.First();
if (appTile != null)
{
FlipTileData TileData = new FlipTileData()
{
Title = "",
BackTitle = "WP Novosti",
BackContent = feed.Items.First().Title.Text,
WideBackContent = feed.Items.First().Title.Text,
Count = 0,
};
appTile.Update(TileData);
}
else
{
}
});
this i use if an item is selected on the listbox:
public void feedListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox listBox = sender as ListBox;
if (listBox != null && listBox.SelectedItem != null)
{
// Get the SyndicationItem that was tapped.
SyndicationItem sItem = (SyndicationItem)listBox.SelectedItem;
// Set up the page navigation only if a link actually exists in the feed item.
if (sItem.Links.Count > 0)
{
Uri uri = sItem.Links.FirstOrDefault().Uri;
NavigationService.Navigate(new Uri("/Pregled.xaml?url=" + uri, UriKind.Relative));
UpdateFeedList(State["feed"] as string);
}
}
}
the rest is just displaying this passed url on another page via webbrowser control. how can i make this really work now like i want described on top?!

Related

Create ListBox items from text file with 2 variables

So, I would like any help to populate a ListBox that is going to show a website name and if it's clicked go to a specific url.
This is what's inside of the text file:
#first website
http://firstwebsite.com
#second website
http://secondwebsite.com
#third website
http://thirdwebsite.com
I can read the file and populate the listbox with the name, but cannot put the url working.
FileOpenPicker picker = new FileOpenPicker();
picker.ViewMode = PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
picker.FileTypeFilter.Add(".txt");
StorageFile file = await picker.PickSingleFileAsync();
if (file != null) {
var stream = await file.OpenAsync(FileAccessMode.Read);
using (StreamReader reader = new StreamReader(stream.AsStream()))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if (line.StartsWith("#") {
listbox.items.Add(line);
}
Any help is great.
Thanks
If you want it in the "Click" event, I mean when you click on the Item, below code works.
private void listBox1_Click(object sender, EventArgs e)
{
string str = ((ListBox)(sender)).Text;
Process.Start(str);
}
Handle NULL conditions and exceptions.
Add System.Diagnostics namespace for "Process".
I've created a very simple console to test this.
Here is what I came up with
var _listBox1 = new ListBox();
//just adding a url for example
_listBox1.Items.Add("http://www.google.com");
//here I am just setting the selected value
_listBox1.SetSelected(0, true);
var selectedUrl = _listBox1.SelectedItem.ToString();
//this will start off the default web browser
Process.Start(selectedUrl);
So the Process.Start() can be put in any event handlers for that listbox. I.E SelectedIndexChanged event
private void _listBox1_SelectedIndexChanged(object pSender, EventArgs pArgs)
{
var selectedUrl = _listBox1.SelectedItem.ToString();
Process.Start(selectedUrl);
}

Set a name for each ImageSource in a list

I call an API and get from there an id, a name and an URL for images...
I display them in a flipview and I used to convert them, saved them in a folder, convert them back and show them...
So I thought it would be much easier if I show them directly from their URL.
Now, when the user clicks (taps) on the image, it has to go to another page to show the detail of that image (and it was working fine, when saving the pic, since I named them by the id (id.png))
is there anyway I can name the ImageSource to be this id, or give it like a property (Title or Name)?
var pics = from special in GlobalVariables.Special
where special.special == "1"
select new { id = special.id, name = special.name, image = special.banner_image, featured = special.special };
foreach (var item in pics)
{
await savePicToDisk(item.image, item.name, item.id, item.featured);
}
So, then, instead of save them:
List<ImageSource> listOfImages = new List<ImageSource>();
string url = "http://52.8.2.140" + picAddress;
ImageSource urlOfPic = new BitmapImage(new Uri(url, UriKind.Absolute));
listOfImages.Add(urlOfPic);
Then, where I have to show them, I just bind it to the flipview:
flipView1.DataContext = Classes.Special.listOfImages;
The Item_Click event's arguments have a property ClickedItem (in some cases you may also need OriginalSource), which you can use to point to your item in a collection. If item's class has a property responsible for Name/Id/other you can freely pass that when navigating, other solution may be passing an index of selected item in the collection. Sample code;
private void ItemClick(object sender, ItemClickEventArgs e)
{
var item = e.ClickedItem as ItemClass;
if (item != null)
Frame.Navigate(typeof(DetailPage), item.ID);
// if your item contains a Name/Id/other you can pass that when navigating
// you can also pass index of your item in collection if you need
}

Is there any way to place multiple links in the LinkLabel control in Windows Forms

Is there any way to place multiple links in the LinkLabel control in Windows Forms?
If I just set it like this
this.linkLabel.Text = "";
foreach (string url in urls)
{
this.linkLabel.Text += url + Environment.NewLine;
}
it merges it into one link.
Thanks in advance.
Yes, though there isn't a way that I can tell to do it directly from the designer, but it is easy to manage via code:
var linkLabel = new LinkLabel();
linkLabel.Text = "(Link 1) and (Link 2)";
linkLabel.Links.Add(1, 6, "Link data 1");
linkLabel.Links.Add(14, 6, "Link data 2");
linkLabel.LinkClicked += (s, e) => Console.WriteLine(e.Link.LinkData);
Basically, the Links collection on the label can host a bunch of links in the LinkLabel. The LinkClicked event contains a reference to the specific link that was clicked so you can access the link data you associated with the link, among other things.
The designer only exposes a LinkArea property which defaults to include all of the text of the LinkLabel. The first Link you add to the Links collection will automatically change the LinkArea property to reflect the first link in the collection.
Something a little closer to what you're asking would look like this:
var addresses = new List<string> {
"http://www.example.com/page1",
"http://www.example.com/page2",
"http://www.example.com/page3",
};
var stringBuilder = new StringBuilder();
var links = new List<LinkLabel.Link>();
foreach (var address in addresses)
{
if (stringBuilder.Length > 0) stringBuilder.AppendLine();
// We cannot add the new LinkLabel.Link to the LinkLabel yet because
// there is no text in the label yet, so the label will complain about
// the link location being out of range. So we'll temporarily store
// the links in a collection and add them later.
links.Add(new LinkLabel.Link(stringBuilder.Length, address.Length, address));
stringBuilder.Append(address);
}
var linkLabel = new LinkLabel();
// We must set the text before we add the links.
linkLabel.Text = stringBuilder.ToString();
foreach (var link in links)
{
linkLabel.Links.Add(link);
}
linkLabel.AutoSize = true;
linkLabel.LinkClicked += (s, e) => {
System.Diagnostics.Process.Start((string)e.Link.LinkData);
};
I'm attaching the URL itself as the LinkData to the link's I'm creating in the loop so I can extract it out as a string when the LinkClicked event is fired.

Text to Speech RSS Feed Help C# WP 8

I am making a simple application that gets RSS feed from a website then automatically reads the Headlines out loud (text to speech), so I followed this Tutorial to create the RSS reader https://msdn.microsoft.com/library/windows/apps/hh487167(v=vs.105).aspx
Now I have no idea how to automatically text to speech the news in the list box, any ideas?
You could simply go for the TTS api from msdn but make sure that you enable the ID_CAP_SPEECH_RECOGNITION in your AppManifest.
Have a look at the sample here.
For more : Speech recognition to text Windows Phone 8
So I figured it out,
here is the code:
private void UpdateFeedList(string feedXML)
{
// Load the feed into a SyndicationFeed instance.
StringReader stringReader = new StringReader(feedXML);
XmlReader xmlReader = XmlReader.Create(stringReader);
SyndicationFeed feed = SyndicationFeed.Load(xmlReader);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// Bind the list of SyndicationItems to our ListBox.
feedListBox.ItemsSource = feed.Items;
loadFeedButton.Content = "Refresh Feed";
feedListBox.SelectionMode = SelectionMode.Multiple;
feedListBox.SelectAll();
});
}
// The SelectionChanged handler for the feed items
private void feedListBox_SelectionChanged(object sender, RoutedEventArgs e)
{
ListBox listBox = sender as ListBox;
if (listBox != null && listBox.SelectedItem != null)
{
// Get the SyndicationItem that was tapped.
SyndicationItem sItem = (SyndicationItem)listBox.SelectedItem;
synth.SpeakTextAsync(sItem.Title.Text);
if (feedListBox.SelectedIndex < feedListBox.Items.Count - 1)
{
feedListBox.SelectedIndex = feedListBox.SelectedIndex + 1;
}
I am pretty sure that there is a better solution, but this worked !

Windows Phone Emulator Functions Properly, Debugging or Deploying to Device Does Not

I am developing a very simple application that parses an XML feed, does some formatting and then displays it in a TextBlock. I've added a hyperLink (called "More..) to the bottom of the page (ideally this would be added to the end of the TextBlock after the XML has been parsed) to add more content by changing the URL of the XML feed to the next page.
The issue I'm experiencing is an odd one as the program works perfectly when in the Windows Phone 7 Emulator, but when I deploy it to the device or debug on the device, it works for the first click of the "More..." button, but the ones after the first click just seem to add empty space into the application when deployed or debugged from the device.
I'm using a Samsung Focus (NoDo) and originally thought this may have had to do with the fact that I may not have had the latest developer tools. I've made sure that I am running the latest version of Visual Studio and am still running into the issue.
Here are some snippets of my code to help out.
I've declared the clickCount variable here:
public partial class MainPage : PhoneApplicationPage
//set clickCount to 2 for second page
int clickCount = 2;
Here is the snippet of code I use to parse the XML file:
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
ListBoxItem areaItem = null;
StringReader stream = new StringReader(e.Result);
XmlReader reader = XmlReader.Create(stream);
string areaName = String.Empty;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "example")
{
areaName = reader.ReadElementContentAsString();
areaItem = new ListBoxItem();
areaItem.Content = areaName;
textBlock1.Inlines.Add(areaName);
textBlock1.Inlines.Add(new LineBreak());
}
}
}
}
}
and the code for when the hyperLink button is clicked:
private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
{
int stringNum = clickCount;
//URL is being incremented each time hyperlink is clicked
string baseURL = "http://startofURL" + stringNum + ".xml";
Uri url = new Uri(baseURL, UriKind.Absolute);
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(url);
//increment page number
clickCount = clickCount + 1;
}
It feels like there's a little more debugging to do here.
Can you test where exactly this is going wrong?
is it the click that is not working on subsequent attempts?
is it the HTTP load which is failing?
is it the adding of inline text which is failing?
Looking at it, I suspect it's the last thing. Can you check that your TextBlock is expecting Multiline text? Also, given what you've written (where you don't really seem to be making use of the Inline's from the code snippet I've seen), it might be easier to append add the new content to a ListBox or a StackPanel rather than to the inside of the TextBlock - ListBox's especially have some benefit in terms of Virtualizing the display of their content.

Categories

Resources