how to do task after xaml page is loaded in windows phone - c#

So I'm working on Windows Phone Project based on music shuffling. I have one xaml page that display Songs in Queue, code file contain List<> that add all song in list but this task take more time.
When i Click a Button to navigate(or show queue)to page my app still remain same page for 4-5 second.
I want that how can I make some code in xaml.cs file that run after page is loaded.
After page loaded I show Progress Indicator and when all data completely in List<> elements, I show a Songs.
My code:
private void Event()
{
currentQueueData = MediaPlayer.Queue;
List<QueueData> boundedQueueData = new List<QueueData>();
SetProIndicator(true);
SystemTray.ProgressIndicator.Text = "Loading...";
if (currentQueueData.Count != 0)
{
for (int i = currentQueueData.ActiveSongIndex, k = 0; i < totalqueueCount; i++)
{
loadedqueueSongs[k] = currentQueueData[i];
boundedQueueData.Add(new QueueData()
{
queueSongIndex = k++,
queueSongName = currentQueueData[i].Name,
queueSongAlbum = currentQueueData[i].Album.Name + ",",
queueSongArtist = " " + currentQueueData[i].Artist.Name,
});
}
queueList.ItemsSource = boundedQueueData;
SetProIndicator(false);
//queueList.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
}
else
{
boundedQueueData.Add(new QueueData()
{
queueSongIndex = 0,
queueSongName = "Currently Queue Is Empty",
queueSongAlbum = "",
queueSongArtist = "",
});
queueList.ItemsSource = boundedQueueData;
}
}
If it is Possible that Event() function load after MyPage.xaml page is loaded?

Following the comments on the question, one possible answer would be to suscribe to the Loaded event of your page and call the Event method from there.
Really simple example:
public MyPage()
{
this.Loaded += PageLoaded;
}
void PageLoaded(object sender, RoutedEventArgs e)
{
this.Event();
}
So what we did is to suscribe to the loaded event on the constructor of the page. By the time the page is loaded, you will be able to call your Event method from the callback.

Related

How can I loop clicking on a button

I'm using GeckoFX and a regex in C# to make a YouTube video channel scraper. I want to get the URL of all videos from a selected channel.
I want to use GeckoFX (the Firefox rendering engine) to go to the videos section and click the Load more button until every video shows.
I am using this code to click Load more over and over:
javascript:(function() {
var LoadButton, IntervalCLB;
IntervalCLB = setInterval(_clickLoadButton, 100);
function _clickLoadButton() {
LoadButton = document.getElementsByClassName('load-more-button');
if (LoadButton.length > 0) {
LoadButton[0].click();
}
else {
clearInterval(IntervalCLB);
alert('Finished - Clicked all "Load More" Buttons.');
}
}
})();
I want to write something like that in C#.
My code looks like:
System.Threading.Thread.Sleep(1000);
GeckoDocument document = geckoWebBrowser1.Document;
GeckoHtmlElement Male = (GeckoHtmlElement)document.GetElementsByClassName("load-more-button")[0];
if ( Male != null ) {
Male.Click();
}
But that just clicks Load more once after one second.
How can I make it loop until the Load more button disappears?
Timer timer = new Timer(1000);
timer.Elapsed += ( sender, e ) => {
GeckoDocument document = geckoWebBrowser1.Document;
GeckoHtmlElement Male = (GeckoHtmlElement)document.GetElementsByClassName("load-more-button")[0];
if ( Male != null ) {
Male.Click();
}
};
timer.Start();
But from practical side I recommend you to look into YouTube API: https://developers.google.com/youtube/v3/code_samples/dotnet

Different functionalities of button[] at each click on a particular button

