Using a ToggleSwitch in App Settings - windows-phone-8

I want to use ToggleSwitch in the app settings. I am not able to save its state on IsolatedStorage so that it can be reflected on the MainPage. I have tried using the available Key/Value pair storage examples on msdn to perform this but have not been able to. Please write a precise solution if anyone knows.

Here you go:
in your settings page XAML
<toolkit:ToggleSwitch Header="Push Notifications"
Checked="PushNotificationsToggle_Checked" Unchecked="PushNotificationsToggle_Unchecked">
</toolkit:ToggleSwitch>
in your settings page code behind
private void PushNotificationsToggle_Unchecked(object sender, RoutedEventArgs e)
{
var settings = IsolatedStorageSettings.ApplicationSettings;
settings["PushNotifications"] = false;
settings.Save();
}
in your main page you can use this setting like this
var settings = IsolatedStorageSettings.ApplicationSettings;
Boolean usePushNotifications = (Boolean)settings["PushNotifications"]

Related

Windows Phone 8 - Message Box alternative

I have to develop an app for Windows Phone 8. I want the user to be briefly notified without force him to face a Message Box (and consequently having to press a button to dismiss it). Are there any alternatives? I'm from Android and something similar to a Toast would be perfect. Also having the opportunity to change the text color would be nice.
Thank you in advance.
There are toasts in Windows Phone as well but AFAIR they can be used only when your app is in the background. But there is a solution: ToastPrompt class from Coding4Fun toolkit which is visually equal to toast notification.
See details:
https://stackoverflow.com/a/13981508/1985167
You can use ToastPrompt from Coding4Fun toolkit,
like this,
private void ToastWrapWithImgAndTitleClick(object sender, RoutedEventArgs e)
{
var toast = GetToastWithImgAndTitle();
toast.TextWrapping = TextWrapping.Wrap;
toast.Show();
}
private static ToastPrompt GetToastWithImgAndTitle()
{
return new ToastPrompt
{
Title = "With Image",
TextOrientation = System.Windows.Controls.Orientation.Vertical,
Message = LongText,
ImageSource = new BitmapImage(new Uri("../../ApplicationIcon.png", UriKind.RelativeOrAbsolute))
};
}

How to keep AutoSuggestBox suggestion box open in Windows Phone 8.1

Using the new AutoSuggestBox control in Windows Phone 8.1 (WinRT XAML), I am trying to keep the suggestion box open all the time -- even after the user clicks a suggestion.
I have no problem starting with the suggestion box open by programmatically setting AutoSuggestBox.IsSuggestionListOpen = true;
Then I hook the SuggestionChosen event like this:
private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args) {
sender.Text = args.SelectedItem.ToString();
sender.IsSuggestionListOpen = true;
}
But unfortunately the suggestion box still closes after selecting an item, even though I set IsSuggestionListOpen to true.
Any help with getting it to stay open after a selection would be appreciated.
The solution I found to this is to hook the LayoutUpdated event.
I have the AutoSuggestBox in a PickerFlyout, so I only want the suggestion box open if the PickerFlyout is open (obviously). So I set a Tag property on the button that opens the PickerFlyout to identify if the PickerFlyout is open or closed. Then in the LayoutUpdated event of the AutoSuggestBox I set the IsSuggestionListOpen property to true if the PickerFlyout is open (and false if it's not).
The code:
private void PickerFlyout_Opened(object sender, object e) {
ActivatePickerFlyoutButton.Tag = "open";
}
private void PickerFlyout_Closed(object sender, object e) {
ActivatePickerFlyoutButton.Tag = "closed";
}
private void AutoSuggestBox_LayoutUpdated(object sender, object e) {
AutoSuggestBox.IsSuggestionListOpen = ((ActivatePickerFlyoutButton.Tag as string).Equals("open"));
}
That is the only place I need to set the IsSuggestionListOpen property, since the LayoutUpdated event fires at all the right times.

Windows phone 8.1 BackPressed not working properly

Windows phone 8.1 new to world. Basic function is back button click. Is that function not working properly is this windows phone 8.1. Is that behavior or i'm made mistake.
Below code using in Homepage but this code calling from all other class too while clicking back. I need to access below method only on Home page .
Please check below code and refer me good solution.
Please look my code:
public HomePage()
{
this.InitializeComponent();
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
}
Thanks
It is working properly. The BackPressed event is working app-wide. Two options that come to my mind:
write eventhandler that would recognize the Page in which you currently invoke it - simple example can look like this:
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
Frame frame = Window.Current.Content as Frame;
if (frame == null) return;
if (frame.Content is HomePage)
{
e.Handled = true;
Debug.WriteLine("I'm in HomePage");
}
else if (frame.CanGoBack)
{
frame.GoBack();
e.Handled = true;
}
}
second option - subscribe to Windows.Phone.UI.Input.HardwareButtons.BackPressed when you enter the Page and unsubscribe when you leave the Page. Note that in this way there are some pitfalls - you have to handle properly OnNavigatedTo, OnNavigatedFrom, Suspending and Resuming (more about Lifecycle here). Note also that the subscription should be done before others - for example NavigationHelper.
Some remarks - the above code should work, but it also depends on other circumstances:
if there is something other subscribed to BackPressed before (in App.xaml.cs) - remember that usually events are fired in order they were subscribed
check if you are using NavigationHelper - it also subscribes to BackPressed
remember not to subscribe multiple times
remember to allow the User to leave your HomePage

