WP 8.1 ToggleButton Change Icon when Checked / UnChecked - xaml

I have the following XAML code for Windows Phone 8.1 (non SilverLight):
<Grid>
<ToggleButton Name="TogBtn" VerticalAlignment="Center" HorizontalAlignment="Center" Checked="ToggleButton_OnChecked">
<SymbolIcon Symbol="play"></SymbolIcon>
</ToggleButton>
</Grid>
The output of the above code is:
How can I change the icon to a stop icon when the toggle button is checked and then back to play icon when unchecked?
I thought this would be easy to find through Google, but apparently not.

Please change your XAML to this:
<Grid>
<ToggleButton x:Name="TogBtn" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="ToggleButton_Checked" Unchecked="ToggleButton_Unchecked">
<SymbolIcon Symbol="Play"></SymbolIcon>
</ToggleButton>
</Grid>
And please add this to your .cs file:
private void ToggleButton_Checked(object sender, RoutedEventArgs e)
{
TogBtn.Content = new SymbolIcon(Symbol.Stop);
}
private void ToggleButton_Unchecked(object sender, RoutedEventArgs e)
{
TogBtn.Content = new SymbolIcon(Symbol.Play);
}
That should do the job!

Related

How to create an identical button to the one in 'Settings' page?

I want to create buttons on my UWP/XAML app similar to the buttons on the Windows 'Settings' page using the WinUI 3 library. The gallery didn't show this kind of button with an icon and a title/description. I'm wondering how it can be accomplished since I am developing an app that mostly serves as a portal to go to websites and navigate easily through web/PC apps. Any help would be appreciated.
I want the buttons to simply redirect the user to a NavigationView page (Page5.xaml)
I have a NavigationView on MainPage.xaml and these buttons are going on Page4, so I'm not sure how I can program the buttons to go to Page5.xaml
https://i.stack.imgur.com/7Uff8.png
You can create a usercontrol and use some of my code:
MyUserControl.xaml:
<Grid x:Name="MainGrid" PointerEntered="Grid_PointerEntered" PointerExited="Grid_PointerExited" PointerPressed="FontIcon_PointerPressed" Margin="20,5,20,5" Height="70" CornerRadius="5" Padding="{StaticResource ExpanderHeaderPadding}" HorizontalAlignment="Stretch" Background="{ThemeResource ExpanderHeaderBackground}" BorderThickness="{ThemeResource ExpanderHeaderBorderThickness}" BorderBrush="{ThemeResource ExpanderHeaderBorderBrush}">
<StackPanel Orientation="Horizontal" Margin="0,20,0,20">
<FontIcon FontFamily="Segoe MDL2 Assets" Glyph="" Margin="0,0,20,0"/>
<TextBlock FontSize="24" VerticalAlignment="Center" HorizontalAlignment="Left" Text="Font"/>
</StackPanel>
<FontIcon FontFamily="Segoe MDL2 Assets" Glyph="" HorizontalAlignment="Right" Margin="0,0,20,0"/>
</Grid>
MyUserControl1.xaml.cs:
public MyUserControl1()
{
this.InitializeComponent();
var color = (MainGrid.Background as SolidColorBrush).Color;
color.A = 20;
MainGrid.Background = new SolidColorBrush(color);
}
private void FontIcon_PointerPressed(object sender, PointerRoutedEventArgs e)
{
if (e.GetCurrentPoint(sender as UIElement).Properties.IsLeftButtonPressed)
{
//Do whatever you want
Debug.WriteLine("Pressed");
}
}
//Change the color on hover:
private void Grid_PointerEntered(object sender, PointerRoutedEventArgs e)
{
var color = (MainGrid.Background as SolidColorBrush).Color;
color.A = 50;
MainGrid.Background = new SolidColorBrush(color);
}
private void Grid_PointerExited(object sender, PointerRoutedEventArgs e)
{
var color = (MainGrid.Background as SolidColorBrush).Color;
color.A = 20;
MainGrid.Background = new SolidColorBrush(color);
}

Long list selector windows phone child control event in DataTemplate

