WP7 passing multiple pieces of data between pages - c#

In my app I am building my own media player. When a user selects a song to play want to be able to pass the link to the sample media and the metadata associated with it (Artist, Track, Album Art, etc.) The part I'm stuck on is how to group all the data and pass it to the media player page. Here's what i have so far.
Determine what item was selected and add data to query string.
UPDATED
public void musicSampleSelectedHandler(object sender, RoutedEventArgs e)
{
Track selected = (sender as Image).DataContext as Track;
ListBoxItem pressedItem = this.listReleaseMain.ItemContainerGenerator.ContainerFromItem(selected) as ListBoxItem;
if (pressedItem != null)
{
string _rT = selected.title;
string _rN = selected.release.name;
//string _rA = selected.artists; ????
string _rI = selected.images.large.url;
string _rS = selected.sampleUrl;
this.NavigationService.Navigate(new Uri("/Pages/MediaPage.xaml?releaseName=" + _rN + "&releaseTrack=" + _rT + "&releaseImage=" + _rI
+ "&releaseSample=" + _rS, UriKind.Relative));
}
}
OnNavigatedTo method to pull data out of the query string
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string releaseName = String.Empty;
string releaseImg = String.Empty;
string releaseUrl = String.Empty;
if (NavigationContext.QueryString.TryGetValue("releaseUrl", out releaseUrl))
{
sampleMedia.Source = new Uri(releaseUrl, UriKind.Absolute);
}
}
I'm not sure if I can use the query sting to pass multiple pieces to the media player, or if I to do something different to pass the data to the other page. All of my data comes from the web using a webclient. Thanks for the help.

QueryString is just a Dictionary of the parameters passed in via the Uri. The Uri uses the standard syntax of passing in parameters delimited by &. So in your example, if you had something like:
this.NavigationService.Navigate(new Uri("/Pages/MediaPage.xaml?releaseUrl=" + releaseUrl + "&releaseImg=" + releaseImg , UriKind.Relative));
then you can then parse this out using something like:
if (NavigationContext.QueryString.TryGetValue("releaseUrl", out releaseUrl))
{
sampleMedia.Source = new Uri(releaseUrl, UriKind.Absolute);
}
if (NavigationContext.QueryString.TryGetValue("releaseImg", out releaseImg))
{
// do something with releaseImg
}

Related

Create all the json files at a time by getting all the values from a text file using C#

