I have page1 with a button that opens page2. Page2 takes approx 1 second to open and contains a list of options (user controls) that when tapped, saves the selection and returns the user to page1.
When the user taps the button on page1, if they become impatient, they will tap it again before page2 has loaded/displayed. the problem is, that the tap is recognised and the first option on page2 is tapped, even though it's not yet visible. This then saves the selection and returns the user to page1.
Is there anyway I can stop the tap/click event being recognised until the page2 has fully loaded?
One idea may be to subscribe to the Tap/Click event in OnNaviagatedTo on first page, then inside Tap/Click event unsubscribe it, so that user won't be able to click it again. Then when he navigates back, OnNavigatedTo subscribes again. You may also think about flag that will be set up upon Tap/Click and dismissed in Loaded event of second page. Sample:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Debug.WriteLine("OnNavigatedTo");
base.OnNavigatedTo(e);
this.button.Click += Button_Click;
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
(sender as Button).Click -= Button_Click;
await Task.Delay(1000); // simulate heavy load of second page
this.Frame.Navigate(typeof(SecondPage));
}
}
Related
In my MainWindow.xaml i have a Frame. The content of this Frame will change when i click a Button. So if I click a button to show private customers, the Frame Content shows the site of the private customers:
private void privatecustomer_Click(object sender, RoutedEventArgs e)
{
Main.Content = new Privatecustomer.privatecustomer_show();
}
After clicking this Button the Frame will change the Content.
The new Content shows Privatecustomer.privatecustomer_show.xaml now.
In this xaml i have another Button. If i click this Button, the Content of the Frame in MainWindow should change to "Privatecustomer.privatecustomer_add.xaml".
But How I can tell from privatecustomer_show.xaml to MainWindow.xaml, that the Button addPrivatecustomer in privatecustomer_show is clicked and that the Main.Content have to change to
Main.Content = new Privatecustomer.privatecustomer_add();
?
I hope I can get some help here
How about adding an event in privatecustomer_show.xaml that the MainWindow.xaml can subscribe to.
Like this
public event EventHandler AddPrivateCustomer;
protected virtual void OnAddPrivateCustomerEventArgs e)
{
if (AddPrivateCustomer!= null)
AddPrivateCustomer(this, e);
}
Update: Please note the updated code, I made a copy'n'paste mistake in my first version.
Change your:
private void privatecustomer_Click(object sender, RoutedEventArgs e)
To:
private void privatecustomer_Click(object sender, RoutedEventArgs e)
{
var privateCustomerContent=new Privatecustomer.privatecustomer_show();
privateCustomerContent.AddPrivateCustomer+=onClick_addPrivateCustomer;
Main.Content = privateCustomerContent;
}
private onClick_addPrivateCustomer(object o,EventArgs e)
{
// Change Main.Content
}
The idea is that your privatecustomer_show control sends events for when it want to change something outside of what it has access to.
You MainWindow which has full control of all its child windows will then subscribe to each event.
Set Page2.Tag property to the instance of your MainWindow (use Binding in Xaml, or simply set it in code). After the specified Button in Page2 is clickd, simply use the Tag property (which is now your MainWindow).
Another option is to use
App.Current.MainWindow
When the Button is clicked. (A warning. I don't know the structure your project and this might not be the instance you are looking for).
When I use a page animation to navigate from one page to another, I navigate fine to the second page but I can't navigate back to the main page (using the hardware back button) .When I try to navigate back I find all controls on the page gone.
Code to go the second page
private void Button1_Click(object sender, RoutedEventArgs e)
{
//Run animation then navigate to second page
myAnimation.Begin();
myAnimation.Completed += (s,ev)=>
{
NavigationService.Navigate(new Uri("/nextPage.xaml?id=Button1",UriKind.Relative));
};
}
Code on second page
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e )
{
base.OnNavigatedTo(e);
myAnimation.Begin(); //Another Animation
}
My guess is that your storyboard moves the controls outside of the view. When the user presses the back button, the previous page is restored in the exact state you left it. So the controls are still be hidden from the view.
To solve the issue, simply reset the storyboard when the user navigates to the page:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
myAnimation.Stop();
}
I have created a custom XAML UserControl class that I pass to the ShowDialog method. I would like to be able to trigger clicking the OK button on the dialog through other events generated in my UserControl - for example double clicking a ListItem. I have the code to handle the double click just fine (tied into the MouseDown even and checked the click count) but I don't know how to trigger a new event to the parent dialog to close it.
// Bound to TextBlock, part of a ListBox on a UserControl
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
// trigger dialog to close with OK button
}
}
Make your user control implement IDialogContent. You can then call directly to the CloseDialog event you implemented, and that will trigger the dialog closure.
You will need to decide how you want to handle that in your follow up code by setting some sort of state on your user control/view model or some other data as fits your particular extension.
// Bound to TextBlock, part of a ListBox on a UserControl
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
CloseDialog(this, EventArgs.Empty);
}
}
I have a Main page that contains a listBox.
When a user selects a profile form the list box, this opens up a child window called pWindow.
This window as the option to delete the current profile via a hyperlink button that opens up a another confirmation window called dprofile.
My question being is it possible that once a user has confirmed to delete the current profile they are in, and confirmed it in the button click on dProfile, how can I update the listBox in the first Main page so that the list no longer contains the deleted profile (which it is not doing at present.
In the dProfile window I have created an event -
public event EventHandler SubmitClicked;
Where in the OK button click I have-
private void OKButton_Click(object sender, RoutedEventArgs e)
{
if (SubmitClicked != null)
{
SubmitClicked(this, new EventArgs());
}
}
So on the Main page I have added-
private void deleteProfile_SubmitClicked(object sender, EventArgs e)
{
WebService.Service1SoapClient client = new WebService.Service1SoapClient();
listBox1.Items.Clear();
client.profileListCompleted += new EventHandler<profileListCompletedEventArgs>(client_profileListCompleted);
client.profileListAsync(ID);
}
I thought this may have updated the listBox as it was confirmed in the dProfile form however when the form closes, the listBox stays the same and I have to manually refresh the webpage to see the update. How can I do this?
If I understood it correctly then you have three pages. Main, pWindow and dProfile. Earlier you were trying to close pWindwow from dProfile and that was working properly. Now you want to refresh the listBox1 on Main Page.
To achieve that you may follow a similar strategy. You are probably opening pWindow from Main page with something on the following line
pWindow pWin = new pWindow();
pWin.Show();
Now you may define a new event in pWindow class.
public event EventHandler pWindowRefeshListBox;
Then in your event handler for deleteProfile_SubmitClicked you may raise the event to refresh listbox1, something on the following line:
private void deleteProfile_SubmitClicked(object sender, EventArgs e)
{
if(pWindowRefreshListBox != null)
pWindowRefreshListBox(this, new EventArgs());
this.Close();
}
Then in your main page register the event against pWin object, which you defined earlier.
pWin.pWindowRefreshListBox += new new EventHandler(pWindow_pWindowRefreshListBox);
Then define the event in Main page.
private void pWindow_pWindowRefreshListBox(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
This should refresh the listbox. I haven't test the code or the syntax. So you may check it
before implementing.
EDIT
you may define the event in dProfile as static
public static event EventHandler SubmitClicked;
Then you will be able to register it in Main and pWindow against Class Name
dProfile.SubmitClicked += new ..............
Then implement it accordingly, in pWindow, close the window and in main refresh listbox
EDIT:
You may create instance of deleteProfile on the main page register the following in your main
deleteProfile.SubmitClicked += new EventHandler(deleteProfile _SubmitClicked)
this should work
i have a aspx page on it i'm dynamically adding the web user controls in a placeholder. and know i have cancel button on the one user control and on click of that Cancel button i want to load other user control inside the aspx page (placeholder).
how can i do that, if i create an event handler in usercontrol then that become null.
thanks
A user control should never, ever have any kind of dependency to the page where it lives. Add an event to the control and fire it from the Cancel button event handler.
public event EventHandler CancelClicked;
protected Cancel_Click(object sender, EventArgs e)
{
if(CancelClicked != null)
{
CancelClicked(this, e);
}
}
From your page, subscribe to the controls's CancelClicked event and do whatever operation should be done on that other user control.
I believe the best way to achieve this would to create an event in the user control that is fired when you need it to communicate, then in the page you can subscribe to this event.
Your User Control
Put this in your control as an event variable.
public event EventHandler CancelRequested;
Then in the cancel button click event :
CancelRequested(this, new CommandEventArgs("CancelClicked", SomeVariable));
Your Page (or parent)
Then subscribe to the event in the parent page like this (when you dynamically add it in):
ctrlYourControl.CancelRequested += new EventHandler(ctrlYourControl_CancelRequested);
Declare the event handler :
void ctrlYourControl_CancelRequested(object sender, EventArgs e)
{
// display other user control
}
Add this event to your user controls:
public event EventHandler CancelClicked;
Now, this method on your parent aspx page.
public void OnCancelClicked (object sender, EventArgs data)
{
// do your stuffs like loading other controls and
// whatever when event fired
}
When you are loading your user control dynamically, add this:
var someControl = LoadControl(#"~\SomeControl.ascx") as SomeControl;
someControl.CancelClicked += new EventHandler(OnCancelClicked);
And on your user controls:
public void btnCancel_Click(object sender, EventArgs e)
{
if(CancelClicked != null)
{
CancelClicked(this, e);
}
}
This should do !