I am searching for this long time and i couldn't get it.
I have a Long list selector in my windows phone 8 project.
How can i manage the button event in each item in the data template? I need to get the selected item in that button event.
Code snippet shown below. Please help.
try this
// in your button click event type this code
var selectedValue = ((sender as Button).dataTemplate;
or
var selectedValue = ((sender as Button).dataTemplate as SbCaDd).AcNo;
If you want to access the dataContext then try this one.
XAML
<phone:LongListSelector Grid.Row="1"
Name="llsMsg"
LayoutMode="List"
VirtualizingStackPanel.VirtualizationMode="Recycling">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel>
<Grid>
<TextBlock Text="{Binding}"
Foreground="Black" />
<Button Content="View Details"
Width="200"
Click="Button_Click"/>
</Grid>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
C#
private void Button_Click(object sender, RoutedEventArgs e)
{
var dataContext = (sender as Button).DataContext;
var dataContext = (sender as Button).DataContext as YourDataModel;
}

Displaying a Progress Bar

I have an image control in my main page and the code is as follows:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel HorizontalAlignment="Left" Height="597" VerticalAlignment="Top" Width="440">
<Image x:Name="hinh1" Height="488" Stretch="Fill"/>
<ProgressBar Name="loading" Height="10" IsIndeterminate="True" Visibility="Collapsed"/>
</StackPanel>
</Grid>
and in code behind i have this code :
Uri hinh = new Uri
("http://taigamejar.net/wp-content/uploads/2014/01/Hinh-Anh-Dep-5.jpg", UriKind.Absolute);
hinh1.Source = new BitmapImage(hinh);
While waiting for the image to load, I want to call progress bar run to inform the user that it is loading. Once the the image has loaded, the progress bar should disappear. How can I do this?
If I were you, I would prefer to use , not ProgressBar.
So, I'll give
protected override void OnNavigatedTo(NavigationEventArgs e)
{
loading.IsActive = true;
Uri hinh = new Uri
("http://taigamejar.net/wp-content/uploads/2014/01/Hinh-Anh-Dep-5.jpg", UriKind.Absolute);
hinh1.Source = new BitmapImage(hinh);
hinh1.ImageOpened+=hinh1_ImageOpened; //loadingbar will be disappear when this triggered
}
private void hinh1_ImageOpened(object sender, RoutedEventArgs e)
{
loading.IsActive = false; //this will disable the progressring.
}
And XAML:
<StackPanel HorizontalAlignment="Left" Height="597" VerticalAlignment="Top" Width="400">
<Image x:Name="hinh1" Height="488" Stretch="Fill" ImageOpened="hinh1_ImageOpened"/>
<ProgressRing Name="loading" Height="109" IsActive="True" />
</StackPanel>
If you don't have WP8.1 SDK yet, you can get ProgressRing here: http://www.onurtirpan.com/onur-tirpan/english/windows-phone-english/using-progressring-in-windows-phone/

watermarked PasswordBox in winrt

is it possible to get a watermarks passwordbox in WinRt? It is no problem to get a textbox with a watermark, but I don't know a toolkit where I can get a password box with a watermark.
How can I implement one for myself?
Take a look on WinRT XAML Toolkit.
They also have
WatermarkTextBox
WatermarkPasswordBox
By yourself you can implement your own controls:
in .xaml:
<Border x:Name="brdPassword" Margin="5,0,5,10" BorderThickness="2" BorderBrush="White" CornerRadius="5" Grid.Row="0"
Background="White" Height="50" VerticalAlignment="Stretch">
<Grid>
<TextBox x:Name="PasswordWatermark" TextWrapping="Wrap"
Text="Watermark" Foreground="#FFC4C4C4" IsHitTestVisible="False"
Background="{x:Null}" BorderThickness="0" Padding="0,-10"
FontSize="26.667" />
<PasswordBox x:Name="pbPassword" LostFocus="PasswordLostFocus"
GotFocus="PasswordGotFocus" Background="{x:Null}"
FontSize="26.667" Margin="0,-12,0,-9" VerticalAlignment="Center"
BorderThickness="0" Opacity="0" />
</Grid>
</Border>
in .cs
private void PasswordLostFocus(object sender, RoutedEventArgs e)
{
CheckPasswordWatermark();
}
private void CheckPasswordWatermark()
{
var passwordEmpty = string.IsNullOrEmpty(pbPassword.Password);
PasswordWatermark.Opacity = passwordEmpty ? 100 : 0;
pbPassword.Opacity = passwordEmpty ? 0 : 100;
}
private void PasswordGotFocus(object sender, RoutedEventArgs e)
{
PasswordWatermark.Opacity = 0;
pbPassword.Opacity = 100;
}
Hope it's help
I don't think we can put watermark in the Password control.
You can put a TextBox with wartermark in the same row and same column with the Password control, then handle the two controls' GotFocus and LostFocus events to make the control Visible or Collapsed.
There is no toolkit yet which provides watermarked password box. However this may help:-
http://code.msdn.microsoft.com/windowsdesktop/Watermarked-TextBox-and-444ebdec
Also, check out http://julmar.com/blog/mark/?p=300 for both a Textbox and PasswordBox implementation for WinRT.

Play audio file in the background in Windows8 app?

I am developing an app that displays information while at the same time plays audio file in the background. This is a Windows 8 tablet app. Any help or suggestion is helpful.
Thanks
In the XAML, you will have a MediaElement with the following attributes
<ContentControl x:Name="Host">
<MediaElement Source="Assets/myMusic.mp3" IsLooping="True" AutoPlay="True" AudioCategory="BackgroundCapableMedia" PosterSource="/Images/placeholder-sdk.png" VerticalAlignment="Center" HorizontalAlignment="Center" Name="myMediaElement" Height="350" Width="640" />
</ContentControl>
In the App bar, you can have some buttons for media control like
<Page.BottomAppBar>
<AppBar Opened="BottomAppBar_Opened" Background="#FF3C3838" x:Name="BottomAppBarColors" Padding="10,0,10,0" AutomationProperties.Name="Bottom App Bar Colors">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*"/>
<ColumnDefinition Width="50*"/>
</Grid.ColumnDefinitions>
<StackPanel x:Name="LFullScreenLandscapeViewGrid" Orientation="Horizontal" Grid.Column="0" HorizontalAlignment="Left">
<Button x:Name="Play" Click="PlayButton_Click" Style="{StaticResource PlayAppBarButtonStyle}" Tag="Play"/>
<Button x:Name="Stop" Click="StopButton_Click" Style="{StaticResource StopAppBarButtonStyle}" Tag="Stop"/>
</StackPanel>
<StackPanel Visibility="Collapsed" x:Name="PortraitViewGrid" Orientation="Horizontal" Grid.ColumnSpan="2" HorizontalAlignment="Right">
<Button x:Name="PauseCollapsed" Click="PauseButton_Click" Style="{StaticResource PauseAppBarButtonStyle}" Tag="Pause"/>
<Button x:Name="PlayCollapsed" Click="PlayButton_Click" Style="{StaticResource PlayAppBarButtonStyle}" Tag="Play"/>
<Button x:Name="StopCollapsed" Click="StopButton_Click" Style="{StaticResource StopAppBarButtonStyle}" Tag="Stop"/>
</StackPanel>
</Grid>
</AppBar>
</Page.BottomAppBar>
In the .CS code file, you can control the mediaelement object.
private void PauseButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.Pause();
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.Stop();
}
private void ForwardButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.DefaultPlaybackRate = 0.0;
myMediaElement.PlaybackRate = 2.0;
}
private void RewindButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.DefaultPlaybackRate = 0.0;
myMediaElement.PlaybackRate = -1.0;
}
If you have multiple screens that use the media and controls to play, pause and stop the media file, then it's better to write code for these events once and then use it from different screens. I have seen people writing the following code in every screen they use the media.
private void PauseButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.Pause();
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.Stop();
}
private void ForwardButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.DefaultPlaybackRate = 0.0;
myMediaElement.PlaybackRate = 2.0;
}
private void RewindButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.DefaultPlaybackRate = 0.0;
myMediaElement.PlaybackRate = -1.0;
}
So instead of repeating the code in every screen, follow a good event pattern to reduce duplicate code
The best thing is to use MVVM pattern and you can find a sample at http://code.msdn.microsoft.com/windowsapps/Background-Audio-c-Metro-d2fc7719/view/SourceCode
So the design is to write up the code in one common place and fire them from the front end UI.
You need to use a MediaElement.