Where to access the isolated storage value? - windows-phone-7

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.

Related

Getting data from the observable collection to the button event

public ObservableCollection<WordList> MyWordList { get; set; }
public DictionaryPage()
{
InitializeComponent();
BindingContext = new DictionaryPageViewModel();
MyWordList = new ObservableCollection<WordList>
{
new WordList { Color = "Red", Letter = "A", Word = "Abdomen", Meaning = "Mean : " + "Mean",Detail= "Mean", Voice = "myVoice.mp3" }
};
}
private async void PronunciationButton_Clicked(object sender, System.EventArgs e)
{
await CrossMediaManager.Current.PlayFromAssembly("HERE HERE HERE");
}
I have an observable collection like this and I want to put the voice value in it to the button event below. Thanks for your help
MyWordList is a class level variable. Just reference it in your Clicked handler
private async void PronunciationButton_Clicked(object sender, System.EventArgs e)
{
For each (var word in MyWordList) {
await CrossMediaManager.Current.PlayFromAssembly(word.Voice);
}
}

ProgressIndicatior is not initialized

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 .....

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. ");
}
}

Unable to show the selected item in the Wp7 Listpicker control

Basically i am trying to pull the contacts from the phone and showing them in the Listpicker control for a feature in my app. I have two Listpickers, one for name of contacts list and the other showing the list of phonenumbers for the chosen contact.
Here is my code:
//Declarations
ContactsSearchEventArgs e1;
String SelectedName;
String SelectedNumber;
List<string> contacts = new List<string>();
List<string> phnum = new List<string>();
public AddressBook() // Constructor
{
InitializeComponent();
Contacts contacts = new Contacts();
contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contacts_SearchCompleted);
contacts.SearchAsync(string.Empty,FilterKind.None,null);
}
void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
e1 = e;
foreach (var result in e.Results)
{
if (result.PhoneNumbers.Count() != 0)
{
contacts.Add(result.DisplayName.ToString());
}
}
Namelist.ItemsSource = contacts;
}
private void Namelist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedName = (sender as ListPicker).SelectedItem.ToString();
phnum.Clear();
foreach (var result in e1.Results)
{
if (SelectedName == result.DisplayName)
{
phnum.Add(result.PhoneNumbers.FirstOrDefault().ToString());
}
}
Numbers.ItemsSource = phnum;
}
private void Numbers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedNumber = (sender as ListPicker).SelectedItem.ToString();
}
Am able to populate the Numberlist with phonenumbers for the chosen name at the Listpicker background, but the number is not showing up in the front. I think Numbers_SelectionChanged() event is called only one time when the page loads and am not seeing it triggerd when i change the contact list. Anyone has an idea of where am going wrong ?
If you change
List<string>
To
ObservableCollection<string>
this should work.
Also then you only need to set the ItemSource once, in Xaml or you constructor.
But you may run into another issue with the November 2011 Toolkit and ListPicker.
See more in thread.
private void Namelist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedName = (sender as ListPicker).SelectedItem.ToString();
phnum = new List<string>(); // Changed instead of phnum.Clear()
foreach (var result in e1.Results)
{
if (SelectedName == result.DisplayName)
{
phnum.Add(result.PhoneNumbers.FirstOrDefault().ToString());
}
}
Numbers.ItemsSource = phnum;
}
This works !!. While debugging i found its phnum.Clear() giving a problem. So i thought to create a new instance of phnum list for selected contact.

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