Toggle Button Checked Property Windows Phone 8 - windows-phone-8

I add a toggle button on Windows Phone 8 . When i checked (on), its save a value in Isolated storage and I check that value in constructor whatever it has a toggle value or null. If there is a toggle value I want to display toggle button checked . But I dont know the property to how to checked it when application is run .
Toggle Button XAML :
<toolkit:ToggleSwitch x:Name="toggle" Content="On" Width="165" FontSize="28" VerticalAlignment="Center" HorizontalAlignment="Right"/>
C# :
public Subscription()
{
InitializeComponent();
this.toggle.Checked += new EventHandler<RoutedEventArgs>(toggle_Checked);
this.toggle.Unchecked += new EventHandler<RoutedEventArgs>(toggle_Unchecked);
var appSettings = IsolatedStorageSettings.ApplicationSettings;
if (HasSValue() == "NoValue")
{
// Here i want to Display toggle button unchecked
}
else
{
// Here i want to Display toggle button checked
}
}
void toggle_Unchecked(object sender, RoutedEventArgs e)
{
this.toggle.Content = "Off";
this.toggle.SwitchForeground = new SolidColorBrush(Colors.Red);
var appSettings = IsolatedStorageSettings.ApplicationSettings;
appSettings.Remove("toggleValue");
appSettings.Save();
}
void toggle_Checked(object sender, RoutedEventArgs e)
{
this.toggle.Content = "On";
this.toggle.SwitchForeground = new SolidColorBrush(Colors.Green);
MessageBox.Show("R U Sure ?");
var appSettings = IsolatedStorageSettings.ApplicationSettings;
appSettings.Add("toggleValue", "MAHIN");
appSettings.Save();
}
public string HasSValue()
{
var appSettings = IsolatedStorageSettings.ApplicationSettings;
if (appSettings.Contains("toggleValue"))
{
return (string) appSettings["toggleValue"];
}
else
{
return "NoValue";
}
}

Try this
if (HasSValue() == "NoValue")
{
this.toggle.IsChecked = false;
}
else
{
this.toggle.IsChecked = true;
}
Hope this helps
Update your Constructor as follows
public Subscription()
{
InitializeComponent();
var appSettings = IsolatedStorageSettings.ApplicationSettings;
if (HasSValue() == "NoValue")
{
// Here i want to Display toggle button unchecked
}
else
{
// Here i want to Display toggle button checked
}
this.toggle.Checked += new EventHandler<RoutedEventArgs>(toggle_Checked);
this.toggle.Unchecked += new EventHandler<RoutedEventArgs>(toggle_Unchecked);
}

Related

Pass Pins values to next page

I am using map in my app and added some pins to it.
When I click on pin it display some info.
Now I want to pass the values/info of pins to next page after clicking on pin tooltip.
Here is what I am trying:
Pin pins;
protected async override void OnAppearing()
{
base.OnAppearing();
// workerListVM.DisplayMap();
var employeesDetail = await workerListVM.getdetail();
foreach (var employees in employeesDetail)
{
try
{
map1.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(employees.Latitude, employees.Longitude),Distance.FromMiles(.5)));
var position = new Position(employees.Latitude, employees.Longitude);
pins = new Pin()
{
Type = PinType.Place,
Position = position,
Label = employees.UserName,
Address = employees.City
};
map1.Pins.Add(pins);
pins.Clicked += Pins_Clicked;
}
catch (NullReferenceException) { }
catch (Exception) { }
}
}
private void Pins_Clicked(object sender, EventArgs e)
{
DisplayAlert($"{pins.Label}", $"{pins.Address}", "Ok");
}
But its not working.

HTML ExportToExcel Button