how can i set the selectedIndex in a listPicker which i retrieve for isolatedStorageSettings in windows phone 8

I am developing a windows phone 8 app in which i have to use listPicker control. I need to save the selectedIndex from selected item in listPicker, in isolatedStorageSettings to be able to use it when the app opens. I want the saved index to be the selected index in my listPicker when the apps runs again. I have tried to do this with the onnavigatedto and onnavigatedfrom methods in the page in which i have the control. The problem is when i change se selected item and return back from full mode, the selected item does not change. I had searched this problem heare again and i didn't found the solution yet. How can i solve it?
Sorry for my English
I followed this settings_sample for the general setup by modifying the ListBox example. I ran into several problems trying to use a ListPicker with isolated storage like this.
http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff769510(v=vs.105).aspx
I removed the databindings for the ListPicker, set the SelectedIndex after initializing, and stored the SelectedIndex in isolated storage on SelectionChanged after the first occurrence of loading the page. It's a roundabout solution, but my searches came up empty.
public List<string> daysOfWeek = new List<string>() { "Sunday", "Monday", "etc" };
public int listPickerCounter = 0;
public Settings()
{
InitializeComponent();
BuildLocalizedApplicationBar();
// Fill listPicker with string items
this.listPicker.ItemsSource = daysOfWeek;
// Set SelectedIndex = IsolatedStorage Variable
if (IsolatedStorageSettings.ApplicationSettings.Contains("ListPickerSetting"))
{
this.listPicker.SelectedIndex = (int)IsolatedStorageSettings.ApplicationSettings["ListPickerSetting"];
}
}
On SelectionChanged update the isolated storage after the first occurrence of loading page.
private void listPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (listPickerCounter > 0 && IsolatedStorageSettings.ApplicationSettings.Contains("ListPickerSetting"))
{
IsolatedStorageSettings.ApplicationSettings["ListPickerSetting"] = (int)this.listPicker.SelectedIndex;
}
listPickerCounter++;
}
Edit: forgot to add another reference that really helped understand isolated storage.
http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj714090(v=vs.105).aspx

how to display a video from a JList?

I am currently doing an application using Swing and i am stuck at a certain point. In my function, i have to link videos from a JList. The problem is i am not sure how to link the videos from the JList. I am using an OpenBrowser class to link the video to the internet. I did consider using JButton but i would have to hardcode it and that would be ugly. Is there any other alternatives to do this? I am in desperate need and would be eternally grateful to whoever that can help me.
Safa :)
If you don't want to open a browser with the video using a selection listener, you can consider the idea of launching it with a double click on a JList entry.
sample code
String[] items = {"i1", "i2", "i3", "i4"};
JList list = new JList(items);
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
JList list = (JList)evt.getSource();
if (evt.getClickCount() == 2) { //check if it is a Double-click
int index = list.locationToIndex(evt.getPoint());
// do whatever you want with the entry at that index
}
}
});
Desktp classes to browse some site (sample code):
if (desktop.isSupported(Desktop.Action.BROWSE)) {
URI uri = new URI("http://www.google.com");
desktop.browse(uri);
}
The desktop.browse() call will open your favourite browser with the given URL.