I have an array of button created dynamically, suppose 8 buttons, what I want is that when I click a particular button its background picture is changed and the name of button is stored in a linked list. When I click the same button again the background picture goes back to the original and the button name is deleted from linked list. Now I am able to do the first part, the second click is not working as I want it to.
Basically it's a datastructures project (shopping store) therefore I am using linked list, I have a linked list whose content is displayed through picture boxes[] and labels. Here what i am trying to do is when I click the picture box, the content of that particular node is added to a new linked list (added to the cart) and when I click on the picturebox again that particular item is deleted from the linked list (removed from the cart). Clicking it for the first time it is doing what i want it to do but the second click is not really working.
It's a datastructures project therefore I can't really use any built in classes for linked list, I had to write all methods myself and I did and they work.
cb[i].Click += (sender, e)=>{
if (flag == 0) {
// Console.WriteLine(obj.Retrieve(index).NodeContent);
// Console.WriteLine(obj.Retrieve(index).number);
inv.Add(obj.Retrieve(index).NodeContent, obj.Retrieve(index).number);
bill += Convert.ToInt32(obj.Retrieve(index).number);
cb[index].Image = Image.FromFile(#"F:\uni work\3rd semester\project images\rcart.jpg");
flag++;
}
else if (flag == 1)
{
// Console.WriteLine(bill);
bill -= Convert.ToInt32(obj.Retrieve(index).number);
// Console.WriteLine(bill);
inv.Delete(index);
cb[index].Image = Image.FromFile(#"F:\uni work\3rd semester\project images\cart.png");
flag--;
}
Since you are using a LinkedList it does have a Contains Method and a Remove Method that take a string. You haven't specified exactly what your problem is this should work. When you assign images to a control you loose the information that tells you what Image it is.
public partial class Form1 : Form
{
LinkedList<String> myList = new LinkedList<String>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 8; i++)
{
Button b = new Button() { Height = 30, Width = 70, Location = new Point(i, 50 * i),Name = "NewButton" + (i + 1).ToString() , Tag=i};
b.Click += b_Click;
this.Controls.Add(b);
}
}
void b_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
if(myList.Contains(b.Name)) //Check if button is in the List then Change Picture and remove
{
b.BackgroundImage = Properties.Resources.Peg_Blue;
myList.Remove(b.Name);
}
else
{
b.BackgroundImage = Properties.Resources.Peg_Red;
myList.AddLast(b.Name);
}
}
}
Why not create a class for each button, containing the two images and switch between them on each click?

wp8: open htm page in webBrowser by button click?

I have several html pages in my app. I named those html files as f1.html, f2.html, f3.html, \... f454.html. So, I want to display these files by user preference. So, I have created a textbox and button in CustomMessageBox using NuGet and an xaml page with the name webview.xaml. If user entered 3 in the textbox, f3.html should be open in webview.xaml.
I don't know how to code. Best answer will much appreciated seriously.
C# Code I did til now [UPDATE];
TextBox getFileNo = new TextBox();
getFileNo.Height = 72;
getFileNo.Width = 150;
getFileNo.MaxLength = 3;
TextBox getHashNo = new TextBox();
getHashNo.Height = 72;
getHashNo.Width = 150;
getHashNo.MaxLength = 3;
string showFile;
showFile = getFileNo.Text;
string hashId;
hashId = getHashNo.text;
NavigationService.Navigate(new Uri("/webview.xaml?Page=" + site, UriKind.Relative));
In webview.xaml:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.ContainsKey("Page"))
{
var page = NavigationContext.QueryString["Page"];
browser.Navigate(new Uri("/f" + page + ".html#" + hashId, UriKind.Relative));
}
}
You can navigate to the webview.xaml page passing it querystring with the desired hmtl file:
NavigationService.Navigate(new Uri(String.Format("/webview.xaml?Page={0}&id={1}", showFile, hashId), UriKind.Relative));
In your webview.xaml page, you can override OnNavigatedTo method to check the passed page and open it in web browser:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.ContainsKey("Page") && NavigationContext.QueryString.ContainsKey("id"))
{
var page = NavigationContext.QueryString["Page"];
var hashId = NavigationContext.QueryString["id"];
yourWebBrowser.Navigate(new Uri(String.Format("/f{0}.html#{1}", page, hashId), UriKind.Relative));
}
}
See Passing paramenters section from How to perform page navigation on Windows Phone for more information.

How to refresh Web Usercontrol on button click

