I am building an Rss reader app and having problems with gifs.
Does anyone know how to build the ImageConverter class so it would convert gif to png?
Converting gifs in code will also work for me.
My app works in a way that it takes everything from the feed, puts it in a list() first and then it populates the listbox, (in a way the user chooses to). gifs leave blank images :(
so basically converter would have to work with a link, not a direct data stream.
I will add some code on how my data is updated:
ObservableCollection<FeedItem> slika0; //where the feeditems from the selected category go
public ObservableCollection<FeedItem> Slika0
{
get { return slika0; }
set
{
slika0 = value;
OnPropertyChanged("Slika0");
}
}
int broj_elemenata = 0;
bool nema_elemenata = false;
private void UpdateFeedList(string feedXML)
{
StringReader stringReader = new StringReader(feedXML);
XmlReader xmlReader = XmlReader.Create(stringReader);
SyndicationFeed feed = SyndicationFeed.Load(xmlReader);
List<string> kategorije = new List<string>();
foreach (SyndicationItem sitem in feed.Items)
{
foreach (SyndicationCategory sc in feed.Categories)
{
if (!kategorije.Contains(sc.Name))
{
kategorije.Add(sc.Name);
}
}
}
FeedItem FeedItem = new FeedItem();
Slika0 = new ObservableCollection<FeedItem>();
//SyndicationCategory tražena_kat = new SyndicationCategory("Trendy");
foreach (SyndicationItem item in feed.Items)
{
foreach (SyndicationCategory sc in item.Categories)
{
FeedItem.Content_List.Add(sc.Name);
foreach (SyndicationElementExtension ext in item.ElementExtensions)
{
XElement ele = ext.GetObject<XElement>();
if (ele.Name.LocalName == "encoded" && ele.Name.Namespace.ToString().Contains("content"))
{
FeedItem.Content_List.Add(item.Title.Text);
FeedItem.Content_List.Add(ele.Value);//takes the content of the feed
}
}
foreach (SyndicationLink link in item.Links)
{
FeedItem.Content_List.Add(link.Uri.AbsoluteUri); //takes the links for browsing and the image
}
}
}
IsolatedStorageSettings.ApplicationSettings["ContentList"] = FeedItem.Content_List;
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
int i;
int x = 0;
Slika0.Clear();
foreach (var item in FeedItem.Content_List)
{
i = FeedItem.Content_List.IndexOf(item, x); //x = index of category in the list
if (item == "Trendy")
{
FeedItem FF = new FeedItem();
FF.Post_text = FeedItem.Content_List[i + 1];//title of article
FF.Content_link = FeedItem.Content_List[i + 2];//content
FF.Post_link = FeedItem.Content_List[i + 3];//the location of link for browsing
FF.Post_slika = FeedItem.Content_List[i + 4]; //location of image link
if (FF.Post_slika == "") //if there is no link for picture code is executed
{
FF.Post_slika = "Slike/zimo_veliki_tile.png";
}
Slika0.Add(FF);
this.lsSlika_P.ItemsSource = Slika0; //take
x = i + 5;
broj_elemenata++;
}
}
if (lsSlika_P.Items.Count <= 3)
{
scroll_panorama.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
}
if (lsSlika_P.Items.Count == 0)
{
nema_elemenata = true;
}
else nema_elemenata = false;
});
pan_progres.Visibility = Visibility.Collapsed;//progress bar
}
Related
I am working on a WPF XAML application that scrapes certain websites for products. I have the search part working and it finds what I'm looking for. But as soon as there is more then 1 result I get a System.InvalidoperationException. I use a ObservableCollection to put the results into a <ListBox>.
Here is the search method:
private static ObservableCollection<EntryModel> _entries = new ObservableCollection<EntryModel>();
public static ObservableCollection<EntryModel> LoadCollectionData
{
get { return _entries; }
set { _entries = value; }
}
public static void PrehmSearchResults(string SearchQuery)
{
HtmlWeb web = new HtmlWeb();
try
{
string ZoekOpdracht = SearchQuery.Replace(" ", "+");
HtmlDocument doc = web.Load("https://www.prehmshop.de/advanced_search_result.php?keywords=" + ZoekOpdracht);
var title = doc.DocumentNode.CssSelect("div.header_cell > a").Single().InnerText;
var links = doc.DocumentNode.CssSelect("a.product_link");
var productLink = new List<string>();
var productTitle = new List<string>();
foreach (var item in links)
{
if (item.Attributes["href"].Value.Contains(".html"))
{
productLink.Add(item.Attributes["href"].Value);
productTitle.Add(title);
}
}
var TitleAndLink = productLink.Zip(productTitle, (l, t) => new { productLink = l, productTitle = t });
foreach (var nw in TitleAndLink)
{
var product = new List<EntryModel>();
var adDetails = new EntryModel
{
Title = nw.productTitle,
Link = nw.productLink
};
Debug.Print(adDetails.ToString());
var ZoekOpdrachtInTitle = adDetails.Title.ToLower().Contains(ZoekOpdracht.ToLower());
if (ZoekOpdrachtInTitle)
{
_entries.Add(adDetails);
}
}
}
So I found the solution without changing too much code. thanks for the help from #PaulSinnema.
The link is part of the title so I only had to change
var title = doc.DocumentNode.CssSelect("div.header_cell > a").ToList();
And I had to change the foreach loop:
foreach (var item in title)
{
if (item.Attributes["href"].Value.Contains(".html"))
{
productLink.Add(item.Attributes["href"].Value);
productTitle.Add(item.InnerText);
}
}
Am trying to fetch Hyperlinks address from word document from every paragraph using OpenXML.
public static string GetAddressFromPara(Paragraph Paras)
{
IEnumerable<Hyperlink> hplk = Paras.Descendants<Hyperlink>();
if (hplk != null)
{
foreach (Hyperlink hp in hplk)
{
//string address = ???????;
}
}
}
I believe it should be
foreach (Hyperlink hp in hplk)
{
hyperlinkText = new StringBuilder();
foreach (Text text in hp.Descendants<Text>())
hyperlinkText.Append(text.InnerText);
hyperlinkRelationshipId = hp.Id.Value;
ExternalRelationship hyperlinkRelationship = doc
.MainDocumentPart
.ExternalRelationships
.Single(c => c.Id == hyperlinkRelationshipId);
hyperlinkUri = new StringBuilder(hyperlinkRelationship.Uri.AbsoluteUri);
}
I've a requirement in which I've to display the saved users to the SharePoint people editor control. For this, I am saving the user names to People/Group column. And I am using the following code for taking these users to people editor control:
SetPeopleEditor(item, Constants.FieldNames.IT_DIRECTOR, pe_ITDirector, oWeb);
the definition of the above method is shown below:
private PickerEntity SetPeopleEditor(SPListItem item, string columnName, PeopleEditor peopleEditor, SPWeb web)
{
ArrayList entityArrayList = new ArrayList();
PickerEntity entity = null;
if (item[columnName] != null)
{
char[] to_splitter = { ';' };
string to_list = item[columnName].ToString(); // Reads value stored in SPList. (i.e., "Domain\User1; Domain\User2")
string[] arr = to_list.Split(to_splitter);
string user = string.Empty;
for (int i = 1; i < arr.Length; i++)
{
if ((i % 2) != 0)
{
user = arr[i].Substring(arr[i].IndexOf("#") + 1);
entity = new PickerEntity();
entity.Key = user;
entity.IsResolved = true;
entity = peopleEditor.ValidateEntity(entity);
entityArrayList.Add(entity);
}
}
}
return entity;
}
But unfortunately, the control always showing empty value. How can I achieve this by populating data to the people editor control?
You can do as follows,
SPFieldUserValueCollection userValueCollection =
new SPFieldUserValueCollection(SPContext.Current.Web, SPContext.Current.Item["ColumnName"] as string);
if (userValueCollection .Count > 0)
{
spPeoplePickerContol.CommaSeparatedAccounts = userValueCollection[0].User.LoginName;
}
You have to include peopleEditor.Entities.Add(entity) after validating entity to add the entity to peopleEditor control.
private void SetPeopleEditor(SPListItem item, string columnName, PeopleEditor peopleEditor, SPWeb web)
{
if (item[columnName] != null)
{
char[] to_splitter = { ';' };
string to_list = item[columnName].ToString(); // Reads value stored in SPList. (i.e., "Domain\User1; Domain\User2")
string[] arr = to_list.Split(to_splitter);
string user = string.Empty;
for (int i = 1; i < arr.Length; i++)
{
if ((i % 2) != 0)
{
user = arr[i].Substring(arr[i].IndexOf("#") + 1);
PickerEntity entity = new PickerEntity();
entity.Key = user;
entity.IsResolved = true;
entity = peopleEditor.ValidateEntity(entity);
peopleEditor.Entities.Add(entity);
}
}
}
}
Try this:
string x = item["SP_Group"] == null ? "" : item["SP_Group"].ToString();
if (x != "")
{
SPFieldUserValue uv = new SPFieldUserValue(web, x);
SPGroup group = mySite.Groups.GetByID(uv.LookupId);
if (group.Users.Count > 0)
{
System.Collections.ArrayList entityArrayList = new System.Collections.ArrayList();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
foreach (SPUser sUser in group.Users)
{
PickerEntity entity = new PickerEntity();
entity.Key = sUser.LoginName;
entity = peopleEditor.ValidateEntity(entity);
entityArrayList.Add(entity);
}
});
peopleEditor.UpdateEntities(entityArrayList);
peopleEditor.Validate();
}
else
{
peopleEditor.Entities.Clear();
}
}
string x = item["SP_Users"] == null ? "" : item["SP_Users"].ToString();
if (x != "")
{
SPFieldUserValueCollection uvcoll = new SPFieldUserValueCollection(mySite, x);
if (uvcoll.Count > 0)
{
System.Collections.ArrayList entityArrayList = new System.Collections.ArrayList();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
foreach (SPFieldUserValue uv in uvcoll)
{
SPUser sUser = uv.User;
PickerEntity entity = new PickerEntity();
entity.Key = sUser.LoginName;
entity = peopleEditor.ValidateEntity(entity);
entityArrayList.Add(entity);
}
});
peopleEditor.UpdateEntities(entityArrayList);
peopleEditor.Validate();
}
else
{
peopleEditor.Entities.Clear();
}
}
I'm trying to read a file line by line, which works perfectly but I want to seperate the results I get into subitems in the listview.
I am also searching for all .jar files in the folder so I can use those as the name (first column). The second column needs to have the "version", the third column the "author" and the fourth column the "description".
Here's one of the text files I receive from within the jar files:
name: AFK
main: com.github.alesvojta.AFK.AFK
version: 2.0.5
author: Ales Vojta / schneckk
description: Provides AFK messages
website: http://dev.bukkit.org/server-mods/afk/
commands:
afk:
description: Provides AFK message when player types /afk.
usage: /<command>
this is the code I have right now:
private List<string> GetInstalledPlugins()
{
List<string> list = new List<string>();
lvInstalledPlugins.Items.Clear();
if (!Directory.Exists(Environment.CurrentDirectory + "\\plugins"))
{
Directory.CreateDirectory(Environment.CurrentDirectory + "\\plugins");
DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory + "\\plugins");
FileInfo[] fileInfo = di.GetFiles("*.jar");
foreach (var info in fileInfo)
{
//lvInstalledPlugins.Items.Add(info.Name);
list.Add(info.Name);
}
}
else
{
DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory + "\\plugins");
FileInfo[] fileInfo = di.GetFiles("*.jar");
foreach (var info in fileInfo)
{
//lvInstalledPlugins.Items.Add(info.Name);
list.Add(info.Name);
}
}
return list;
}
private void test(IEnumerable<string> list)
{
List<ListViewItem> PluginList = new List<ListViewItem>();
var items = new string[4];
try
{
foreach (var ListItem in list)
{
Console.WriteLine(ListItem);
var name = Environment.CurrentDirectory + "\\plugins\\" + ListItem;
var zip = new ZipInputStream(File.OpenRead(name));
var filestream = new FileStream(name, FileMode.Open, FileAccess.Read);
var zipfile = new ZipFile(filestream);
ZipEntry item;
while ((item = zip.GetNextEntry()) != null)
{
if (item.Name == "plugin.yml")
{
using (var s = new StreamReader(zipfile.GetInputStream(item)))
{
string line;
while ((line = s.ReadLine()) != null)
{
if (line.Contains("name"))
{
items[0] = line;
}
if (line.Contains("version"))
{
items[1] = line;
}
if (line.Contains("author"))
{
items[2] = line;
}
if (line.Contains("description"))
{
items[3] = line;
}
try
{
var lvitem = new ListViewItem(items);
lvitem.Name = items[0];
lvitem.Text = items[0];
lvitem.SubItems.Add(items[1]);
lvitem.SubItems.Add(items[2]);
lvitem.SubItems.Add(items[3]);
PluginList.Add(lvitem);
}
catch (Exception)
{
}
}
lvInstalledPlugins.Items.AddRange(PluginList.ToArray());
}
}
}
}
This doesn't seem to work :/, any ideas? I've been working on this for the whole day and can't seem to get it to work :(.
Not exactly sure of your question, but going by the title, the answer to the question below may provide some assistance.
C# listView, how do I add items to columns 2, 3 and 4 etc?
I've been struggling for the past 3 days with removing or overwriting ListPicker values. I want the click event of a button to remove the old list items and populate it with the new ones. I use LINQ to parse values from an XML file. The problem is that no matter what I try I always get an exception such as "Operation not supported on read-only collection." etc. Is there a way to remove all the values from the ListPicker?
Here's the code:
public partial class Visa : PhoneApplicationPage
{
List<Tiedot> vastausLista = new List<Tiedot>();
XDocument lista = XDocument.Load("Faktat.xml");
Random rand = new Random();
int piste = 0;
int levelStart = 1;
string level = string.Empty;
// Constructor
public Visa()
{
InitializeComponent();
tbPisteet.Text = string.Format("{0}/25", piste.ToString());
level = string.Format("level{0}", levelStart.ToString());
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (levelStart <= 1)
{
var documents =
(from docs in lista.Descendants("Taso")
where docs.Attribute("id").Value == level
select new
{
Elain = docs.Elements("vaihtoehto")
}).ToList();
foreach (var doc in documents)
{
foreach (var section in doc.Elain)
{
foreach (var item in section.Elements("vastaus"))
{
vastausLista.Add(new Tiedot { Elain = item.Value });
}
}
}
vaihtoehtoLista.ItemsSource = vastausLista;
var kuvaKysymys = (from tiedot in lista.Descendants("Taso")
where tiedot.Attribute("id").Value == level
select new Tiedot
{
Kuva = (string)tiedot.Element("kuva").Value,
Question = (string)tiedot.Element("kysymys").Value
}).FirstOrDefault();
BitmapImage kuvaKuva = new BitmapImage();
kuvaKuva.UriSource = new Uri(kuvaKysymys.Kuva, UriKind.Relative);
image.Source = kuvaKuva;
tbQuestion.Text = kuvaKysymys.Question;
}
base.OnNavigatedTo(e);
}
private void vaihtoehtoLista_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (vaihtoehtoLista.SelectedIndex == 1)
{
Update();
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
UpdateLevel();
}
public void Update()
{
piste++;
tbPisteet.Text = string.Format("{0}/25", piste.ToString());
MessageBox.Show("You're correct!!");
}
public void RemoveOldLevel()
{
while (vastausLista.Count > 0)
vaihtoehtoLista.Items.Remove(vastausLista[0]);
}
public void UpdateLevel()
{
levelStart++;
level = string.Format("level{0}", levelStart.ToString());
var documents =
(from docs in lista.Descendants("Taso")
where docs.Attribute("id").Value == level
select new
{
Elain = docs.Elements("vaihtoehto")
}).ToList();
foreach (var doc in documents)
{
foreach (var section in doc.Elain)
{
foreach (var item in section.Elements("vastaus"))
{
vastausLista.Add(new Tiedot { Elain = item.Value });
}
}
}
RemoveOldLevel();
vaihtoehtoLista.ItemsSource = vastausLista;
var kuvaKysymys = (from tiedot in lista.Descendants("Taso")
where tiedot.Attribute("id").Value == level
select new Tiedot
{
Kuva = (string)tiedot.Element("kuva").Value,
Question = (string)tiedot.Element("kysymys").Value
}).FirstOrDefault();
BitmapImage kuvaKuva = new BitmapImage();
kuvaKuva.UriSource = new Uri(kuvaKysymys.Kuva, UriKind.Relative);
image.Source = kuvaKuva;
tbQuestion.Text = kuvaKysymys.Question;
}
}
You have to use an ObservableCollection with your elements inside, as the DataContext of your ListPicker. Something like this:
ListPicker picker = new ListPicker();
ObservableCollection<Object> coll = your items inside;
picker.DataContext = coll;
And after you can modify directly the ObservableCollection. Or you can use:
ListPicker picker = new ListPicker();
picker.ItemsSource = List<> with your items;
But you have to reset the ItemSource each time you change the content of your List<>.