Xamarin - ActivityIndicator takes time to appear - xamarin

I have a button with an
await this.Navigation.PushModalAsync
The command runs on click.
I want to show the ActivityIndicator on a press, this is my button clicked command:
async void ButtonClicked(object sender, EventArgs e)
{
actCentros.IsRunning = true;
await this.Navigation.PushModalAsync(...);
actCentros.IsRunning = false;
}
This takes about 1 second to appear. Without the PushModalAsync it appears on click.
Why does it happen?

What I have found to work is to bind the IsRunning property of the Activity Indicator to a Boolean INotifyPropertyChanged based property on your ViewModel, such as 'IsProcessing', and setting this to true at the start of the Command's Execute delegate method, and back to false at the end of the method. I do not notice any delay in this approach from clicking the button bound to the Command.

Related

WindowsForms TopMost versus ComboBox

I have a WindowsForms application in which I open a Form as a Dialog (Form2.ShowDialog) and in this Form I have a Timer that sets the TopMost property of the Form to true.
But I also have a ComboBox in this Form and when I click on the ComboBox to select an Item, the list opens and closes immediately as the Timer sets the TopMost property back to true.
If you ask me This is wrong way and should be replace your "load data function" from timer to form_load event. so if you want current way you should disable timer in ComboBox Enter Event and enable timer in ComboBox Leave Event.
private void comboBox1_Enter(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void comboBox1_Leave(object sender, EventArgs e)
{
timer1.Enabled = true;
}

Xaml Button is "clicked" if it's pressed-and-hold on a touch screen

I am developing a Windows UWP App with C++. I want to add a button that can either be left-clicked or right-clicked and do corresponding task.
The button works perfectly on normal devices with mouse. Unfortunately on touch screen, if users press-and-hold the button, both left-click and right-click events are triggered.
I am using "Click" to handle the left click event and "RightTapped" to handle the right click event. Any suggestion on how to disable the left-click event when users press-and-hold the button? Thank you!
The XAML code is short:
<Button x:Name="m_NewPageButton"
Content="New Page"
Height="48"
Width="199"
Click="OnClick"
RightTapped="OnRightClick"/>
And here's the cpp code:
void MainPage::OnClick()
{
// Left click task
}
void MainPage::OnRightClick()
{
// Right click task
}
Edit:
Thanks guys, the solution is to change the left click handler to , which will not be triggered by press-and-hold.
Hacky but works...
private bool rightClicked;
private async void Button_Click(object sender, RoutedEventArgs e)
{
if (rightClicked)
return;
await new MessageDialog("left").ShowAsync();
}
private async void Button_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
rightClicked = true;
await new MessageDialog("right").ShowAsync();
rightClicked = false;
}
I'm guessing this is a flaw in the design.. :(
Actually... If you use the button's Tapped and RightTapped events it works as it should. So in this case just replace Clicked with Tapped and all is well... Then you can ignore the hack I put above.
You can use the Holding event to detect a tap and hold. Use the Tapped event for a single tap (or click). The Tapped event should only fire for touch on a quick tap and release. The mouse specific click will raise Tapped but a touch tap-and-hold will not.

Close a form when FormClosing cancels the event

I am building an application that minimizes to tray when the user clicks the close button (the cross on the upper right corner) using this code:
private void FrmTest_FormClosing(object sender, FormClosingEventArgs e) {
e.Cancel = true;
WindowState = FormWindowState.Minimized;
}
however, if I want to actually close the window on a button using Close() this handler is called and the form doesn't close.
How can I close my form / application now?
I fixed it (thought I checked this, but apparently, something went amiss while debugging).
You can use the CloseReason property to find out why the event is called. When the user clicks on the close button on the window, e.CloseReason is UserClosing, otherwise it is ApplicationExitCall:
private void FrmTest_FormClosing(object sender, FormClosingEventArgs e) {
if (e.CloseReason == CloseReason.UserClosing) {
e.Cancel = true;
WindowState = FormWindowState.Minimized;
}
}
Update when using Close() to close the form, the CloseReason will be the same and you should use a boolean as Hans Passant mentions in the comments. When using Application.Exit(), this CloseReason solution works.

List item unresponsive on second click event

In my Windows Phone 7 application I have a number of list views. Each listViewItem has a click event (By setting 'selectionChanged' attribute to the listBox in my xaml). Now a very peculiar thing happens:
When I click on an item in the listbox the first time everything goes well, in this case the user gets taken to another screen. When I go back from that screen to the listbox, I select the very same listboxitem but this time the event doesn't register, nothing happens...
I then first have to tap on another item, let that ones even fire, then only can I tap on the first item. So in other words, I can't fire a click event for a listItem twice in a row. I'm thinking it's because the event handler on the listbox says 'onSelectionChanged', if you select the same item the selection hasn't technically changed.
So what other eventHandling attribute can I use on my listbox to register selection events on it's items?
Thanks for any help!
AFAIK, Theres is no such event. So, the work around is,
In the OnNavigatedTo event handler of the first page, set the SelectedIndex to -1
YourListBox.SelectedIndex = -1;
And while doing so, make one small modification to your Selection_Changed handler
void Selection_Changed(...)
{
if(YourListBox.SelectedIndex == -1)
return;
//rest of your code
}
In your case, SelectedItem in the ListBox is set for the first time. The second time you tap on the same item, technically its not a SelectionChanged event, hence its not firing.
Clearing the SelectedItem at the end of SelectionChanged event would do the trick.
Below is a code snippet that could be helpful,
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//
// do your stuff here
//
//reset the selection of the sender (ListBox)
(sender as ListBox).SelectedItem = null;
}

Windows Phone 7 Selection_Changed automatically

currently I'm developing an app for WP7 but came across a little problem with a Listbox event call Selection_Change. The problem is that when i return to the page that contains the listbox the selection_change event triggers without being changed at all or without any user input. The listbox code is similar to this:
private void lsbHistory_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int index = lsbHistory.SelectedIndex;
NavigationService.Navigate(new Uri("/Views/NextPage, UriKind.Relative));
}
On the page I navigate to, the only way out of the navigated page is by pressing back button or start button meaning that it will return to the page that contains the listbox. When I Navigate back the selection change triggers leading me sometimes to a exception. Has anyone been through this before?
Consider always checking if it's -1 (the default value).
private void lsbHistory_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int index = lsbHistory.SelectedIndex;
if (index != -1)
{
NavigationService.Navigate(new Uri("/Views/NextPage, UriKind.Relative));
lsbHistory.SelectedIndex = -1; // Set it to -1, to enable re-selection.
}
}
Also, you should consider wrapping the Navigate call in Dispatcher.BeginInvoke to have a better, more smooth, page transition.
The event will be fired when the list is populated.
The simplest solution for you will probably be to add a check that there is nothing selected before triggering your navigation:
if (lsbHistory.SelectedIndex > -1)
{
// do navigation
}
One thing to notice is that when you navigate back to the page which containt the ListBox, the ListBox still has the SelectedItem set to the value it had when the user navigated away. This means that lsbHistory.SelectedIndex will get the index of the item which was selected when the user navigated forward.
Maybe there's something in your code which presumes that the ListBox's SelectedItem is null when the user navigates to the page?

Resources