I create a long text file name POI.txt which contain City name at first and then places name of that city. My Text File look like this-
Flensburg;Flensburger Brauerei;Flensburg station;Fachhochschule Flensburg;Flens-Arena;Duborg-Skolen;Nordertor
Kiel;Walther Schücking Institute of International Law;Ferdinand Tönnies Society;Zoological Museum of Kiel University
Berlin;Battle of Berlin;Timeline of Berlin;Warschauer Straße;Berlin Brandenburger Tor station;Embassy of the United Kingdom, Berlin
and many many city.........
After that I keep the city name in Combobox1 and the places name in Combobox2. So When One city comes in combobox1 it shows all the Places of that city in the 2nd combobox. I create a button after the 2nd Combobox. I use this button to create a json file of a places.
This is my json create method for each place.
public void ToJsonForLocation(string CityName,string PoiName)
{
var startPath = Application.StartupPath;
string folderName = Path.Combine(startPath, "OnePoiJson");
string SubfolderName = Path.Combine(folderName, CityName);
System.IO.Directory.CreateDirectory(SubfolderName);
string fileName = PoiName + ".json";
var path = Path.Combine(SubfolderName, fileName);
var Jpeg_File = new DirectoryInfo(startPath + #"\Image\" + CityName).GetFiles("*.jpg");
POIData Poi=new POIData();
Poi.Name = PoiName;
Poi.Shorttext = new WikiShortText().GetShortText(PoiName);
Poi.GeoCoordinates = GeosFromString(startPath + #"\Latitude Longitude\" + PoiName + ".txt");
Poi.Images = new List<string> { Jpeg_File[0].Name };
//Poi.Images =new List<string> { string.Format("{0:X}.jpg", new WikiImage().GetImage(PoiName).Image.GetHashCode())};
string json = JsonConvert.SerializeObject(Poi,Formatting.Indented);
File.WriteAllText(path,json);
}
I am calling this method in the main form in this way-
private void JSON_Click(object sender, EventArgs e)
{
JSON_Output poi_Json = new JSON_Output();
poi_Json.ToJsonForLocation(comboBox1.Text, comboBox2.Text);
}
Now my problem is that with my code, I need to click every places on the 2nd combobox to create a json file for that city.It takes too much time. I want to create all the file at a time. I know I need to create a dictionary to do this. But As I am very new to handle this situation I want to know how to proceed with it.

EO (Essentialobjects) new windows handler error

I am using EO (Essentialobjects) WebBrowser elmulator,i makin login ,and then i have new windows popup,and i can't handle it.I am new with this EO dll ,and can't find any example of this.
Do any one know how to handle the popUp/Newwindow after i make event of click.
Here my code:
string loginPostUrl = "https://www.test.com";
EO.WebBrowser.BrowserOptions op = new EO.WebBrowser.BrowserOptions();
EO.WebBrowser.Request req = new EO.WebBrowser.Request(loginPostUrl);
webView1.LoadRequestAndWait(req);
webView1.EvalScript("document.getElementById('User').value='test'");
webView1.EvalScript("document.getElementById('Pwd1').value='xxx'");
webView1.EvalScript("document.getElementById('User').onclick();");
//after click i get error That says i need to handle NewWindows event
//Itried this but not working
EO.WebBrowser.DOM.Document doc = webView1.GetDOMWindow().document;
EO.WebBrowser.NewWindowHandler handler = new
NewWindowHandler(WebView_NewWindow);
public void WebView_NewWindow(object sender, NewWindowEventArgs e)
{
}
Not sure about this but here's guess....
string info = webView1.EvalScript(#"document.getElementById('User').onclick = (function(event) {return document.getElementById('User').value + '&' + document.getElementById('Pwd1').value});");
might get you a string like > "user&Pwd1" wich you can split.
string[] array = info.split('&');
string user = array[0];
string pasw = array[1];

Winnovative With Parameters in URL

i have a big problem and i need your help. i'm trying send to url parameters to generate the pdf file with the library winnovative. when trying the first time I have no problems and generates pdf but if I want to get the pdf again this gives me error because the parameters in url they are sent and fail to request and falls when so finally assign to generate the pdf file.
I have attached the code for review:
public override void Pagina_PrimeraCarga(object sender, EventArgs e)
{
string datosRequest = Request.QueryString["DATOS"];
char delimitadores = ';';
string[] datos = datosRequest.Split(delimitadores);
imgBanco.Attributes.Add("ImageUrl", "~/App_Themes/Imagenes/Logo.gif");
System.DateTime fecha = new System.DateTime(2014, 12, 17);
lblDia.Text = Convert.ToString(fecha.Day);
lblMes.Text = Convert.ToString(fecha.Month);
lblAno.Text = Convert.ToString(fecha.Year);
string rutEmpresa = datos[3];
int rut = Convert.ToInt32(rutEmpresa);
string rutRes = rut.ToString("N0", CultureInfo.InvariantCulture).Replace(",", ".");
rutRes = rutRes + "-" + datos[4];
lblOficina.Text = "OFICINA: " + datos[0];
lblNombreTitular.Text = "NOMBRE TITULAR: " + datos[1];
lblRut.Text = "R.U.T.: " + rutRes;
lblDireccion.Text = "DIRECCION: " + datos[2];
lblFono.Text = "FONO: " + datos[5];
}
P.D: my apologies for my bad English but my native language is Spanish
P.D.2: Thanks to everyone who could help me in this case
I think that your problem is that after postBack your query string will be empty. Try this
add hiddenfield
<asp:HiddenField runat="server" ID="hidden1" />
then in your pageLoad
if (!IsPostBack)
{
string datosRequest = Request.QueryString["DATOS"];
if(datosRequest != null)
{
//do something
hidden1.Value = datosRequest ;
}
}
else
{
datosRequest = hidden1.Value;
}
I have solved the problem. I was thinking a bit and detects when passed a second time to obtain the pdf that was creating the cookie to be passed to the other form was created and therefore did not pass the data. for this reason I had to add 2 lines but my code for closing the pdf this server delete the cookie and when consulted again remove the client:
Response.Cookies.Clear ();
myCookie.Expires = DateTime.Now.AddDays (1D);

Resolving Windows phone 8.1 Compilation Error: GetENumerator

I am using the onNavigate method in windows phone 8.1 and trying to pass a list of navigation objects I have created. When I try and retrieve the list from e.parameters (which seems to work) I cannot iterate through the list and cast them back to the original object types. It works if I send a single object but not a list.
For Example.
From a button_click event on the mainpage I am calling the code below to navigate to page2 and pass a list of objects.
I have a class defined as below.
class NavigationContext
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
For testing I create 3 objects and add it to a list as follows
NavigationContext navobj = new NavigationContext(){ID = 77,Name = "DN", Description = "TD1"};
NavigationContext navobj2 = new NavigationContext(){ID = 22,Name = "JN",Description = "JD1"};
NavigationContext navobj3 = new NavigationContext(){ID = 22,Name = "CN",Description = "CN1"};
I then create the list as below
List<NavigationContext> navlist = new List<NavigationContext>(){navobj,navobj2,navobj3};
I then Make the call below
Frame.Navigate(typeof(Page2), navlist);
If I pass one object the code works and compiles and the new page is navigated too sucessfully*
**If I pass a list as above I get the following error on the foreach iterator in the OnNavigatedTo method
**Example code below*
string myfiller = "";
foreach (NavigationContext navobj in e.Parameter)
{
myfiller += navobj.ID.ToString() + " " + navobj.Name + " " + navobj.Description+ "\n";
}
I get the following error:
foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for GetENumerator.
I have tried to find ways to cast the list back to NavigationContext objects but I think I am doing something stupid because I cann0t get it to work.
Any suggestions?
Try this:
List<NavigationContext> navigationObjects = e.Parameter as List<NavigationContext>;
if(null != navigationObjects)
{
foreach (NavigationContext navobj in navigationObjects)
{
myfiller += navobj.ID.ToString() + " " + navobj.Name + " " + navobj.Description+ "\n";
}
}
Note that if the e.Parameter object cannot be interpreted as the correct type (a List<NavigationContext>) then using the as operator will result in a null value that can be tested to prevent further problems when you try to use the elements of the list as NavigationContext objects.

Windows Phone 7 passing data from listbox to another page via context query string

Hi there I am a beginner with developing on C#. I am having some problems passing data from one page to another. Within a listbox I have data which i have obtained from a database via web service.
I have created some coding to move sets of selected data to the next page and input it into the assigned text blocks. Currently this coding only works for one data field "eventId."
Could you please have a look at my code and tell me what I have done wrong and how i can fix this.
Here is my coding from the page which holds the listbox with the sets of data:
private void FirstListBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
EventServiceReference1.Event myEvent = (EventServiceReference1.Event)FirstListBox2.SelectedItem;
int eventId = myEvent.EventID;
string eventList = myEvent.EventList;
string eventDescription = myEvent.Description;
string eventDate = myEvent.Date;
string eventTime = myEvent.Time;
string eventLocation = myEvent.Location;
var url = string.Format("/EventPageTemp.xaml?eventId={0}", eventId + "&eventList={0}", eventList);
NavigationService.Navigate(new Uri(url, UriKind.Relative));
}
Here is my coding from the "EventPageTemp" page which i am passing the data to:
int eventId;
string eventIdStr;
string eventList;
if (NavigationContext.QueryString.TryGetValue
("eventId", out eventIdStr) && int.TryParse(eventIdStr, out eventId))
{// load event data, and set data context
txtEID.Text = eventIdStr;}
if (NavigationContext.QueryString.ContainsKey("eventList"))
{
string val = NavigationContext.QueryString["eventList"];
txtEList.Text = eventList;
}
At the moment it is coming up with the errors:
- the name 'eventList' does not exist in current context
-use of unassigned local variable 'eventList'
Please can you help me figure this out. Thank you.
the issue is your url, eventId + "&eventList={0}", eventList will be pass as eventId:
var url = string.Format("/EventPageTemp.xaml?eventId={0}", eventId + "&eventList={0}", eventList);
it should be:
var url = string.Format("/EventPageTemp.xaml?eventId={0}&eventList={1}", eventId, eventList);

Categories