How to Work with NavigationService.Navigate in Windows Phone8.1 - c#

How To declare "?msg=" in WP8.1...?
i tried this code is working in WP8
In WP8
private void passParam_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/SecondPage.xaml?msg=" + textBox1.Text, UriKind.Relative));
}
coming to WP8.1 used Frame.Navigate
In WP8.1
private void passParam_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(SecondPage.xaml) + textBox1.Text);
}
Then how to declare "?msg=" in WP8.1....?

If you look at Frame.Navigate you will see, the second parameter is a navigation parameter you can pass. Take a look at http://mikaelkoskinen.net/winrt-xaml-navigating-from-page-to-page-how-it-differs-from-windows-phone-7/

In Windows Phone 8.1, Page Navigation method is:
Frame.Navigate(typeof(SecondPage), param);
or
Frame.Navigate(typeof(SecondPage));
In your code, you can set param to be yours like this:
Frame.Navigate(typeof(SecondPage), textBox1.Text);
You can find the documentation for this here MSDN

As people already wrote, what you need is:
Frame.Navigate(typeof(SecondPage), textBox1.Text);
The second paramater can be actually any object.
Then, to get the paremeter in SecondPage you can do this (in your case):
protected override void OnNavigatedTo(NavigationEventArgs e)
{
String your_text = e.Parameter as String;
}

Related

What typeof(Page) Am I Navigating From?

In the Basic Page template, there is the method NavigationHelper_LoadState(object sender, LoadStateEventArgs e){}. How can I check to see what the previous page was?
if(sender.GetType() == typeof(PreviousPage)){} does not work!
My primary objective is to know where my data is coming from in clean in code.
Although one option is to send a parameter with page type, I wouldn't do it because you might need that parameter to pass some other relevant data between pages.
So, I'd rather do something like this:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (Frame.CanGoBack)
{
PageStackEntry lastPage = Frame.BackStack[Frame.BackStackDepth - 1];
if (lastPage.SourcePageType == typeof(MainPage))
{
// do something
}
}
this.navigationHelper.OnNavigatedTo(e);
}
Your method won't work as NavigationHelper_LoadState(object sender, LoadStateEventArgs e){} is being called by NavigationHelper and the the sender passed is NavigationHelper - there is no information passed from which Page the navigation has started.
I don't see any properties in which you could read this information, but you can surely pass your previous type as a parameter:
// when navigating:
Frame.Navigate(typeof(Page1), this.ToString());
// in target Page:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter == typeof(MainPage).ToString())
Debug.WriteLine("Previous was MainPage");
}

Check for background audio playing and if so go to exact page

I want after start application or activation to check if BackgroundAudioPlayer is playing and if so then go to page with player. I know that I couldn't use NavigationService and I found that in App.xaml.cs I should use RootVisual as in Activated but it's not working. RootVisual is null. The first one has no error but the problem is that I get to MainPage.xaml. So how can I fix it? Thanks
private void Application_Launching(object sender, LaunchingEventArgs e)
{
if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing)
RootFrame.Navigate(new Uri("/PlayerPage.xaml", UriKind.RelativeOrAbsolute));
}
private void Application_Activated(object sender, ActivatedEventArgs e)
{
if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing)
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/PlayerPage.xaml", UriKind.RelativeOrAbsolute));
}
I have amended your code, please have a look; it will work for you.
Remove your code form the Application_Activated event and put into Application_Launching Event. And do not write anything into Application_Activated.(in term of navigation context). Follow the two steps:
STEP-1
Go to WMAppManifest.xml file and remove "MainPage.xaml" entry from the default task. And keep empty entry of NavigationPage. like this NavigationPage=""
see the follwoing code snippet for the same.
<Tasks>
<DefaultTask Name ="_default" NavigationPage=""/>
</Tasks>
<Tokens>
<PrimaryToken TokenID="liveTilesToken" TaskName="_default">
<TemplateType5>
<BackgroundImageURI IsRelative="true" IsResource="false">Background.png</BackgroundImageURI>
<Count>0</Count>
<Title>liveTiles</Title>
</TemplateType5>
</PrimaryToken>
</Tokens>
STEP-2 Update code accordingly
private void Application_Launching(object sender, LaunchingEventArgs e)
{
Uri newUri = null;
newUri = true ? new Uri("/MainPage.xaml", UriKind.Relative) : new Uri("/PlayerPage.xaml", UriKind.Relative);
RootFrame.Navigate(newUri);
}
Hope it helps.
Thank you.

