How to change Background image of button on mouse click, created "Dynamically" ? - Windows Phone - windows-phone-8

I've created buttons dynamically, now unable to handle events on it, there is no usefull link on internet..
button.MouseEnter += new EventHandler(button_MouseEnter);
button.MouseLeave += new EventHandler(button_MouseLeave);
...
void button_MouseLeave(object sender, EventArgs e)
{
}
void button_MouseEnter(object sender, EventArgs e)
{
}
This code is not working ...

MouseEnter and MouseLeave are respectively triggered when the mouse pointer hovers on and out of an object. The event you're searching for is Click.
Other than that, your code should work:
button.Click += new RoutedEventHandler(button_Click);
void button_Click(object sender, RoutedEventArgs e)
{
// Whatever
}

Try the MouseLeftButtonDown and MouseLeftButtonUp events. Change the image to the new one in MouseLeftButtonDown and change it back with MouseLeftButtonUp.

So simple, just put this code as it is.
button.MouseEnter += new EventHandler(button_MouseEnter);
button.MouseLeave += new EventHandler(button_MouseLeave);
void button_MouseLeave(object sender, EventArgs e)
{
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("/Images/camBlue.png", UriKind.Relative));
Button1.Background = brush;
}
void button_MouseEnter(object sender, EventArgs e)
{
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("/Images/camRed.png", UriKind.Relative));
Button1.Background = brush;
}

Related

MediaElement Windows phone 8.1

I have a weird bug here and i don't know how to call this,but here's the thing.. i have my MediaElement in my XAML ->> <MediaElement Height="10" Width="10" x:Name="Nomes"/> and i have a Button to call that Element which is mp3 audio, and works fine C# ->>
private async void AMN(object sender, RoutedEventArgs e)
{
Nomes.Source = new Uri("ms-appx:///Sounds/AMN.mp3", UriKind.RelativeOrAbsolute);
Nomes.Play();
await Task.Delay(TimeSpan.FromSeconds(1));
VibrationDevice vb = VibrationDevice.GetDefault();
vb.Vibrate(TimeSpan.FromMilliseconds(100));
await Task.Delay(TimeSpan.FromSeconds(1));
Frame.Navigate(typeof(AmericaDoNorte));
}
Here is my SecondPage Override Method
protected override void OnNavigatedTo(NavigationEventArgs e)
{
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if(rootFrame == null)
{
return;
}
if (rootFrame.CanGoBack)
{
rootFrame.GoBack();
e.Handled = true;
}
}
And when the Vibrate Method is call i navigate through a new page,and works fine,but when i comeback to that page, the audio which supposed be to play when i hit the button play by yourself without i click, how this is possible? Thanks!
I guess your MediaElement property AutoPlay is enabled. Go to properties and have a look.
Hope this helps.
Thanks!

datepicker in popup directly open when I click a button?

As described i input a datepicker in my xaml file
when i run the page ,datepicker just show like this:
then I have to tap the datepicker to enter the select page like this :
Now
I need to directly open the fullscreen datepicker select page when I click a button
the address give a way that I can just Navigate to the select page,
but I don't know how ?
I'm the poster.
i find a solution myself
Override DatePicker class with our custom DatePickerCustom class. Create new class "DatePickerCustom.cs"
public class DatePickerCustom : DatePicker
{
public void ClickTemplateButton()
{
Button btn = (GetTemplateChild("DateTimeButton") as Button);
ButtonAutomationPeer peer = new ButtonAutomationPeer(btn);
IInvokeProvider provider = (peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider);
provider.Invoke();
}
}
then in the mainpage.xaml.cs
private DatePickerCustom datePicker;
// Constructor
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
// create datePicker programmatically
if (this.datePicker == null)
{
this.datePicker = new DatePickerCustom();
this.datePicker.IsTabStop = false;
this.datePicker.MaxHeight = 0;
this.datePicker.ValueChanged += new EventHandler<DateTimeValueChangedEventArgs>(datePicker_ValueChanged);
LayoutRoot.Children.Add(this.datePicker);
}
}
void datePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
{
// now we may use got value from datePicker
TextBlock1.Text = this.datePicker.ValueString;
}
so that when do an action like tap or click, the fullscreen datepicker page will be shown
private void button1_Click(object sender, RoutedEventArgs e)
{
this.datePicker.ClickTemplateButton();
}
ps: timepicker can also do the same thing
ps2:here is the details
#Mario Galván
hope it help u

Issue with Sound Recording in windowsphone8