I am using a web user control on an aspx page for displaying the locations on a google map into which I am passing locations as parameters from ASPX page load event and whenever the page is loaded I am getting the locations perfectly.
I have to change the locations whenever user searches for a particular location on clicking the "Search" button ion the ASPX page and i am passing the locations to web user control in the "search" button click event but unable to get the locations onto the web user control.
public partial class MapControl : System.Web.UI.UserControl
{
public List<string> MyLocation {get; set; }
protected void Page_Load(object sender, EventArgs e)
{
GeoCode geocode;
List<GMarker> markers = new List<GMarker>();
for (int i = 0; i < MyLocation.Count(); i++)
{
geocode = GMap1.getGeoCodeRequest(MyLocation[i]);
GLatLng gLatLng = new GLatLng(geocode.Placemark.coordinates.lat, geocode.Placemark.coordinates.lng);
GMap1.setCenter(gLatLng, 20, GMapType.GTypes.Normal);
GMarker oMarker = new GMarker(gLatLng);
markers.Add(oMarker);
//GMap1.Add(oMarker);
GListener listener = new GListener(oMarker.ID, GListener.Event.click, string.Format(#"function () {{ var w = new google.maps.InfoWindow(); w.setContent('<center>{0}</center>'); w.open({1}, {2});}}", "<b>" + MyLocation[i] + "</b>", GMap1.GMap_Id, oMarker.ID));
GMap1.Add(listener);
}
GMap1.Add(new GMapUI());
GMap1.GZoom = 10;
protected void Button1_Click1(object sender, EventArgs e)
{
List<string> locations = new List<string>();
for (int i = 0; i < ListView1.Items.Count; i++)
{
Label addressline1 = (Label)this.ListView1.Items[i].FindControl("addr1");
string addrloc = addressline1.Text;
locations.Add(addrloc);
}
MapControl mc = LoadControl("~/MapControl.ascx") as MapControl;
mc.MyLocation = locations;
}
And i included this web user control into Search.aspx file and and when i passed locations from page load event its working but when i tried to pass locations from search button click event getting Null value into Mylocations value of Web user control as null.. so Is there any way to to reload the User control with newer locations.
I think you can use here updatepanel. You have to keep google map into update panel and set trigger against this button to your map. Then I think you will get what you want. If any confusion pls let me know

highlighting the button that fired an event

Consider a SilverLight project that has 31 hyperlinkbuttons. Those represent the days of the month. I'm using this code to highlight the hyperlinkbutton that respresent today's day.
var daynumberHyperButton = this.FindName("Day" + DateTime.Today.Day) as HyperlinkButton;
//Highlighting the day of the month
if (daynumberHyperButton != null)
{
daynumberHyperButton.Background = new SolidColorBrush(Colors.Gray);
}
Then if I click on this highlighted hyperlinkbutton, it will open a childwindow to write some report.
private void dayHyperLink_Click(object sender, RoutedEventArgs e)
{
//This will initite and show the report window
ReportWindow rapport = new ReportWindow();
rapport.Closed += new EventHandler(rapport_Closed);
rapport.Show();
}
When I close the childwindows by clicking the OK button, it changes the color of the hyperlinkbutton that was highlighted (todays day) because I'm using this code to do that:-
private void rapport_Closed(object sender, EventArgs e)
{
ReportWindow rapport = (ReportWindow)sender;
var daynumberHyperButton = this.FindName("Day" + DateTime.Today.Day) as HyperlinkButton;
if (rapport.UsersValue == "Röd" && rapport.DialogResult==true)
{
daynumberHyperButton.Background = new SolidColorBrush(Colors.Red);
}
else if (rapport.UsersValue == "Gul")
{
daynumberHyperButton.Background = new SolidColorBrush(Colors.Yellow);
}
else
{
daynumberHyperButton.Background = new SolidColorBrush(Colors.Green);
}
}
But if I click on any other hyperlinkbutton that is not highlighted, it still only change the color of the highlighted hyperlinkbutton. I know this because my rapport_Closed event has:
var daynumberHyperButton = this.FindName("Day" + DateTime.Today.Day) as HyperlinkButton;
How can I change the above code, which is part of my rapport_Closed event, so that it changes the color of the event firing (the one that opens the childwindow) hyperlinkbutton, no matter which hyperlinkbuttonis the one that fires the event?
Ok now i can say i've done it. Here is what i did if anyone have a similar problem.
In may Home.xaml.cs, i added a public property like this:-
public HyperlinkButton dayHyperLink { get; set; }
To the Click event i added this code:-
dayHyperLink = (HyperlinkButton)sender;
To the rapport_Closing event i changed the if statment to the code below:-
if (rapport.UsersValue == "Röd" && rapport.DialogResult == true)
{
dayHyperLink.Background = new SolidColorBrush(Colors.Red);
}
This made me feel happy ;)

Categories

Resources