ProgressIndicatior is not initialized - windows-phone-7

In MainPage I have one button. When user clicks on that button, I'm initializing the progress indicator variable. But it is not initializing and it showing null while debugging.
So, how should we show Progress Indicator in windows phone 8 for some task.
Below is my code.
ProgressIndicator pi;
private void search_button_clicked(object sender, RoutedEventArgs e)
{
pi = Microsoft.Phone.Shell.SystemTray.ProgressIndicator;
//Show the indicator
pi.IsVisible = true;//Here I'm getting null reference exception
........here the code for download xml file by calling web service............
}
I don't understand why Progress Indicator variable is not initializing.

you may try this ...
private void ShowProgressIndicator(String msg)
{
if (ProgressIndicator == null)
{
ProgressIndicator = new ProgressIndicator();
ProgressIndicator.IsIndeterminate = true;
}
ProgressIndicator.Text = msg;
ProgressIndicator.IsVisible = true;
SystemTray.SetProgressIndicator(this, ProgressIndicator);
}
private void HideProgressIndicator()
{
ProgressIndicator.IsVisible = false;
SystemTray.SetProgressIndicator(this, ProgressIndicator);
}
and then in your button click
private void search_button_clicked(object sender, RoutedEventArgs e)
{
ShowProgressIndicator("Searching");
}
i hope this might help you .....

Related

PositionChanged event gives the old location details when application is restarted

I have the below code which works fine except that it gives the last location details it tracked when application is restarted.
And it gives the correct location details after I click on btnGetLocationDetails button 2 or 3 times.
Any idea how I can fix this issue so that every time app is launched user doesn't need to click on this button 2 or 3 times to get the correct current location?
Code:
public partial class MainPage : PhoneApplicationPage
{
GeoCoordinateWatcher watcher = null;
Location location=null;
private void btnGetLocationDetails_Click_1(object sender, RoutedEventArgs e)
{
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
if (watcher.Permission == GeoPositionPermission.Granted)
{
watcher.MovementThreshold = Convert.ToDouble("100");
}
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>(watcher_PositionChanged);
// PositionChanged events occur whenever your position changes
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_OnStatusChanged);
watcher.Start();
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
//get the coordinates
location = new Location();
location.Latitude = e.Position.Location.Latitude;
location.Longitude = e.Position.Location.Longitude;
location.Altitude = e.Position.Location.Altitude;
}
void watcher_OnStatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
if (e.Status == GeoPositionStatus.Disabled)
MessageBox.Show("The location service is turned off.");
else if (e.Status == GeoPositionStatus.NoData)
MessageBox.Show("No location data is available. ");
}
}

How to move from popup page to other pages in windows phone application

I am developing a small game type application,when user wins the game he will get the popup as win for this I wrote the below code.
public void stoptimer()
{
if ((Convert.ToString(b1.Content) == "1") && (Convert.ToString(b2.Content) == "2") && (Convert.ToString(b3.Content) == "3") && (Convert.ToString(b4.Content) == "4") && (Convert.ToString(b5.Content) == "5") && (Convert.ToString(b6.Content) == "6") && (Convert.ToString(b7.Content) == "7") && (Convert.ToString(b8.Content) == "8") && (Convert.ToString(b9.Content) == "9") && (Convert.ToString(b10.Content) == "10") && (Convert.ToString(b11.Content) == "11") && (Convert.ToString(b12.Content) == "12") && (Convert.ToString(b13.Content) == "13") && (Convert.ToString(b14.Content) == "14") )
{
newTimer.Stop();
time = txtClock.Text;
//textBox2.Text = txtClock.Text;
Popup buyNowScreen;
buyNowScreen = new Popup();
buyNowScreen.Child =
new popupscreen
();
buyNowScreen.IsOpen = true;
buyNowScreen.VerticalOffset = 100;
buyNowScreen.HorizontalOffset = 25;
}
}
And I wrote the below code for navigate from popup page to other pages but it is not working getting the NullReferenceException.
private void button3_Click(object sender, RoutedEventArgs e)
{
ClosePopup();
NavigationService.Navigate(new Uri("/Menu.xaml", UriKind.Relative));
}
private void button1_Click(object sender, RoutedEventArgs e)
{
ClosePopup();
NavigationService.Navigate(new Uri("/NumericEasy.xaml", UriKind.Relative));
}
private void ClosePopup()
{
Popup buyPop = this.Parent as Popup;
if (buyPop.IsOpen)
{
buyPop.IsOpen = false;
}
}
Here is the procedure for giving events to popup page's controls.
Popup buyNowScreen=new Popup();
popupscreen popup1=new popscreen();
buyNowScreen.Child =popup1;
buyNowScreen.isOpen=true;
popup1.button1.click+= new RoutedEventHandler(btn_playagain_click);
private void btn_playagain_click(object sender, EventArgs e)
{
p.IsOpen = false;
NavigationService.Navigate(new Uri("/NumericEasy.xaml?Refresh=true", UriKind.Relative));
}
Navigation service works between pages, you cant use a popup so simple. This service can't find, what page to use, that's why it's throwing an exception.
To solve your problem, you should do this:
Popup class
1)At your popup constructor you should get PhoneApplicationPage object.
private PhoneApplicationPage _page;
public SomePopup(PhoneApplicationPage page)
{
_page = page;
2) You should create a new type for click event.
public delegate void NavigateHandler(object sender, EventArgs e, PhoneApplicationPage page);
public event NavigateHandler NavigateFromPopup;
3) At your button_click event you should call it:
public void ButtonX_Click(object sender, RoutedEventArgs e)
{
if (NavigateFromPopup!= null)
NavigateFromPopup(this, EventArgs.Empty, _page);
}
Your page class
4) At your page, when you create a popup, you should add this eventHandler:
SomePopup p = new SomePopup(this);
p.NavigateFromPopup +=new SomePopup.NavigateHandler(p_NavigateFromPopup);
5) And finally, at your page you should write down this event:
private void p_NavigateFromPopup(object sender, EventArgs e, PhoneApplicationPage page)
{
page.NavigationService.Navigate(new Uri("...", UriKind.RelativeOrAbsolute));
}
That should work.

