I've got the code, and all seems correct, I've had it reviewed and it seems impossible to find out why the button isn't doing what its coded to do. I'm making a Music player, and when I press the play button, it will be sent to the back and the pause button will become visible, when I next click the pause button, nothing happens and its primary function stops working all together. Here is the code for people to examine.
private void btnPlay_Click(object sender, EventArgs e)
{
try
{
if (_mp3Player != null)
_mp3Player.Play();
btnPlay.SendToBack();
btnPause.BringToFront();
}
catch (Win32Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnPause_Click(object sender, EventArgs e)
{
if (_mp3Player != null)
_mp3Player.Stop();
btnPause.SendToBack();
btnPlay.BringToFront();
}
Perhaps it would be best to use visibility?
private void btnPause_Click(object sender, EventArgs e)
{
if (_mp3Player != null)
{
_mp3Player.Stop();
}
btnPause.Visible = False;
btnPlay.Visible = True;
}
Or even the enabled property?
... btnPause.Enabled = false; ...
However I feel you could make it better by having it be the same button, with a value, so just have a value on it of true if it's player or false if it's pause and then in the click event check against that value to determine what it is currently and then just execute the relevant functionality and change the text or image that you have on the button.
Related
I know the solution on Android
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
// do something here
return true;
}
return super.onKeyDown(keyCode, event); }
But i cant do it on the forms side. The focus is an Entry so the keyboard is visible and when i press hardware back button (keyboard disappear) i'd like to set my variable value.
Edited:
bool _tapped;
LwTeszt.PropertyChanged += (s, e) =>
{
if (LwTeszt.SelectedItem != null)
{
EntryTeszt.Focus();
}
};
EntryTeszt.Unfocused += EntryTeszt_Unfocus;
private void EntryTeszt_Unfocus(object sender, FocusEventArgs e)
{
_tapped = true;
}
private void ViewCell_Tapped(object sender, EventArgs e)
{
if (_tapped)
{
EntryTeszt.Unfocus();
_tapped = false;
return;
}
EntryTeszt.Focus();
}
Unless you have additional logic that you didn't post I think you might be overcomplicating things. I believe all you need to do is focus your entry on ViewCell_Tapped
private void ViewCell_Tapped(object sender, EventArgs e)
{
EntryTeszt.Focus();
}
Forms will handle unfocusing your Entry for you when back is pressed and in that case you won't need the LwTeszt.PropertyChanged, _tapped, and the EntryTeszt.Unfocused parts.
There is an Event you have to just override that and you can handle back button in Xamarin forms. Use below code for handling back button:
protected override bool OnBackButtonPressed()
{
// your code
return base.OnBackButtonPressed();
}
Also you can refer this question for more information.
I want to check if the Clipboard consists of a data and if not, let the "Paste" Button be enabled. But unfortunately, even after I clear the Clipboard it still doesn't show it's null. I am working with Windows Forms.
I manually clear the clipboard:
private void button2_Click(object sender, EventArgs e)
{
Clipboard.Clear();
}
and then I add the following code to the Form LoadEvent:
if (Clipboard.GetDataObject() != null)
{
this.pn1_BtnPaste.Enabled = true;
}
And it makes a button enabled which is weird to me. Can anybody explain why is that happening?
EDIT:
Because I got understood wrong, let me change the code to make it more clear:
private void button2_Click(object sender, EventArgs e)
{
Clipboard.Clear();
if (Clipboard.GetDataObject() != null)
{
this.pn1_BtnPaste.Enabled = true;
}
else
this.pn1_BtnPaste.Enabled = false;
}
I click the "button2" and the "pn1_BtnPaste" is enabled anyway.
Data can appear on the clipboard at any time. The Application.Idle event is a decent way to update the button state:
public Form1() {
InitializeComponent();
Application.Idle += Application_Idle;
}
You have to unsubscribe it again when the window closes to be on the safe side:
protected override void OnFormClosed(FormClosedEventArgs e) {
Application.Idle -= Application_Idle;
base.OnFormClosed(e);
}
Clipboard.GetDataObject() does not work the way you think it does, it never returns null. If you want to handle any data then you can write the event handler like this:
private void Application_Idle(object sender, EventArgs e) {
PasteButton.Enabled = Clipboard.GetDataObject().GetFormats().Length > 0;
}
But it is pretty likely you'll find out that handling every possible format is lot let practical than you assumed.
SoundPlayer StartUpMusic = new SoundPlayer(Resources.Guiles_Theme);
private void MuteButton_Click(object sender, EventArgs e)
{
if (StartUpMusic.IsLoadCompleted == true)
{
StartUpMusic.Stop();
StartUpMusic.Dispose();
}
else
{
StartUpMusic.Load();
StartUpMusic.Play();
}
}
This is an event triggered when the user clicks the play button. I think my condition within the if statement is not good. I basically want the sound to be muted when the button is pressed. Then I want the sound to continue when the button is pressed and the sound is already muted. What is wrong here? You time and effort are greatly appreciated. Thank you!
Just use a boolean flag to indicate if the sound is playing or not. So something like this might work:
private boolean isPlaying = true;
private void MuteButton_Click(object sender, EventArgs e)
{
if (StartUpMusic.IsLoadCompleted)
{
if (isPlaying)
StartUpMusic.Stop();
else
StartUpMusic.Play();
isPlaying = !isPlaying;
}
}
I am making a application where i would like to log in with a face detection. But this is not real, its just to make ik look like its scanning.
So when i press the LOG IN button, the kinect takes my picture and show me the picture, on top of it is showing me in a text that its scanning.
Now i am stuck with the following issue, when i press the login button, the scanning label appears, but i would like to fire an other event that takes me to the next page, the homepage.
So i want the SCANNING label appearing for 3 seconds, and then the page should change.
This is what i tried, i worked with a timer, but that doesnt do annything.
private void ActionButton_Click(object sender, System.EventArgs eventArgs)
{
_main.TakePicture();
identifyBox.Source = _main.source.Clone();
scanningLabel.Visibility = Visibility.Visible;
_storyboard = (Storyboard)FindResource("scanningSB");
//_storyboard.Begin();
Start();
}
private void Start()
{
_tm = new Timer(3000);
_tm.Elapsed += new ElapsedEventHandler(_tm_Elapsed);
_tm.Enabled = true;
}
void _tm_Elapsed(object sender, ElapsedEventArgs e)
{
if (_tm == new Timer(3000))
{
((Timer)sender).Enabled = false;
_main.ContentPage.Children.Clear();
_main.ContentPage.Children.Add(_homeScreen);
}
}
Okay i removed the if statement, but now it fires every 3 seconds a method.
How can i make it work 1 time.
Ok even this works, now i my ContentPage wont change? It gives me this error: The calling thread cannot access this object because a different thread owns it.
What can be wrong?
I think you can remove condition
if (_tm == new Timer(3000))
and keep it simple
void _tm_Elapsed(object sender, ElapsedEventArgs e)
{
((Timer)sender).Enabled = false;
_main.ContentPage.Children.Clear();
_main.ContentPage.Children.Add(_homeScreen);
}
when you set _tm = new Timer(3000); it will set the time to fire event after 3 seconds..
Change the _tm_Elapse to this:
void _tm_Elapsed(object sender, ElapsedEventArgs e)
{
if (_tm == (sender as Timer))
{
_main.ContentPage.Children.Clear();
_main.ContentPage.Children.Add(_homeScreen);
}
}
Edit for answering:
"I just want it 1 time to fire after 3 sec"
void _tm_Elapsed(object sender, ElapsedEventArgs e)
{
if (_tm == (sender as Timer))
{
_tm.Stop();
_main.ContentPage.Children.Clear();
_main.ContentPage.Children.Add(_homeScreen);
}
}
This is even simpler, and it will only run once per call, the three second timer is built in, and furthermore, it won't disable other program functionalities while it is running:
async Task Start()
{
await Task.Delay(3000);
_main.ContentPage.Children.Clear();
_main.ContentPage.Children.Add(_homeScreen);
}
I have a WebBrowser element in a page, to which I would like to add a back and forward buttons, and have those buttons disabled when there's nothing to go back to and nothing to go forward to.
In Cocoa, the UIWebView has methods to easily check that: canGoBack and canGoForward, and you have goBack and goForward methods available (along with reload etc..)
Android has the exact same method names for achieving the same.
I see those methods are available in .Net 4 and 3.5 SP1.
I've found some references about using javascript commands in Silverlight but I find this very cumbersome, plus there's no way to detect if there's anything in the history (unless of course I manage this myself)
Surely, there's something a tad more advanced in Windows Phone ..
Here is how I ended up doing it.
This assumes you have set a backButton and forwardButton; the status of these buttons will be updated accordingly depending on where you are in the navigation stack.
webView is the WebBrowser object
List<Uri> HistoryStack;
int HistoryStack_Index;
bool fromHistory;
// Constructor
public HelpView()
{
InitializeComponent();
HistoryStack = new List<Uri>();
HistoryStack_Index = 0;
fromHistory = false;
webView.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(WebView_Navigated);
UpdateNavButtons();
}
private void backButton_Click(object sender, RoutedEventArgs e)
{
if (HistoryStack_Index > 1)
{
HistoryStack_Index--;
fromHistory = true;
webView.Navigate(HistoryStack[HistoryStack_Index-1]);
updateNavButtons();
}
}
private void forwardButton_Click(object sender, RoutedEventArgs e)
{
if (HistoryStack_Index < HistoryStack.Count)
{
HistoryStack_Index++;
fromHistory = true;
webView.Navigate(HistoryStack[HistoryStack_Index-1]);
UpdateNavButtons();
}
}
private void UpdateNavButtons()
{
this.backButton.IsEnabled = HistoryStack_Index > 1;
this.forwardButton.IsEnabled = HistoryStack_Index < HistoryStack.Count;
}
private void WebView_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
if (!fromHistory)
{
if (HistoryStack_Index < HistoryStack.Count)
{
HistoryStack.RemoveRange(HistoryStack_Index, HistoryStack.Count - HistoryStack_Index);
}
HistoryStack.Add(e.Uri);
HistoryStack_Index++;
UpdateNavButtons();
}
fromHistory = false;
}
I have a back button added to the applicationbar of a page in one of my apps which contains a webbrowser. I wanted the back button in the app bar to take the web page navigation backward, and wanted the hardware back button to go to the previous xaml page. This way, the user doesn't have to use the hardware back button to navigate backward through all the visited web pages in the webbrowser in order to go back to the prior xaml page. Here is how I did it, and you could easily set up a forward stack and when the user clicks the back (appbar) button, the page pops from that stack and is pushed to the forward stack.
private void NavigateWeb()
{
if (!loaded)
{
NavigationStack.Clear();
try
{
Web.Source = new Uri("http://m.weightwatchers.com/");
loaded = true;
}
catch (Exception ex)
{
MessageBox.Show("Unable to navigate to page.\n" + ex.Message,
"Error", MessageBoxButton.OK);
}
}
}
void Web_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
NavigationStack.Push(e.Uri);
}
void btnBack_Click(object sender, EventArgs e)
{
if (NavigationStack.Count > 2)
{
// get rid of the topmost item...
NavigationStack.Pop();
// now navigate to the next topmost item
// note that this is another Pop - as when the navigate occurs a Push() will happen
Web.Navigate(NavigationStack.Pop());
}
}
The reason I check for NavigationStack.Count > 2 is that the particular webpage that I'm showing in the webbrowser always starts with a "click here to continue" link on the first page, and there is no reason to go back to there. That's the downfall of showing other people's sites in your webbrowser - you don't have control over what is shown.
In regards to the javascript solution it is doing something like this:
private void backButton_Click(object sender, RoutedEventArgs e)
{
try
{
webView.InvokeScript("eval", "history.go(-1);");
}
catch
{
// Eat error
}
}
private void forwardButton_Click(object sender, RoutedEventArgs e)
{
try
{
webView.InvokeScript("eval", "history.go(1);");
}
catch
{
// Eat error
}
}
with having the IsScriptingEnabled set to true for the WebBrowser element.
However, this always generates an exception with error 80020006. I read various posts about how the DOCTYPE could have been the culprit, the system caching or IsScriptEnabled being set after the content was loaded... It just never worked...