I use TimeSpanPicker in my app. When I pick my time from the timer, it will go back to the initial page automatically (lets say setTime.xaml page). However, I want to change the navigation to another page rather than the setTime.xaml page.
enter code here
>> setTime.Xaml.cs Page
TimeSpanPicker tsp = new TimeSpanPicker();
tsp.ValueChanged += change_Value();
>> Event Handler
private void change_Value(object sender, RoutedPropertyChanedEventArgs <TimeSPan> e)
{
//do something
}
How can I change its navigation to another page?
Add a event for ValueChanged. Write the event handler like this:
private void TimePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
Related
Typically, one pops the current page using this from the NavigationStack:
Navigation.PopAsync( true );
How to I use Navigation to redraw the page just before the current page?
Background: The current page changed something that need to get re-presented in the one but last page.
I'm assuming that the data model that you are using is not observable/bindable and thus the page is not "auto-updated"...
You could use MessagingCenter to publish a "Refresh Event" to avoid coupling the two Pages with events...
In your MainPage:
MessagingCenter.Subscribe<MainPage> (this, "RefreshMainPage", (sender) => {
// Call your main page refresh method
});
In your Second Page:
MessagingCenter.Send<MainPage> (this, "RefreshMainPage");
Navigation.PopAsync( true );
https://developer.xamarin.com/guides/xamarin-forms/messaging-center/
As #SushiHangover mentioned, MessagingCenter is a good option.
Another way would be to subscribe to Page2's OnDisappearing() event from page1 and do something to the Page1 UI/data like so:
Edit: The old way I answered this question (see changelog) does work but I have since modified how I do it, after seeing others' examples, to prevent memory leaks. It is better to unsubscribe from the Disappearing event after it has been used. If you plan to use it again then you can just resubscribe to it before running PushAsync() again on your Page2 instance:
private async void OnGoToPage2Clicked(object sender, EventArgs args) {
Page2 page2 = new Page2();
page2.Disappearing += OnPage2Disappearing;
await Navigation.PushAsync(page2);
}
private async void OnPage2Disappearing(object sender, EventArgs eventArgs) {
await _viewModel.RefreshPage1Data(); //Or how ever you need to refresh the data
((Page2)sender).Disappearing -= OnPage2Disappearing; //Unsubscribe from the event to allow the GC to collect the page and prevent memory leaks
}
Here is what I had to do to get hvaughan3's solution to work for me:
private async void OnGoToPage2Clicked(object sender, EventArgs args) {
Page2 page2 = new Page2();
page2.Disappearing += Page2_Disappearing;
await Navigation.PushAsync(page2);
}
private void Page2_Disappearing(object sender, EventArgs e) {
this.Refresh(); // what your refresh or init function is.
}
When I saw that solution I liked that as the option for me since I'm real heavy into using events to solve most of my problems. Thanks hvaughan3!
I'm working on Visual Studio, in C# language
I would like to ask if there's someway to go back to get the ButtonX.Text I pressed on the previous page, some sort of Login without a password.
Basically I need a worker to specify which person he is by clicking on their name (button) then it goes foward to the next page I have a label on the MasterPage but it resets everytime it goes on a next page what I would like to do is keep the info there
If you need some code tell me.
Thanks
You could use session variables?
On the button click handler on the first page...
protected void button_Click(object sender, EventArgs e)
{
Session["Worker"] = button.Text;
}
Then on the second page...
Label.Text = Session["Worker"];
Based-off your reply to Zollistic's answer, you could do this...
Apply this event to all your all your worker buttons...
protected void button_Click(object sender, EventArgs e)
{
if (Session["Worker"] == null) Session["Worker"] = "";
Session["Worker"] += button.Text + ",";
}
Now Session["Worker"] has a character-delimited list of all the clicked buttons. The character in this example is a comma; but you can change it to whatever you want (i.e. a pipe "|").
I have 2 pages in my application, A and B.
If I'm navigation from the outside of the application to A, I want to display a message box. If I'm navigation from B to A, I don't want to display anything.
Is there any way to identify in A the page which initiated navigation? i.e in A.Loaded (or any other event) I need something like
if(pageFromWhichIAmComingFrom == B)
OnNavigatedTo, OnNavigationFrom and OnNavigatedFrom don't seem to help me.
You could use the PhoneApplicationService class to store information about what page you were on last. For example, use OnNavigatedFrom on Page A:
void OnNavigatedFrom(object sender, Eventargs e)
{
PhoneApplicationService.Current.State["LastPage"] = "PageA";
}
And then check for that on the next page:
void OnNavigatedTo(object sender, Eventargs e)
{
if(PhoneApplicationService.Current.State["LastPage"].ToString() == "PageA")
{
// came from page A
}
else
{
// came from a different page
}
}
Hope this helps!
UPDATE:
One more thing I just saw that might be worth trying is using the NavigationService.BackStack property. I haven't tried this, but it seems like it should work. In your OnNavigatedTo event handler, you should be able to get the last entry from the stack to see your last page. This would be simpler and wouldn't require you to set any properties manually. Example:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var lastPage = NavigationService.BackStack.FirstOrDefault();
}
Found here.
when I navigate to Page1.xaml, I have an empty navidation stack, what I need to add into
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e){}
to add Page2.xaml into Navigation stack (I need to navidate into Page2.xaml only when I press go back button)
If I understand correctly, you want to navigate to Page2.xaml when the user press the Back button, correct?
You'll have to use the BackKeyPressed event to make that work, like so:
public MainPage()
{
InitializeComponent();
this.BackKeyPress += new EventHandler<System.ComponentModel.CancelEventArgs>(MainPage_BackKeyPress);
}
void MainPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
Dispatcher.BeginInvoke(() =>
{
NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative));
});
}
But please be advised that changing the default behavior of the Back button may lead to fail app certification!
I'm a bit of a beginner with this so i'll try and keep it simple.
I have a a xaml page with a button click event linking it to another xaml page. What I'm trying to do is on the click event take two strings and pass them to a text box on the second page. Can you please show me a simple code example of how to do this?
On the button click event of the first page you do something like the following
private void button1_Click(object sender, RoutedEventArgs e)
{
string urlWIthData = string.Format("/Page2.xaml?name={0}", txtName.Text);
this.NavigationService.Navigate(new Uri(urlWIthData, UriKind.Relative));
}
On the desintation page, you do the following:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
myTextBox.Text = this.NavigationContext.QueryString["name"].ToString();
}