I have an ExportToExcel button in my application which is seen all the time. I want this to be changed to be shown only after I click submit and the result is shown.
Here is the code:
<asp:Button ID="btnExport" runat="server" Text="Export To Excel" OnClick = "ExportToExcel" />
protected void ExportToExcel(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "text/xls";
Response.AddHeader("content-disposition", "attachment;filename=HistoricalReporting.xls");
Response.Charset = "";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
GridView1.AllowPaging = false;
this.BindGridView();
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = #"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}

How to Keep Toggle Button state in Windows phone 8 application?

I create a toggle button and its working fine . But problem is that how can i Keep previous toggle activity, I mean when application is exit and reopen, it should display the previous toggle state . Here is my code
XAML:
<toolkit:ToggleSwitch x:Name="toggle" Content="ToggleSwitch is on" Header="ToggleSwitch"/>
CS :
public partial class EnglishSub : PhoneApplicationPage
{
public BanglaSub()
{
InitializeComponent();
this.toggle.Checked += new EventHandler<RoutedEventArgs>(toggle_Checked);
this.toggle.Unchecked += new EventHandler<RoutedEventArgs>(toggle_Unchecked);
this.toggle.Content = "ToggleSwitch is off";
}
void toggle_Unchecked(object sender, RoutedEventArgs e)
{
this.toggle.Content = "ToggleSwitch is off";
this.toggle.SwitchForeground = new SolidColorBrush(Colors.Red);
MessageBox.Show("Disable");
}
void toggle_Checked(object sender, RoutedEventArgs e)
{
this.toggle.Content = "ToggleSwitch is on";
this.toggle.SwitchForeground = new SolidColorBrush(Colors.Green);
MessageBox.Show("Enable");
}
}
You can use IsolatedStorageSetings to store application data. and read it when your app page is loaded again. here is how
public bool GetToggleValue()
{
if (IsolatedStorageSettings.ApplicationSettings.Contains("toggleValue"))
{
return bool.Parse(IsolatedStorageSettings.ApplicationSettings["toggleValue"].ToString());
}
else return false;
}
call above method in your page load to set the toggle value
and set checked unchecked value in the settings in your checked unchecked event handlers here is how
IsolatedStorageSettings.ApplicationSettings.Add("toggleValue", true);
IsolatedStorageSettings.Save();

IsolatedStorageSettings WP8

I have one problem which I cannot resolve (at least I didn't found solution yet...) I wanted to add data into IsolatedStorageSettings in WP8. My goal is to check if there are settings already in there, but when I want to check I get an exception which I cannot resolve.
private IsolatedStorageSettings appSettings = new IsolatedStorageSettings();
private void Button_Click(object sender, RoutedEventArgs e)
{
//correct this part
if (String.IsNullOrEmpty((string)appSettings["ICE1"])||
String.IsNullOrEmpty((string)appSettings["ICE2"]) ||
String.IsNullOrEmpty((string)appSettings["ICE3"]) ||
String.IsNullOrEmpty((string)appSettings["address"]) ||
String.IsNullOrEmpty((string)appSettings["fullName"]))
{
appSettings.Add("fullName", fullName.Text);
appSettings.Save();
appSettings.Add("address", address.Text);
appSettings.Save();
if (Regex.IsMatch(prefix1.Text, "^+\\d{2,3}") && Regex.IsMatch(number1.Text, "d{6,10}"))
{
appSettings.Add("ICE1", prefix1.Text + number1.Text);
appSettings.Save();
}
else
{
MessageBox.Show("Invalid prefix number, please use +01 and for numbers only 0-9...");
}
if (Regex.IsMatch(prefix2.Text, "^+\\d{2,3}") && Regex.IsMatch(number2.Text, "d{6,10}"))
{
appSettings.Add("ICE2", prefix2.Text + number2.Text);
appSettings.Save();
}
else
{
MessageBox.Show("Invalid prefix number, please use +01 and for numbers only 0-9...");
}
if (Regex.IsMatch(prefix2.Text, "^+\\d{2,3}") && Regex.IsMatch(number2.Text, "d{6,10}"))
{
appSettings.Add("ICE3", prefix2.Text + number2.Text);
appSettings.Save();
}
else
{
MessageBox.Show("Invalid prefix number, please use +01 and for numbers only 0-9...");
}
MessageBox.Show("Your information is saved...!");
}
else
{
//
appSettings["fullName"] = fullName.Text;
appSettings.Save();
appSettings["address"] = address.Text;
appSettings.Save();
if (Regex.IsMatch(prefix1.Text, "^+\\d{2,3}") && Regex.IsMatch(number1.Text, "d{6,10}"))
{
appSettings["ICE1"]= prefix1.Text + number1.Text;
appSettings.Save();
}
else
{
MessageBox.Show("Invalid prefix number, please use +01 and for numbers only 0-9...");
}
if (Regex.IsMatch(prefix2.Text, "^+\\d{2,3}") && Regex.IsMatch(number2.Text, "d{6,10}"))
{
appSettings["ICE2"] = prefix2.Text + number2.Text;
appSettings.Save();
}
else
{
MessageBox.Show("Invalid prefix number, please use +01 and for numbers only 0-9...");
}
if (Regex.IsMatch(prefix2.Text, "^+\\d{2,3}") && Regex.IsMatch(number2.Text, "d{6,10}"))
{
appSettings["ICE3"] = prefix3.Text + number3.Text;
appSettings.Save();
}
else
{
MessageBox.Show("Invalid prefix number, please use +01 and for numbers only 0-9...");
}
MessageBox.Show("Your information is saved...!");
}
Can someone explain how IsolatedStorageWorks?
How can I correct this part because this is causing the problem:
if (String.IsNullOrEmpty((string)appSettings["ICE1"])||
String.IsNullOrEmpty((string)appSettings["ICE2"]) ||
String.IsNullOrEmpty((string)appSettings["ICE3"]) ||
String.IsNullOrEmpty((string)appSettings["address"]) ||
String.IsNullOrEmpty((string)appSettings["fullName"]))
Do I need to do appSettings.Save() every each time when I wish to add the data or update-it...?
I also have another problem of getting data from IsolatedStorageSettings:
public WindowsPhoneControl2()
{
InitializeComponent();
}
string ice1;
string ice2;
string ice3;
PhoneCallTask ptask = new PhoneCallTask();
private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
ptask.PhoneNumber = ice1;
ptask.DisplayName = "Contact person 1";
ptask.Show();
}
private void Image_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
{
ptask.PhoneNumber = ice2;
ptask.DisplayName = "Contact person 2";
ptask.Show();
}
private void Image_Tap_2(object sender, System.Windows.Input.GestureEventArgs e)
{
ptask.PhoneNumber = ice3;
ptask.DisplayName = "Contact person 3";
ptask.Show();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
appSettings.Contains("fullName");
fullName.Text = (string)appSettings["fullName"];
address.Text = (string)appSettings["address"];
phone1.Text = (string)appSettings["ICE1"];
phone2.Text = (string)appSettings["ICE1"];
phone3.Text = (string)appSettings["ICE1"];
ice1 = phone1.Text;
ice2 = phone2.Text;
ice3 = phone3.Text;
}
especially this part which is supposed to get data from dictionary:
fullName.Text = (string)appSettings["fullName"];
address.Text = (string)appSettings["address"];
phone1.Text = (string)appSettings["ICE1"];
phone2.Text = (string)appSettings["ICE1"];
phone3.Text = (string)appSettings["ICE1"];
IsolatedStorage Settins works like a dictionary.
I think you should change:
private IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;
I would rather use method Contains to check whether key is there:
if (userSettings.Contains("ICE1")||
userSettings.Contains("ICE2") ||
userSettings.Contains("ICE3") ||
userSettings.Contains("address") ||
userSettings.Contains("fullName"))
According to MSDN, Settings are saved when the Application is closed or you call Save:
Data written to the IsolatedStorageSettings object is saved when the application that
uses the class is closed. If you want your application to write to isolated storage
immediately, you can call the Save method in application code.
EDIT
Sample code to play with:
public partial class MainPage : PhoneApplicationPage
{
private IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;
public MainPage()
{
InitializeComponent();
bool isKey = userSettings.Contains("anyKey"); //toggle break point at this line
userSettings.Add("anyKey", "myValue");
isKey = userSettings.Contains("anyKey");
string value = (string)userSettings["anyKey"];
}
}
IsolatedStorageSetting stores data in key/value pair. If there exist a key there must would be a value regarding this key. so just simpley check whether key exist in setting or not using if(settings.Contains("YourKey")). If the condition true do your work. Thanks

SWT JFace: SelectionProvider not working in TabFolder

In a GraphicalEditor I created a tab folder:
private final String[] tabNames = { "Text", "Image" };
private ResourcesTextComposite comText;
private ResourcesImageComposite comImage;
...
public void createPartControl(Composite parent) {
...
tabFolder = new TabFolder(parent, SWT.BORDER);
for (int loopIndex = 0; loopIndex < tabNames.length; loopIndex++) {
TabItem tabItem = new TabItem(tabFolder, SWT.NULL);
tabItem.setText(tabNames[loopIndex]);
if (loopIndex == 0) {
comText = new ResourcesTextComposite(tabFolder, SWT.NONE,
resources);
tabItem.setControl(comText);
} else if (loopIndex == 1) {
comImage = new ResourcesImageComposite(tabFolder, SWT.NONE,
resources);
tabItem.setControl(comImage);
}
}
...
}
it has 2 tab items and each item has a composite in it, and each composite has a TableViewer respectively.
I tried this to make each TableViewer the selection provider when the user selects the corresponding tab item (the same function createPartControl of the editor):
public void createPartControl(Composite parent) {
...
tabFolder.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
int tabIdx = tabFolder.getSelectionIndex();
getSite().setSelectionProvider(null);
if (tabIdx == 0) {
getSite().setSelectionProvider(comText.getViewer());
} else if (tabIdx == 1) {
getSite().setSelectionProvider(comImage.getViewer());
}
System.out.println("widgetSelected" + getSite() + ": "
+ getSite().getSelectionProvider());
}
public void widgetDefaultSelected(SelectionEvent e) {
widgetSelected(e);
}
});
...
}
I hope when I select a row in a TableViewer, the Properties view will show the selected model's properties, I've complete those IPropertySource things and they works well in other editors that has no tab folders, so I think the problem should be in the Selection Provider area.
Any ideas or has anyone encountered the same problem?
If you have multiple selection providers in a view or editor, then you need to use a mediator like org.eclipse.jdt.internal.ui.viewsupport.SelectionProviderMediator. Note that it is unfortunately internal, so you need to copy it to your own project