I'm creating an app for Windows Phone 8.1 (Runtime app). I'm using ThemeResource everywhere to pick up the current foreground and background brushes dynamically. But one problem I'm facing is with the StatusBar. I'm setting it's color to be the PhoneChromeBrush, which again changes based upon the theme. I see that the foreground color and the background color can only be set from code behind.
This is the code:
var statusBar = StatusBar.GetForCurrentView();
statusBar.BackgroundColor = (App.Current.Resources["PhoneChromeBrush"] as SolidColorBrush).Color;
statusBar.BackgroundOpacity = 1;
statusBar.ProgressIndicator.ProgressValue = 1;
await statusBar.ShowAsync();
What is the way to change the color of StatusBar when the theme actually changes? Is there any event I can listen to for theme changes?
I'm not sure of any event you can listen to for when the theme changes, but if you are changing the theme yourself, then this should work:
MainPage.xaml
<Button Content="Use Default Theme" Name="useDefaultThemeButton"
Click="useDefaultThemeButton_Click"/>
<Button Content="Use Light Theme" Name="useLightThemeButton"
Click="useLightThemeButton_Click"/>
<Button Content="Use Dark Theme" Name="useDarkThemeButton"
Click="useDarkThemeButton_Click"/>
MainPage.xaml.cs
using Windows.UI.ViewManagement;
using Windows.UI;
// Use Default Theme
private void useDefaultThemeButton_Click(object sender, RoutedEventArgs e)
{
RequestedTheme = ElementTheme.Default;
StatusBar.GetForCurrentView().ForegroundColor = null;
}
// Use Light Theme
private void useLightThemeButton_Click(object sender, RoutedEventArgs e)
{
RequestedTheme = ElementTheme.Light;
// exact color unknown
StatusBar.GetForCurrentView().ForegroundColor = Colors.Black;
}
// Use Dark Themme
private void useDarkThemeButton_Click(object sender, RoutedEventArgs e)
{
RequestedTheme = ElementTheme.Dark;
// exact color unknown
StatusBar.GetForCurrentView().ForegroundColor = Colors.White;
}
I actually fixed this by putting the same code in the Window.Current.Activated event handler instead of OnLaunched. I thought OnLaunched would be called every time but it's not the case.
Related
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))
};
}
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 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
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"]
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.