I made simple app in Windows Phone Silverlight 8.1.
It looks like cell phone keypad with lcd-looks like screen that shows number of current tapped button and plays tone sound of that number.
I've been searching for a while but i found nothing specific.
I Have problem with Play(); method because it does execute while i'm debugging but in realtime after app-deploy to my device i hear nothing...
I researched many topics and I can tell that it depends on
mediaElementObject.CurrentState;
Sometimes it's on "Closed" state and sometimes on "Opening"(and then i hear the sound while debugging).
C#
private void KeypadButtonClick(object sender, RoutedEventArgs e)
{
Button currentButton = sender as Button;
MediaElement keySound = null;
if (currentButton != null)
{
string buttonContentValue = currentButton.Content.ToString();
keySound = new MediaElement();
ContentPanel.Children.Add(keySound);
PlayTargetKeypadSound(buttonContentValue, keySound);
RefreshNumber(buttonContentValue);
}
if (keySound != null)
{
ContentPanel.Children.Remove(keySound);
}
}
private void PlayTargetKeypadSound(string buttonContentValue, MediaElement keySound)
{
String path = "/Assets/Sounds/keyNumber_" + buttonContentValue + ".wav";
keySound.CurrentStateChanged += KeySoundOnCurrentStateChanged;
keySound.Source = new Uri(path, UriKind.RelativeOrAbsolute);
keySound.Volume = 1.0;
keySound.AutoPlay = false;
}
private void KeySoundOnCurrentStateChanged(object sender, RoutedEventArgs routedEventArgs)
{
MediaElement sound = sender as MediaElement;
if (sound != null) sound.Play();
}
private void RefreshNumber(string buttonContentValue)
{
NumberTextBlock.Text = buttonContentValue;
}
XAML
<Grid x:Name="ContentPanel" Grid.Row="2" Margin="8,0,8,12">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- First Row -->
<Button Click="KeypadButtonClick" Content="1" Style="{StaticResource KeypadButtonStyle}" />
<Button Click="KeypadButtonClick" Content="2" Style="{StaticResource KeypadButtonStyle}" Grid.Column="1" />
<Button Click="KeypadButtonClick" Content="3" Style="{StaticResource KeypadButtonStyle}" Grid.Column="2" />
<!-- Second Row -->
<Button Click="KeypadButtonClick" Content="4" Style="{StaticResource KeypadButtonStyle}" Grid.Row="1" />
<Button Click="KeypadButtonClick" Content="5" Style="{StaticResource KeypadButtonStyle}" Grid.Row="1" Grid.Column="1" />
<Button Click="KeypadButtonClick" Content="6" Style="{StaticResource KeypadButtonStyle}" Grid.Row="1" Grid.Column="2" />
<!-- Third Row -->
<Button Click="KeypadButtonClick" Content="7" Style="{StaticResource KeypadButtonStyle}" Grid.Row="2" />
<Button Click="KeypadButtonClick" Content="8" Style="{StaticResource KeypadButtonStyle}" Grid.Row="2" Grid.Column="1" />
<Button Click="KeypadButtonClick" Content="9" Style="{StaticResource KeypadButtonStyle}" Grid.Row="2" Grid.Column="2" />
<!-- Fourth Row -->
<Button Click="KeypadButtonClick" Content="*" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" />
<Button Click="KeypadButtonClick" Content="0" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" Grid.Column="1" />
<Button Click="KeypadButtonClick" Content="#" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" Grid.Column="2" />
</Grid>
Many thanks for helping
Ok.
I solved it...
C#
private void KeypadButtonClick(object sender, RoutedEventArgs e)
{
Button currentButton = sender as Button;
if (currentButton != null)
{
string buttonContentValue = currentButton.Content.ToString();
PlayTargetKeypadSound(buttonContentValue);
RefreshNumber(buttonContentValue);
}
}
private void PlayTargetKeypadSound(string buttonContentValue)
{
String path = "/Assets/Sounds/keyNumber_" + buttonContentValue + ".wav";
KeySoundMediaElement.MediaOpened += KeySoundMediaElementOnMediaOpened;
KeySoundMediaElement.Source = new Uri(path, UriKind.RelativeOrAbsolute);
}
private void KeySoundMediaElementOnMediaOpened(object sender, RoutedEventArgs routedEventArgs)
{
MediaElement sound = sender as MediaElement;
if (sound != null) sound.Play();
}
private void RefreshNumber(string buttonContentValue)
{
NumberTextBlock.Text = buttonContentValue;
}
XAML
<!-- Fourth Row -->
<Button Click="KeypadButtonClick" Content="*" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" />
<Button Click="KeypadButtonClick" Content="0" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" Grid.Column="1" />
<Button Click="KeypadButtonClick" Content="#" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" Grid.Column="2" />
<MediaElement
x:Name="KeySoundMediaElement"
Volume="1"
AutoPlay="False"
/>
Related
So I have an audio in WAV format https://youtu.be/nunJDdIlnnk but for some reason, I cannot get it to paste all the transcription in the Textbox, However in the method SpeechRecognizer_Recognizing I can see on my debug windows, that everything is being recognized as the woman speaks.
var config = SpeechConfig.FromSubscription(KEY, Region);
MessageBox.Show("Ready to use speech service in " + config.Region);
// Configure voice
config.SpeechSynthesisVoiceName = "en-US-AriaNeural";
// Configure speech recognition
var taskCompleteionSource = new TaskCompletionSource<int>();
using var audioConfig = AudioConfig.FromWavFileInput(FilePath);
using var speechRecognizer = new SpeechRecognizer(config, audioConfig);
speechRecognizer.Recognizing += SpeechRecognizer_Recognizing;
speechRecognizer.Recognized += SpeechRecognizer_Recognized;
speechRecognizer.SessionStarted += SpeechRecognizer_SessionStarted;
speechRecognizer.SessionStopped += SpeechRecognizer_SessionStopped;
await speechRecognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);
// Waits for completion.
// Use Task.WaitAny to keep the task rooted.
Task.WaitAny(new[] { taskCompleteionSource.Task });
await speechRecognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
}
private void SpeechRecognizer_SessionStopped(object? sender, SessionEventArgs e)
{
Debug.WriteLine("Stopped");
}
private void SpeechRecognizer_SessionStarted(object? sender, SessionEventArgs e)
{
Debug.WriteLine("Started");
}
private void SpeechRecognizer_Recognized(object? sender, SpeechRecognitionEventArgs e)
{
var transcriptionStringBuilder = new StringBuilder();
if (e.Result.Reason == ResultReason.RecognizedSpeech)
{
transcriptionStringBuilder.Append(e.Result.Text);
Text = transcriptionStringBuilder.ToString(); // This will be the text, that is displayed on textbox
IsVisible = Visibility.Visible;
}
}
private void SpeechRecognizer_Recognizing(object? sender, SpeechRecognitionEventArgs e)
{
Debug.WriteLine("Recogizing: " + e.Result.Text);
}
Output window
This is my XAML code for the UI
<Border Background="#272537"
CornerRadius="20">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="75" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Transcribe Me"
Foreground="White"
Margin="10,0,0,0"
FontSize="23"
VerticalAlignment="Center" />
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<syncfusion:SfHubTile Background="#FF1FAEFF">
<behaviors:Interaction.Triggers>
<behaviors:EventTrigger EventName="Click">
<behaviors:InvokeCommandAction Command="{Binding AzureCommand}"
CommandParameter="Audio" />
</behaviors:EventTrigger>
</behaviors:Interaction.Triggers>
<iconPacks:PackIconMaterial Kind="Music" Width="120"
Height="120"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="White" />
</syncfusion:SfHubTile>
<syncfusion:SfHubTile Grid.Column="1">
<syncfusion:SfHubTile.Header>
<TextBlock Text="Document"
Foreground="White"
FontSize="18"
FontStyle="Oblique" />
</syncfusion:SfHubTile.Header>
<syncfusion:SfHubTile.Title>
<TextBlock Text="Translate"
Foreground="White"
FontSize="18"
HorizontalAlignment="Center"
FontStyle="Oblique" />
</syncfusion:SfHubTile.Title>
<behaviors:Interaction.Triggers>
<behaviors:EventTrigger EventName="Click">
<behaviors:InvokeCommandAction Command="{Binding AzureCommand}"
PassEventArgsToCommand="True"
CommandParameter="Document" />
</behaviors:EventTrigger>
</behaviors:Interaction.Triggers>
</syncfusion:SfHubTile>
<TextBox Grid.Row="1"
IsReadOnly="True"
TextWrapping="Wrap"
Visibility="{Binding IsVisible}"
Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}"
AcceptsReturn="True"
Grid.ColumnSpan="2"
Margin="10" />
<Button Grid.Column="1"
Grid.Row="2"
Visibility="{Binding IsVisible}"
Content="Copy"
Command="{Binding CopyCommand}"
Foreground="White"
FontWeight="Black"
VerticalAlignment="Center"
Margin="0,0,10,10">
</Button>
</Grid>
</Grid>
</Border>
When I run the code, my text box only has the last part of the of the video, witch is "Do I look alright"
The problem was, that I was puttingin my textbox every single sentence that she said.
What you have to do, is to capture everything in the STOPPED method, and probably use a list of chars, loop thru every char, and crate a stringbulder, because using str += item, will create a string every time and is less efficient.
SUMMARY OF THE PROBLEM:
I have main window which has a frame and a button, in the frame i'm going to show one user control at a time (i provided an image of my usercontrol).
in the first UserControl, i will write 4 player names (two for each team).
after i press the button and i go to next usercontrol i want to show the names of the players in textblock. I also want to bind IsChecked property of radio button in first UserControl to the second UserControl. (i have other buttons that show up in mainWindow depends on what usercontrol is in the Frame right Now)
WHAT I TRIED:
I tried binding the textbox property to the textblock in the second usercontrol but it doesnt show or i cant access it.
because i have the button in the mainWindow i tried saving the names in variable then pass it to next usercontrol but didnt work also.
Also tried alot of random online solution i found online but nothing worked.
i need a way to expose the property of First UserControl to be used in the Second UserControl
MY CODE
MainWindow xaml
<Grid SnapsToDevicePixels="True" x:Name="myGrid" >
<Grid.Background>
<ImageBrush x:Name="myBackground" ImageSource="background1.png" Stretch="UniformToFill"/>
</Grid.Background>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="300" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="*" />
<RowDefinition Height=" auto" />
<RowDefinition Height=" 20" />
</Grid.RowDefinitions>
<Frame x:Name="Main" Grid.Row="0" Grid.RowSpan="2" Grid.Column="0" Grid.ColumnSpan="5" NavigationUIVisibility="Hidden" >
</Frame>
<Button Style="{StaticResource StandardButton}" Visibility="Visible" x:Name="playersBtn" Grid.Column="2" Grid.Row ="2" Margin="100,40,100,40" FontSize="36" Content="إبدأ" FontFamily="Ara Hamah Homs" Padding="50,0" Background="#FFFFE900" Click="playerInfoBtn" BorderBrush="{x:Null}" Foreground="Black" >
</Button>
MainWindow c#
namespace Try3
{
public partial class MainWindow : Window
{
SoundPlayer MP = new SoundPlayer();
public MainWindow()
{
InitializeComponent();
MP.SoundLocation = "C:/Users/eadh2/Desktop/game tries/try3/try3/sound1.wav";
MP.PlayLooping();
}
private void playerInfoBtn(object sender, RoutedEventArgs e)
{
Main.Content = new PlayersInfoPage();
playersBtn.Visibility = Visibility.Hidden;
startBtn.Visibility = Visibility.Visible;
icon.Visibility = Visibility.Collapsed;
ImageBrush b = new ImageBrush();
b.ImageSource = new BitmapImage(new Uri("pack://application:,,,/background flat.png"));
myGrid.Background = b;
}
private void startBtn_Click(object sender, RoutedEventArgs e)
{
Main.Content = new gamePage();
startBtn.Visibility = Visibility.Collapsed;
gamePageBoarder.Visibility = Visibility.Visible;
}
private void startBtn_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
startBtn.Background = Brushes.DarkGreen;
}
}
}
First usercontrol xaml to enter player names (Image of First UserControl to enter player names)
<UserControl x:Class="Try3.PlayersInfoPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Try3"
mc:Ignorable="d"
d:DesignHeight="862.179" d:DesignWidth="1210.632" >
<Grid SnapsToDevicePixels="True">
<Grid.ColumnDefinitions >
<ColumnDefinition Width="150" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width=" *" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="AUTO" />
<ColumnDefinition Width="150" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height=" *" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<TextBlock Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" Text="بيانات الفريق الأحمر" FontSize="36" Margin="50,0,50,0" FontFamily="Ara Hamah Homs" />
<TextBox x:Name="redFirst" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Margin="0,30,0,20" Padding="10" FontSize="36" HorizontalContentAlignment="Center" Text="اللاعب الأول" Foreground="#FF919191" GotMouseCapture="redFirst_GotMouseCapture" FontFamily="Ara Hamah Homs" />
<TextBox x:Name="redSecond" Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2" Margin="0,30,0,110" Padding="10" FontSize="36" HorizontalContentAlignment="Center" Text="اللاعب الثاني" Foreground="#FF919191" GotMouseCapture="redSecond_GotMouseCapture1" FontFamily="Ara Hamah Homs" />
<TextBlock Grid.Column="4" Grid.Row="1" Grid.ColumnSpan="2" Text="بيانات الفريق الأخضر " FontSize="36" Margin="50,0,50,20" FontFamily="Ara Hamah Homs" />
<TextBox x:Name="greenFirst" Grid.Row="2" Grid.Column="4" Grid.ColumnSpan="2" Margin="0,30,0,20" Padding="10" FontSize="36" HorizontalContentAlignment="Center" Text="اللاعب الأول" Foreground="#FF919191" GotMouseCapture="greenFirst_GotMouseCapture2" FontFamily="Ara Hamah Homs" />
<TextBox x:Name="greenSecond" Grid.Row="3" Grid.Column="4" Grid.ColumnSpan="2" Margin="0,30,0,110" Padding="10" FontSize="36" HorizontalContentAlignment="Center" Text="اللاعب الثاني" Foreground="#FF919191" GotMouseCapture="greenSecond_GotMouseCapture3" FontFamily="Ara Hamah Homs" />
<Border x:Name="roundsBoarder" Grid.Column="3" Grid.Row="2" Grid.RowSpan="2" BorderThickness ="3" BorderBrush="Black" CornerRadius="0" Margin="20,50,20,130" MaxHeight="170" MaxWidth="350" VerticalAlignment="Center" Background="#99FFFFFF" MouseEnter="Border_MouseEnter" MouseLeave="roundsBoarder_MouseLeave">
<StackPanel HorizontalAlignment="Center" >
<StackPanel Orientation="Horizontal" >
<Viewbox Height="40" Width="40" >
<RadioButton BorderBrush="#FFFFE900" VerticalContentAlignment="Center" HorizontalAlignment="Left" GroupName="roundsNumber" FontFamily="Ara Hamah Homs" FontSize="12"/>
</Viewbox>
<TextBlock Text="جولة" FontSize="42" Margin="20,0,0,0" FontFamily="Alnaqaaa R" />
</StackPanel>
<StackPanel Orientation="Horizontal" >
<Viewbox Height="40" Width="40" >
<RadioButton BorderBrush="#FFFFE900" VerticalContentAlignment="Center" HorizontalAlignment="Left" GroupName="roundsNumber" FontFamily="Ara Hamah Homs" FontSize="12"/>
</Viewbox>
<TextBlock Text="ثلاث جولات" FontSize="42" Margin="20,0,0,0" FontFamily="Alnaqaaa R" />
</StackPanel>
</StackPanel>
</Border>
</Grid>
First UserControl C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Try3
{
public partial class PlayersInfoPage : UserControl
{
public PlayersInfoPage()
{
InitializeComponent();
}
private void redFirst_GotMouseCapture(object sender, MouseEventArgs e)
{
redFirst.Text = "";
redFirst.Foreground = Brushes.Black;
}
private void redSecond_GotMouseCapture1(object sender, MouseEventArgs e)
{
redSecond.Text = "";
redSecond.Foreground = Brushes.Black;
}
private void greenFirst_GotMouseCapture2(object sender, MouseEventArgs e)
{
greenFirst.Text = "";
greenFirst.Foreground = Brushes.Black;
}
private void greenSecond_GotMouseCapture3(object sender, MouseEventArgs e)
{
greenSecond.Text = "";
greenSecond.Foreground = Brushes.Black;
}
private void Save_MouseEnter(object sender, MouseEventArgs e)
{
}
private void Save_MouseLeave(object sender, MouseEventArgs e)
{
}
private void Border_MouseEnter(object sender, MouseEventArgs e)
{
roundsBoarder.Background = new SolidColorBrush(Colors.White) { Opacity = 1 };
roundsBoarder.CornerRadius = new CornerRadius(20);
}
private void roundsBoarder_MouseLeave(object sender, MouseEventArgs e)
{
roundsBoarder.Background = new SolidColorBrush(Colors.White) { Opacity = 0.6 };
roundsBoarder.CornerRadius = new CornerRadius(0);
}
}
}
Second UserControl xaml to show player names in each side
<UserControl x:Class="Try3.gamePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:try3"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="50" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions >
<RowDefinition Height="50" />
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
<RowDefinition Height="50" />
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<StackPanel Orientation="Vertical" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" >
<TextBlock HorizontalAlignment="Center" FontSize="36" FontFamily="Ara Hamah Homs" Text="الفريق الأحمر" />
<TextBlock x:Name="redFirstPlayer" FontSize="24" FontFamily="Ara Hamah Homs" Text="اللاعب الاول" HorizontalAlignment="Center"/>
<TextBlock x:Name="redSecondPlayer" FontSize="24" FontFamily="Ara Hamah Homs" Text="اللاعب الثاني" HorizontalAlignment="Center"/>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Column="6" Grid.ColumnSpan="2" Grid.Row="1">
<TextBlock FontSize="36" FontFamily="Ara Hamah Homs" Text="الفريق الأخضر" HorizontalAlignment="Center" Margin="0,0,0,20"/>
<TextBlock x:Name="greenFirstPlayer" FontSize="24" FontFamily="Ara Hamah Homs" Text="اللاعب الاول" HorizontalAlignment="Center"/>
<TextBlock x:Name="greenSecondPlayer" FontSize="24" FontFamily="Ara Hamah Homs" Text="اللاعب الثاني" HorizontalAlignment="Center"/>
</StackPanel>
<TextBlock x:Name="redScore" Grid.Column="1" Grid.Row="3" FontSize="42" FontFamily="Ara Hamah Homs" Text="0" HorizontalAlignment="center" />
<Image Source="trophy (1).png" Grid.Column="2" Grid.Row="3" HorizontalAlignment="Left"/>
<TextBlock x:Name="greenScore" Grid.Column="7" Grid.Row="3" FontSize="42" FontFamily="Ara Hamah Homs" HorizontalAlignment="center" Text="0" />
<Image Source="trophy (1).png" Grid.Column="6" Grid.Row="3" HorizontalAlignment="Right"/>
<StackPanel Grid.Row="1" Grid.Column="6" Grid.ColumnSpan="2" Margin="0,50,0,0" >
</StackPanel>
</Grid>
I am doing a project where I have to change button colors based on sliders in WPF. This is my XAML design.
XAML file
So, I have made 64 buttons, and I have tried implementing multiple options, and none worked for me.
I have this xaml code:
<Grid Grid.Column="0" Grid.RowSpan="4" Grid.ColumnSpan="4" HorizontalAlignment="Left" Height="790" Margin="0,10,0,0" Grid.Row="0" VerticalAlignment="Top" Width="800">
<Grid Margin="230,114,230,346" AutomationProperties.Name="ledMatrix" Tag="ledMatrix" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Button Grid.Column="0" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED00" Click="changeLedIndicatiorColor" x:Name="LEDButton"/>
<Button Grid.Column="1" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED01"/>
<Button Grid.Column="2" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED02"/>
<Button Grid.Column="3" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED03"/>
<Button Grid.Column="4" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED04"/>
<Button Grid.Column="5" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED05"/>
<Button Grid.Column="6" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED06"/>
<Button Grid.Column="7" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED07"/>
and sliders:
<Grid Margin="230,480,230,170">
<Slider x:Name="SeekR" Margin="0,0,0,120" Tag="seekR" Maximum="255" Background="Red" ></Slider>
<Slider x:Name="SeekG" Margin="0,50,0,70" Tag="seekG" Maximum="255" Background="Green"></Slider>
<Slider x:Name="SeekB" Margin="0,100,0,20" Tag="seekB" Maximum="255" Background="Blue"></Slider>
</Grid>
I have tried placing this in my xaml.cs file :
public void changeColor()
{
byte rr = (byte)SeekR.Value;
byte gg = (byte)SeekG.Value;
byte bb = (byte)SeekB.Value;
Color cc = Color.FromRgb(rr, gg, bb); //Create object of Color class.
SolidColorBrush colorBrush = new SolidColorBrush(cc); //Creating object of SolidColorBruch class.
LEDButton.Background = colorBrush; //Setting background of stack panel.
}
private void changeLedIndicatiorColor(object sender, RoutedEventArgs e)
{
changeColor();
}
But from this code, I have to name all the buttons differently butI want to have the same binding for all buttons and try to connect it all together, so for example I would like to set color by sliders and when I click the button color would change. Then, I can change the color, and make other buttons that different color.
Refactor ChangeColor to accept a Button, the button to change the color for:
private void ChangeLedIndicatiorColor_OnClick(object sender, RoutedEventArgs e)
{
ChangeColor(sender as Button);
}
public void ChangeColor(Button button)
{
byte rr = (byte)SeekR.Value;
byte gg = (byte)SeekG.Value;
byte bb = (byte)SeekB.Value;
Color cc = Color.FromRgb(rr, gg, bb);
SolidColorBrush colorBrush = new SolidColorBrush(cc);
button.Background = colorBrush;
}
Then register the ChangeLedIndicatiorColor event handler for each Button.Click:
...
<Button Click="ChangeLedIndicatiorColor" />
<Button Click="ChangeLedIndicatiorColor" />
<Button Click="ChangeLedIndicatiorColor" />
...
You can use the EventSetter so that the event can be applicable to all the buttons. You don't need to attach the click event handler to each button anymore.
<Window.Resources>
<Style TargetType="Button">
<EventSetter Event="Click" Handler="ChangeLedIndicatiorColor"/>
</Style>
</Window.Resources>
And inside event handler you can change the color of the button which triggered the click event.
private void changeLedIndicatiorColor(object sender, RoutedEventArgs e)
{
byte rr = (byte)SeekR.Value;
byte gg = (byte)SeekG.Value;
byte bb = (byte)SeekB.Value;
Color cc = Color.FromRgb(rr, gg, bb);
SolidColorBrush colorBrush = new SolidColorBrush(cc);
(sender as Button).Background = colorBrush;
}
I have a rectangle textbox in WPF and there is already a button inside the textbox which is clickable. I need to make the entire rectangle textbox clickable, so users can click anywhere in the textbox rather than just the button to interact.
Is it possible to do so?
Here is my XAML:
<Grid Background="{StaticResource TinyGrayBrush}" Height="260" Width="530" Margin="10,10,10,10" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120*"/>
<ColumnDefinition Width="140*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Image x:Name="imgCard" Source="{StaticResource Exception-NoImageAvailable}" Stretch="Fill" />
<Image x:Name="hotpickEn" Source="{StaticResource HotPickEn}" Height="20" Width="70" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0" Visibility="Hidden" />
<Image x:Name="hotpickCh" Source="{StaticResource HotPickCH}" Height="20" Width="70" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0" Visibility="Hidden" />
</Grid>
<Grid x:Name="gdEnglish" Visibility="Visible" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="12*"/>
<RowDefinition Height="62*"/>
<RowDefinition Height="90*"/>
<RowDefinition Height="5*"/>
<RowDefinition Height="55*"/>
<RowDefinition Height="10*"/>
</Grid.RowDefinitions>
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="25,5,25,5"/>
</Style>
</Grid.Resources>
<TextBlock Grid.Row="1" x:Name="txtTitle" Text="SPARKLING SUNDAY BRUNCH BUFFET" FontSize="20" TextWrapping="Wrap" FontWeight="Medium" Foreground="{StaticResource SemiDarkGrayBrush}" Margin="5,0,5,6.5" />
<TextBlock Grid.Row="2" x:Name="txtDesc" Text="Pop your Sunday Morning with luxury sparkling bubble" FontSize="16" TextWrapping="Wrap" Foreground="{StaticResource GrayBrush}" Margin="5,3.5,5,6.5" />
<TextBlock Grid.Row="3" x:Name="txtPoint" Text="138,900 GP" FontSize="21" TextWrapping="Wrap" FontWeight="Medium" Foreground="{StaticResource GrayBrush}" Margin="25,3.5,25,6" />
<Button Grid.Row="4" x:Name="btnFindOutMore" Content="FIND OUT MORE OR REDEEM" Style="{StaticResource SmallWhiteRedButton}" Margin="12,0,12,0" FontSize="20" />
</Grid>
<Grid x:Name="gdChinese" Visibility="Hidden" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="12*"/>
<RowDefinition Height="62*"/>
<RowDefinition Height="90*"/>
<RowDefinition Height="5*"/>
<RowDefinition Height="55*"/>
<RowDefinition Height="10*"/>
</Grid.RowDefinitions>
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="25,5,25,5"/>
</Style>
</Grid.Resources>
<TextBlock Grid.Row="1" x:Name="txtCHTitle" Text="SPARKLING SUNDAY BRUNCH BUFFET" FontSize="20" TextWrapping="Wrap" FontWeight="Medium" Foreground="{StaticResource SemiDarkGrayBrush}" HorizontalAlignment="Left" Margin="5,4.667,0,5.333" />
<TextBlock Grid.Row="2" x:Name="txtCHDesc" Text="Pop your Sunday Morning with luxury sparkling bubble" FontSize="16" TextWrapping="Wrap" Foreground="{StaticResource GrayBrush}" HorizontalAlignment="Left" Margin="5,4.667,0,5.667" />
<TextBlock Grid.Row="3" x:Name="txtCHPoint" Text="138,900 GP" FontSize="21" TextWrapping="Wrap" FontWeight="Medium" Foreground="{StaticResource GrayBrush}" HorizontalAlignment="Left" Margin="25,3.5,25,6" Width="105" />
<Button x:Name="btnCHFindOutMore" Grid.Row="4" Content="FIND OUT MORE OR REDEEM" Style="{StaticResource SmallWhiteRedButton}" Margin="15,0,10,0"/>
</Grid>
As you are using Code-Behind for this:
You basically subscribe to the events of the Grid too
gdEnglish.TouchDown += gdEnglish_TouchDown;
gdEnglish.TouchUp += gdEnglish_TouchUp;
gdEnglish.TouchLeave += gdEnglish_TouchLeave;
gdEnglish.MouseDown += gdEnglish_MouseDown;
gdEnglish.MouseUp += gdEnglish_MouseUp;
gdEnglish.MouseLeave += gdEnglish_MouseLeave;
add a new private variable to indicate the current state
private bool gdEnglish_clickflag = false;
and create the different event handlers
private void gdEnglish_MouseLeave(object sender, MouseEventArgs e)
{
gdEnglish_clickflag = false;
}
private void gdEnglish_MouseUp(object sender, MouseButtonEventArgs e)
{
if (gdEnglish_clickflag)
{
gdEnglish_clickflag = false;
e.Handled = true;
//////////
// YOUR //
// CODE //
//////////
}
}
private void gdEnglish_MouseDown(object sender, MouseButtonEventArgs e)
{
gdEnglish_clickflag = true;
}
private void gdEnglish_TouchLeave(object sender, TouchEventArgs e)
{
gdEnglish_clickflag = false;
}
private void gdEnglish_TouchUp(object sender, TouchEventArgs e)
{
if (gdEnglish_clickflag)
{
gdEnglish_clickflag = false;
e.Handled = true;
//////////
// YOUR //
// CODE //
//////////
}
}
private void gdEnglish_TouchDown(object sender, TouchEventArgs e)
{
gdEnglish_clickflag = true;
}
I still want to recommend that you research the topic MVVM with WPF
it will make things like this more clean and easy
I am trying to implement my own lockscreen in my xamarin app. I have created the numpad as below:
<ContentPage.Content>
<Grid Padding="0,0,0,10">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="2*" />
</Grid.RowDefinitions>
<Image Grid.Row="0" Source="alpaca.png" />
<Label Grid.Row="1" Text="{Binding PincodeMasked}" FontFamily="Lato-Bold" TextColor="Black" VerticalOptions="Center" FontSize="80" HorizontalTextAlignment="Center" />
<Grid Grid.Row="2" Padding="40,0,30,0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0" Image="num1.png" Command="{Binding NumberCommand}" CommandParameter="1" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="0" Grid.Column="1" Image="num2.png" Command="{Binding NumberCommand}" CommandParameter="2" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="0" Grid.Column="2" Image="num3.png" Command="{Binding NumberCommand}" CommandParameter="3" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="1" Grid.Column="0" Image="num4.png" Command="{Binding NumberCommand}" CommandParameter="4" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="1" Grid.Column="1" Image="num5.png" Command="{Binding NumberCommand}" CommandParameter="5" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="1" Grid.Column="2" Image="num6.png" Command="{Binding NumberCommand}" CommandParameter="6" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="2" Grid.Column="0" Image="num7.png" Command="{Binding NumberCommand}" CommandParameter="7" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="2" Grid.Column="1" Image="num8.png" Command="{Binding NumberCommand}" CommandParameter="8" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="2" Grid.Column="2" Image="num9.png" Command="{Binding NumberCommand}" CommandParameter="9" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="3" Grid.Column="1" Image="num0.png" Command="{Binding NumberCommand}" CommandParameter="0" BackgroundColor="#00FFFFFF" />
</Grid>
</Grid>
</ContentPage.Content>
In the ViewModel I want to do something like this
public PinLoginPageModel()
{
NumberCommand = new Command(NumpadToNumber);
}
void NumpadToNumber(object obj)
{
if(Button.CommandParameter == 0)
{
PincodeMasked = "0";
}
}
I want to convert each button click to a corresponding value, however I am getting an error at Button.CommandParameter saying it needs to have an object reference. How can I reference this to the button in the XAML code?
Thanks
You need to change your view mode code like below:
public PinLoginPageModel()
{
NumberCommand = new Command<string>(NumpadToNumber);
}
void NumpadToNumber(string obj)
{
if(obj == "0")
{
PincodeMasked = "0";
}
}
Try this:
void NumpadToNumber(object parameter)
{
if (parameter != null && parameter.ToString() == "0")
{
PincodeMasked = "0";
}
}
Just change your view model like this:
public PinLoginPageModel()
{
NumberCommand = new Command<object>(NumpadToNumber);
}
void NumpadToNumber(object obj)
{
if((obj as int) == 0)
PincodeMasked = "0";/
}