I can't for the life of me figure out how to make a multiple page application in wpf. My issue is that when I load a page into the frame it does not use the style of the page
xaml
<Frame x:Name="Main" Margin="0,82,0,0"</Frame>
c# code
private void SettingsButton_Click(object sender, RoutedEventArgs e)
{
Main.DataContext = new settingsPage();
Main.Content = new settingsPage();
SettingsButton.Content = Main.DataContext.ToString();
}
Hopefully someone knows what is going on here and can help :)
Thanks
Here you can find a fully working example:
Usage of Frame control
The thing is that you just need to switch the source of the frame by using the NavigationService. That should do the trick with your styles.
Related
I'm Not good at English, Thanks in advance.
I have problem that i just cant figure out right now. I am trying to develop a UWP and im stuck implementing this functionality.
I have a Header(Navigation view) in NavPage, such as this.
As you know, The Header and The Frame in one page, frame load a View, When I click "Up" or other button(appbarButton), I want pass a Info to Frame's current page(is MainPage).
At frist, I want invoking a current page method by appBarbutton, need static.No
then, I want pass a info to that page, and handle it in OnNavigatedTo(NavigationEventArgs e)
here is problem.
this is code:
private void BackHome(object sender, RoutedEventArgs e)
{
ContentFrame.Navigate(typeof(MainPage), "*BackHome");
}
private void BackUp(object sender, RoutedEventArgs e)
{
ContentFrame.Navigate(typeof(MainPage), "*BackUp");
}
I try two buttons.
But...when I click this appbar button,it is work, info pass success, but because Navigate() method, the frame's current page is reload, Parameters passed before useless
How pass info and don't reload?
any ideas? thanks every body.
Navigate might actually not be the best suited method to call since what it actually does is telling your frame to read the content of the specified Page, something that you only want to do when your specified page is different from the one you're currently in.
Edit 1.
Do you really need to pass information to the page for the situations where you're going to keep using the same content as your frame? If not, and just in order to stop the reloading for those situations, just check if you are actually calling the Navigate method to another page.
if(ContentFrame.SourcePageType != typeof(MainPage))
ContentFrame.Navigate(typeof(MainPage), "*BackUp");
I cannot figure out why my button click is not navigating to a new page. I have tried writing this code several ways, but none of them work. EnlargedScreenCap is a WPF page and is in the same directory as the window in which I want to load it.
private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
NavigationService nav = NavigationService.GetNavigationService(this);
ImageSource image = sender as ImageSource;
EnlargedScreenCap esc = new EnlargedScreenCap();
esc.SetImage(image);
nav.Navigate(esc);
}
Written like this I get a null reference exception because NavigationService is not getting initialized.
private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
ImageSource image = sender as ImageSource;
EnlargedScreenCap esc = new EnlargedScreenCap();
esc.SetImage(image);
NavigationService nav = NavigationService.GetNavigationService(this);
nav.Navigate(new Uri("//EnlargedScreenCap.xaml"), UriKind.RelativeOrAbsolute);
}
When written like the above code, I get an invalid URI error, although nav is still null.
I tried searching around and this problem has been brought up many times, but no one seems to have a decent explanation or at least one that I have been able to successfully apply.
Could someone point me in the right direction?
I do not think you should be creating the NavigationService the way you are doing. Also if you want to enable navigation all this should be done in a Frame
<Frame x:Name="_mainFrame" />
Then you could do something like
_mainFrame.NavigationService.Navigate(new Uri("EnlargedScreenCap.xaml", UriKind.Relative));
Without the double slashes in front.
Checkout this article.
http://paulstovell.com/blog/wpf-navigation
Hope it helps
My Silverlight application has multiple XAML pages. For example, one displays a clock, one displays a timer. I have buttons to switch back and forth like so:
private void switchRight(object sender, RoutedEventArgs e)
{
this.Content = new Clock();
}
private void switchLeft(object sender, RoutedEventArgs e)
{
this.Content = new Timer();
}
I am trying to use the NavigationService to switch back and forth so I can have other pages running in the background rather than creating a new instance each time.
I am trying
NavigationService.Navigate(new uri("/Timer.xaml", UriKind.Relative));
but it doesn't seem to do anything and I can't find any good examples to help.
Here is a link
http://blogs.msdn.com/b/dphill/archive/2009/04/28/silverlight-navigation-part-3.aspx ,
Beside, I think you can use Threading for background processes.i.e.when you start a timer no need to show any xaml.
But for page instances you need to manage it very carefully otherwise stackoverflow :)
Depending on business rules its hard to decide navigation as being in a web browser.
We created our own Wizard (with rules). You may create your own NavigationManager. For validation I can offer http://fluentvalidation.codeplex.com/
I use frame to load pages in my WPF project, is there a way to detect a page loading is the first or not? Something like "IsPostBack" in ASP.NET, I'm trying to find an equivalent to it in WPF.
I found IsPostBack is a property in System.Web.UI, should I include this namespace in my page?
I still have to use a static variable "bool SystemLoad = true", at the first load it is True and then I set it to False, so when the page is reloaded, it doesn't do as at the first load.
Thank you!
IsPostBack is not relevant to a WPF application, and since your WPF application window does not inherit 'Page', there is no way you can use IsPostBack variable from System.Web.UI.
The best you can do is to implement your custom logic as below.
private bool isLoaded;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (isLoaded)
return;
isLoaded = true;
}
I'm trying to write a silverlight application that takes in InitParams and then uses those InitParams to make a change to the Source of the MediaElement on the page. I'm trying to figure out the proper place to put my code.
I watched Tim Heuer's excellent video on InitParams, but in the video (which was for Silverlight 2), it shows the following on the Page.xaml.cs:
void Page_Loaded(object sender, RoutedEventArgs e)
{
}
I don't see Page_Loaded when I open MainPage.xaml.cs, and I'm wondering if that was automatically created in the Silverlight 2 SDK and left out of the Silverlight 3 SDK. Or perhaps Tim added that in his video manually.
I find that I can go into the opening UserControl tag of MainPage.xaml and add Loaded="<New_Event_Handler>" which creates the following in MainPage.xaml.cs:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
}
By default, there's also the following in MainPage.xaml.cs, which is run during the Application_Startup event in App.xaml.cs:
public MainPage()
{
InitializeComponent();
}
I need to figure out where is the best place to insert my code to change the Source on my MediaElement in my xaml. Should I put it in MainPage? Should I add the Loaded event handler and put it into UserControl_Loaded? If it's supposed to be Page_Loaded, where do I find that in Silverlight 3?
Any help would be much appreciated.
"UserControl_Loaded" and "Page_Loaded" are just method names and the names don't matter (you could name the method "Foo" if you wanted). What makes these methods do anything is the fact that they are attached to the Loaded event on the UserControl (which is what you did when you edited the MainPage.xaml file).