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 !
Related
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);
}
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?!
ive got a problem with getting my data from a xml file to an listbox.
this is the data i want to get in my listbox:
<gjester>
<gjest>
<id>test</id>
<fornanv>test</fornanv>
<etternavn>test</etternavn>
<adresse>test</adresse>
<telefonnr>test</telefonnr>
</gjest>
</gjester>
and i created a listbox in my gui. But i don't know what to write in my code.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
I don't know what to write here
}
There are approximately a bazillion ways to add items from an XML file to your listbox but a good place to start would be the MSDN documentation for the XMLTextReader class and the ListBox.Items.Add() method.
Also - you will probably want to do this somewhere other than the SelectedIndexChanged event on the listbox. For learning purposes, try it on a button click.
Good luck - and after looking into the above I'm sure someone will help you if you haven't figured it out.
This is .NET 4.0 (VS2010 C#) and is completely untested but may give you a start....
private void FillListBoxWithThingsIWantToSelect()
{
XDocument ListBoxOptions = XDocument.Load(Filename);
foreach (XElement element in ListBoxOptions.Root.Elements())
{
if (element.Name.LocalName.Contains("gjester"))
{
foreach (XElement subelement in element.Elements())
{
if (subelement.Name.LocalName.Contains("gjest"))
{
// What do you want to add? The Attribute? Element value
listbox1.Items.Add(element.Value.ToString());
}
}
}
}
}
Would help if you listed your platform and what you want in the listbox.
You want to call this from your constructor.
Can use dictionary object to bind the data from XML to Listbox.
var dic = (from order in ds.Tables[0].AsEnumerable()
select new
{
UserView = order.Field<String>("Value"),
DevView = order.Field<String>("id")
}).AsEnumerable().ToDictionary(k => k.DevView, v => v.UserView);
Click here for reference
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.
Right now I've got a list box that shows RSS article titles/urls of an RSS feed. The title and URL extraction were no problem, but now I'm trying to have the description appear in a rich text box whenever the article title is selected in the list box. I can successfully get the description to show up in the text box, but it's always followed by a bunch of extra html. Example:
There's a silly rumor exploding on the Internet this weekend, alleging that Facebook is shutting down on March 15 because CEO Mark Zuckerberg "wants his old life back," and desires to "put an end to all the madness."<div class="feedflare">
<img src="http://feeds.feedburner.com/~ff/rss/cnn_topstories?d=yIl2AUoC8zA" border="0"></img> <img src="http://feeds.feedburner.com/~ff/rss/cnn_topstories?d=7Q72WNTAKBA" border="0"></img> <img src="http://feeds.feedburner.com/~ff/rss/cnn_topstories?i=at7OdUE16Y0:jsXll_RkIzI:V_sGLiPBpWU" border="0"></img> <img src="http://feeds.feedburner.com/~ff/rss/cnn_topstories?d=qj6IDK7rITs" border="0"></img> <img src="http://feeds.feedburner.com/~ff/rss/cnn_topstories?i=at7OdUE16Y0:jsXll_RkIzI:gIN9vFwOqvQ" border="0"></img>
Code:
private void button1_Click(object sender, EventArgs e)
{
{
XmlTextReader rssReader = new XmlTextReader(txtUrl.Text);
XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssReader);
XmlNodeList titleList = rssDoc.GetElementsByTagName("title");
XmlNodeList urlList = rssDoc.GetElementsByTagName("link");
descList = rssDoc.GetElementsByTagName("description");
for (int i = 0; i < titleList.Count; i++)
{
lvi = rowNews.Items.Add(titleList[i].InnerXml);
lvi.SubItems.Add(urlList[i].InnerXml);
}
}
}
private void rowNews_SelectedIndexChanged(object sender, EventArgs e)
{
if (rowNews.SelectedIndices.Count <= 0)
{
return;
}
int intselectedindex = rowNews.SelectedIndices[0]; // Get index of article title
txtDesc.Text=(descList[intselectedindex].InnerText);
// Get description array index that matched list index
}
You can strip html using approach from Using C# regular expressions to remove HTML tags
You can use InnerText instead of InnerHtml. This will only get the content of your child nodes without any markup.