Where to access the isolated storage value?

public IsolatedStorageSettings appSettings =
IsolatedStorageSettings.ApplicationSettings;
public Settings()
{
InitializeComponent();
this.toggle.Checked += new EventHandler<RoutedEventArgs>(toggle_Checked);
this.toggle.Unchecked += new EventHandler<RoutedEventArgs>(toggle_Unchecked);
this.toggle.Click += new EventHandler<RoutedEventArgs>(toggle_Click);
this.toggle.Indeterminate += new EventHandler<RoutedEventArgs>(toggle_Indeterminate);
`}`
void toggle_Unchecked(object sender, RoutedEventArgs e)
{
this.toggle.Content = "Visibity is off";
this.toggle.SwitchForeground = new SolidColorBrush(Colors.Red);
appSettings.Add("value", "off");
}
void toggle_Checked(object sender, RoutedEventArgs e)
{
this.toggle.Content = "Visibity is on";
this.toggle.SwitchForeground = new SolidColorBrush(Colors.Green);
appSettings.Add("value", "on");
}
void toggle_Indeterminate(object sender, RoutedEventArgs e)
{
//add some content here
}
void toggle_Click(object sender, RoutedEventArgs e)
{
//add some content here
}
by default calling checked method.If a user unchcked the button then again an user logged in app need to show the unchcked because the user previously unchcked the btn.but it's show checked.for that i am saving one value in isolated storage.
can you please tell me where to access the isolated varieable value ?
You can access the isolatedstorage value in any page, in the same way as you created it.
Try to access the value in Settings() constructor after the InitializeComponent();
public Settings()
{
InitializeComponent();
string value;
if (appSettings.Contains("value"))
{
appSettings.TryGetValue("value", out value);
}
and then you can change the value of toggle button based on the 'value'.
It seems that you are not calling the Save method on the ApplicationSettings object.
Please read this guide on how you should work with isolated storage.
To save a setting:
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (!settings.Contains("userData"))
{
settings.Add("userData", "some value");
}
else
{
settings["userData"] = "some value";
}
settings.Save();
To retrieve a setting:
if (IsolatedStorageSettings.ApplicationSettings.Contains("userData"))
{
string result IsolatedStorageSettings.ApplicationSettings["userData"] as string;
}
So in your case, save the state of the CheckBox in the Checked & UnChecked event, and load the state in the init of the page.

WP7 location tools doesn't fire positionChanged event

Trying to test my WP7 app that uses location following this tutorial.
I have additional tools open, start the emulator from VS, let the app launch and then I place a pin in Live-mode in the Additional Tools Location utility, but no event is fired.
Is there anything wrong with my code?
public MainPage()
{
InitializeComponent();
InitWatcher();
}
private void InitWatcher()
{
geoWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
geoWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(geoWatcher_PositionChanged);
geoWatcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(geoWatcher_StatusChanged);
}
private void geoWatcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
var lol = e;
}
private void geoWatcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
var FK = e;
}
The problem is that you have to start the GeoCoordinateWatcher:
geoWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
geoWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(geoWatcher_PositionChanged);
geoWatcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(geoWatcher_StatusChanged);
geoWatcher.Start();
Do you need to call the Start method on your GeoCoordinateWatcher instance?
http://msdn.microsoft.com/en-us/library/ee808853.aspx

PostBack and session values. Why after button is clicked and session created postback still show null

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblPostBack.Text = " Text created first time";
}
else
{
if (Session["Counter"] == null)
{
lblPostBack.Text = "PostBack x however strange becasue if is postback it's mean somebody clicked button and session value has been created";
}
else
{
lblPostBack.Text = "PostBack x should be count here";
}
}
}
protected void cmd_Click(object sender, EventArgs e)
{
int _counter;
if (Session["Counter"] == null)
{
_counter = 1;
}
else
{
_counter = (int)Session["Counter"] + 1;
}
Session["Counter"] = _counter;
lblPostBack.Text += "Counter: " + _counter.ToString();
}
Assuming this is ASP.NET: It's because the Click event on your button fires after the Load event on your page, so the session has not been set.
MSDN on the page lifecycle might be good reading - the button click is a "postback event" in the table in that document.
If I've got the wrong end of the stick, please explain what messages you get after the button clicks, and what you were expecting. Some framework and language tags on the question might not go amiss, either.
Ok it works, just FF mess up
I have added following method and works fine.
private int _counter;
protected void Page_Load(object sender, EventArgs e)
{
(...)
protected void Page_PreRender(Object sender, EventArgs e)
{
Session["Counter"] = _counter;
}

Resources