parameter passing between XAML in windows phone

There are 3 xaml pages, 2 of which call the third page with different parameters. How do I write onNavigatedTo() in the third page?
Am using NavigationService.Navigate(new Uri("/third.xaml?paramter=xxxx", UriKind.Relative));
Borrowed and adapted this code from http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff626521(v=vs.105).aspx.
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string msg = "";
if (NavigationContext.QueryString.TryGetValue("parameter", out msg))
// now your parameter is in the msg variable, and you could do stuff here.
}

Windows 8 navigation, passing an URI as parameter

I just started developing for windows 8, and I'm having some difficult in passing parameters in navigation between pages.
What I am trying to do is: I have two pages (page1 and page2) In page1, there's a menu with a button. When I click in this button, the click event should pass as a parameter an possible URI or path, that will set the source of a image in page2.
this is the code in page1:
private void Button_Click_2(object sender, RoutedEventArgs e)
{
//this.Frame.Navigate(typeof(SplitPage), "ms-appx:/Imgs/1.png");
this.Frame.Navigate(typeof(SplitPage), new Uri("ms-appx:/Imgs/1.png", UriKind.RelativeOrAbsolute));
}
and page2, for receiving the Uri:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var imgSource = e.Parameter as ImageSource;
this.imgParaPintar.Source = imgSource;
}
I'm noticing that imgSource is receiving nothing, it keeps as null.
So, any clues on what I'm doing wrong or what I am missing?
I think you need code like this (untested):
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.imgParaPintar.Source = new BitmapImage((Uri)e.Parameter);
}
You might need to add using Windows.UI.Xaml.Media.Imaging; to your file, if you don't have it there already.
It's worth noting, as stated in the comments here:
http://answers.flyppdevportal.com/categories/metro/csharpvb.aspx?ID=4b847d71-9cd5-4457-add9-f68e457b23ff
That you can only pass primitive types as navigation parameters.

c# call function from class

now i have the current code o MainUC.cs:
private void tsbNoviRacun_Click(object sender, EventArgs e)
{
if (racunuc == null)
{
racunuc = new RacunUC();
racunuc.Dock = DockStyle.Fill;
Controls.Add(racunuc);
}
racunuc.BringToFront();
The thing i want to do is clean the code from main page/form. I have 2 taskbar and 2 toolbar buttons that are calling the same form (this one above), so i don't want to write the code 4 times. I tried to make new class.cs with properties and do it with return value, but it didn't work. Can someone help me with it, or, is there possiblity to call the same code on current page/form. Something like
private void tsbStariRacuni_Click(object sender, EventArgs e)
{
call tsbNoviRacun();
}
"( this isn't working, i know :p)
EDiT: Oh damn me, thanks guys!
In c# there is no "call" keyword for invoking functions. You just type the name and all required arguments in round brackets.
private void tsbStariRacuni_Click(object sender, EventArgs e)
{
tsbNoviRacun_Click(sender, e);
}
This should do it:
public void tsbNoviRacun()
{
if (racunuc == null)
{
racunuc = new RacunUC();
racunuc.Dock = DockStyle.Fill;
Controls.Add(racunuc);
}
racunuc.BringToFront();
}
private void tsbNoviRacun_Click(object sender, EventArgs e)
{
tsbNoviRacun();
}
You can call that method from all the event handlers you want it to run on. Obviously this function is depended on Controls and DockStyle so you must put it within scope of this.

Categories

Resources