How to keep AutoSuggestBox suggestion box open in Windows Phone 8.1 - windows-runtime

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.

Related

How do I remove focus from a button, so that pushing the spacebar or Enter doesn't trigger the button?

When you click or tap a button, it gives it focus, so that when you push the spacebar or Enter, it triggers the button again. I don't want that to happen.
I tried this: button1.Focus(Windows.UI.Xaml.FocusState.Unfocused);
But the unwanted behavior still happens (pushing the spacebar or Enter triggers the button again). I also tried setting focus to another button using the Programmatic and Keyboard FocusStates but that doesn't fix it either.
Any help, especially an explanation of why this is happening, would be greatly appreciated.
You can set the TabStop property to False, but be aware that doing this disallows user from using the button by keyboard.
<Button Content="Click Me By Pointer!" IsTabStop="False"/>
Update
As msdn states:
You can't remove focus from a control by calling this method with FocusState.Unfocused as the parameter. This value is not allowed and causes an exception. To remove focus from a control, set focus to a different control.
So another way to achieve this, is setting focus to previously focused element when the button is about to get focus by pointer. Unfortunatly the GettingFocus event is not working for the Button class (maybe because final version is not released yet?) so we should use GotFocus event, it's ugly it but works.
bool moveFocus = false;
public MainPage() {
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e) {
if (moveFocus) {
UIElement focusedElement = FocusManager.FindNextFocusableElement(FocusNavigationDirection.Right);
if (focusedElement is Control) {
((Control)focusedElement).Focus(FocusState.Programmatic);
}
}
}
private void Button_GotFocus(object sender, RoutedEventArgs e) {
Button button = (Button)sender;
moveFocus = (button.FocusState == FocusState.Pointer);
}
Note that I passed FocusNavigationDirection.Right to FocusManager.FindNextFocusableElement to get an element in right, I should have used FocusNavigationDirection.Previous instead to get previous element but it returns null for unknown reason.

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 do you debug PhotoTaskChooser in WP8 on a physical device?

I have the simplest possible app. The UI is a page with nothing on it but a Tap method on a grid.
The code-behind looks like this...
public partial class MainPage : PhoneApplicationPage
{
private PhotoChooserTask _photoChooser;
// Constructor
public MainPage()
{
InitializeComponent();
_photoChooser.Completed += OnPhotoChosen;
}
private void OnTap(object sender, System.Windows.Input.GestureEventArgs e)
{
_photoChooser.Show();
}
private void OnPhotoChosen(object sender, PhotoResult result)
{
}
}
Now, what happens is that when I debug this application ON THE DEVICE, it briefly shows the photo chooser but then immediately deactivates...I assume because the photo chooser has taken focus. But from everything I've read, this should NOT be happening because the PhotoChooserTask's Completed event has been wired up in the constructor for my page, which should explicitly prevent my app from deactivating when the photochooser is active.
What's even more confusing is that the app seems to work when I'm NOT debugging it. Once I've selected a photo in this scenario, my app regains the foreground.
Is this a bug with the debugger or something else?
So it turns out the solution is to debug using "Start New Instance" in Visual Studio rather than just hitting F5.

Windows Phone: How to Navigate another page which is using DispatcherTimer?

I have two pages Main.xaml which includes only a button and another page timer.xaml which includes a timer. After pressing the button in the main page I want to go to the another page and start timer. I am using following code:
enter code here
**Main Page:**
private void Start_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/timer.xaml", UriKind.Relative));
}
**timer Page:**
public Page1()
{
InitializeComponent();
dispatcherTimer.Interval = new TimeSpan(0, 0, 1, 0, 0);
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Start();
counter=0;
count.Text = counter.ToString();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
counter++;
count.Text = counter.ToString();
}
I can not see time in the timer page however, after pressing the button it will navigate to the timer page correctly but I can not see updates in my timer page. I am debugging my code and it seems that DistpacherTimer() works correctly but my timer page does not update. Do you know how can I fix this problem?
Finally, I could find the problem. the code that you can see above was not whole code that I have written. Actually, in my real code I was sending a list <> from main page to timer page. Passing data from one page to another page is a little tricky. My main problem was that I did not send the data from main page to timer page in correct way, so that caused problem. Thus, there was nothing wrong with my DispatcherTimer() class or Event handler.
In other words, the code above works well if you do not pass data (e.g. list, array and so on) from a page to another page in a wrong way.