My code works properly till page active. But after going back to another page when i again navigate on recording page it doesn't work.
here is sample code with page added.
public partial class MainPage : PhoneApplicationPage
{
private Microphone microphone = Microphone.Default;
private byte[] buffer;
private MemoryStream stream = new MemoryStream();
private SoundEffectInstance soundInstance;
private bool soundIsPlaying = false;
// Status images
private BitmapImage blankImage;
private BitmapImage microphoneImage;
private BitmapImage speakerImage;
public MainPage()
{
InitializeComponent();
// Timer to simulate the XNA Framework game loop (Microphone is
// from the XNA Framework). We also use this timer to monitor the
// state of audio playback so we can update the UI appropriately.
DispatcherTimer dt = new DispatcherTimer();
dt.Interval = TimeSpan.FromMilliseconds(33);
dt.Tick += new EventHandler(dt_Tick);
dt.Start();
microphone.BufferReady += new EventHandler<EventArgs>(microphone_BufferReady);
blankImage = new BitmapImage(new Uri("Images/blank.png", UriKind.RelativeOrAbsolute));
microphoneImage = new BitmapImage(new Uri("Images/microphone.png", UriKind.RelativeOrAbsolute));
speakerImage = new BitmapImage(new Uri("Images/speaker.png", UriKind.RelativeOrAbsolute));
}
void dt_Tick(object sender, EventArgs e)
{
try { FrameworkDispatcher.Update(); }
catch { }
if (true == soundIsPlaying)
{
if (soundInstance.State != SoundState.Playing)
{
// Audio has finished playing
soundIsPlaying = false;
// Update the UI to reflect that the
// sound has stopped playing
SetButtonStates(true, true, false);
UserHelp.Text = "press play\nor record";
StatusImage.Source = blankImage;
}
}
}
private void recordButton_Click(object sender, EventArgs e)
{
microphone.BufferDuration = TimeSpan.FromMilliseconds(500);
buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];
stream.SetLength(0);
microphone.Start();
SetButtonStates(false, false, true);
UserHelp.Text = "record";
StatusImage.Source = microphoneImage;
}
private void stopButton_Click(object sender, EventArgs e)
{
if (microphone.State == MicrophoneState.Started)
{
// In RECORD mode, user clicked the
// stop button to end recording
microphone.Stop();
}
else if (soundInstance.State == SoundState.Playing)
{
// In PLAY mode, user clicked the
// stop button to end playing back
soundInstance.Stop();
}
SetButtonStates(true, true, false);
UserHelp.Text = "ready";
StatusImage.Source = blankImage;
}
private void playButton_Click(object sender, EventArgs e)
{
if (stream.Length > 0)
{
// Update the UI to reflect that
// sound is playing
SetButtonStates(false, false, true);
UserHelp.Text = "play";
StatusImage.Source = speakerImage;
// Play the audio in a new thread so the UI can update.
Thread soundThread = new Thread(new ThreadStart(playSound));
soundThread.Start();
}
}
private void playSound()
{
// Play audio using SoundEffectInstance so we can monitor it's State
// and update the UI in the dt_Tick handler when it is done playing.
SoundEffect sound = new SoundEffect(stream.ToArray(), microphone.SampleRate, AudioChannels.Mono);
soundInstance = sound.CreateInstance();
soundIsPlaying = true;
soundInstance.Play();
}
private void SetButtonStates(bool recordEnabled, bool playEnabled, bool stopEnabled)
{
(ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = recordEnabled;
(ApplicationBar.Buttons[1] as ApplicationBarIconButton).IsEnabled = playEnabled;
(ApplicationBar.Buttons[2] as ApplicationBarIconButton).IsEnabled = stopEnabled;
}
}
Another page
public partial class Page1 : PhoneApplicationPage
{
public Page1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
}
}
Please if possible give some solution to it because my application is dependent on audio
recording..
Thank You very much....
microphone.BufferReady -= new EventHandler<EventArgs>(microphone_BufferReady);
write this code into OnNavigateFrom even.

how to use both hold and click events on the same button in windows phone 8

void b_Hold(object sender, System.Windows.Input.GestureEventArgs e)
{
MessageBoxResult result = MessageBox.Show("A", "MessageBox Example", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
MessageBox.Show(" ....");
}
}
void b_Click(object sender, RoutedEventArgs e)
{
Button btnevent = (Button)sender;
for (int i = 0; i < (Application.Current as App).devices.Length; i++)
if (btnevent.Content.ToString() == (Application.Current as App).devices[i])
{
(Application.Current as App).selectedDevice = i;
NavigationService.Navigate(new Uri("/pages/Buttons.xaml", UriKind.Relative));
}
}
I am working on project and I need to define hold and click events to one button. the problem is click event always fired even i hold on or click the button .
in other words ,, can i disable click event when I hold on the buttons ?
Use tap instead of click in this style
private void Button_OnHold(object sender, GestureEventArgs e)
{
e.Handled = true;
// Your code goes here
}
private void Button_OnTap(object sender, GestureEventArgs e)
{
// Your code goes here
}
After you set Handled property to true click event will not handle your gesture.

Click a button on a website using C#

I'm developing an windowsphone 8 application.
My goal is to click on a certain button on a website from my code behind (C#).
Is there anyway to achieve this?
Example:
I have a button named 'MyButton" on Mainpage. Then in the event handler:
private void BtnMyButton_Click(object sender, EventArgs e)
{
//click button 'Search' on Google
}
You could register a Javascript function to call the click event.
In codebehind:
protected void BtnMyButton_Click(object sender, EventArgs e)
{
string script = "<script type=\"text/javascript\"> document.getElementById('yourButtonID').onclick(); </script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "searchClick", script);
}
Or if you're using jQuery:
protected void BtnMyButton_Click(object sender, EventArgs e)
{
string script = "<script type=\"text/javascript\"> $('yourButtonID').Click(); </script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "searchClick", script);
}