I have two Pages ProductSearch,ProductDetail and Im changing the Content property to Navigate between Pages. I want to know if any events are fired so I can write some code in it?
On ProductDetail Page I have UIElement property
public UIElement MainContent { get; set; }
On ProductSearch Page I Navigate to ProductDetail By setting the Content property like this:
private void OnGetDetailsClick(object sender, RoutedEventArgs e)
{
ProductDetail productDetail = new ProductDetail();
productDetail.MainContent = this.Content;
this.Content = productDetail;
}
On ProductDetail Page's Back Button I navigate back to ProductSearch:
private void OnBackButtonClick(object sender, RoutedEventArgs e)
{
this.Content = MainContent;
}
Now I want to know how can I call a method when I navigate Back to ProductSearch Page i.e how would I know that I have Navigated from ProductDetail Page? I tried to check if it loads the page but found out that When you change content of control it doesn't fire the load event of the page. Any solution?
Yea this will not execute the load event since your are only changing the content obviously.
if you want to take advantage of navigation you should check out this video. Even if its done with blend the concepts apply also with Visual STudio: https://vimeo.com/6599527 (Simple Silverlight Master/Detail Navigation App with Blend 3)
You should check out articles on master detail binding like this one: https://msdn.microsoft.com/en-us/library/cc645060(v=vs.95).aspx
The key is to take advantage of the powerful binding concepts which come with silverlight. And if you are not using deep linking you might want to consider using a user control to hide/show details instead of a extra page.
HTH
Related
Essentially, what I'm trying to do is take a selected item from a combobox on Page 1 and set the image source on Page 2 based on that selection, but I'm not exactly sure how to make this happen.
On Page 1 I have:
private void pOneColorChoice(object sender, SelectionChangedEventArgs e)
{
setPlayerOneColor = PlayerOneColor.SelectedItem;
}
On Page 2 I'm not sure how to call that selection and then fit it into this to set the image:
BitmapImage ImageOne;
if (PlayerOneColor == Black)
{
ImageOne = new BitmapImage(new Uri("Assets/Black.jpg"));
PlayerOneImage.Source = ImageOne;
}
I would add other statements for the other color choices. I'm probably way off but any suggestions would be greatly appreciated!
When you are navigating to the Page2 you can send it as a parameter
private void pOneColorChoice(object sender, SelectionChangedEventArgs e)
{
setPlayerOneColor = PlayerOneColor.SelectedItem;
Frame.Navigate(typeof(Page2), setPlayerOneColor);
}
Here in Page 2 Navigation Method you can get your parameter like this
var name = e.NavigationParameter as yourType;
As has been answered here, you can pass your option as a parameter when you navigate to the page.
I'd recommend taking a look at following an MVVM design pattern for your application though, this way you can access your other page's view model from other pages. MVVM Light is a great starting point.
Doing it with this method will let your have one view model for your settings page where you choose your option, and then from the other, you'd access that same property which you've changed. I can go into more detail if required :)
I need to create a page in Code behind and navigate the main page to created page in Windows Store app
I tried this but its not working the black page is navigated
Page p1 = new Page();
p1.Content = pdfViewer1;
this.Frame.Navigate(typeof(Page),p1);
Navigating to a page created entirely in code behind is very tricky. I am not even sure it is possible to do (at least without some complex hack) This is due to the fact that Visual studio builds some classes behind the scene to ensure the navigation especially the class "XamlTypeInfoProvider" which is used to identify the pages to which navigation is possible.
According to me, the easiest way to navigate to a page created in code behind is to create a "normal" blank page and then to fill this blank page with content created in code behind.
// create the page content in code: here it is in the variable pdfviewer
this.Frame.Navigate(typeof(BlankPage1),pdfViewer);
and within the "blank page" use the OnNavigatedTo event to put the created page content on the screen
public sealed partial class BlankPage1 : Page
{
public BlankPage1()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
(this.Content as Grid).Children.Add( e.Parameter as UIElement);
base.OnNavigatedTo(e);
}
}
You'll need to make sure you're using the correct Frame object. From what you've provided, it looks like you need to use the "root" frame, much as is done when your main window is loaded in your app's OnLaunched override.
Here's an example of launching a secondary page from a main page.
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
Loaded += MainPage_Loaded;
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
rootFrame.Navigate(typeof(SecondaryPage));
}
}
Note that what you pass to Navigate is the type of the Page object, not an instance of it. Navigate will create an instance and navigate to it. The new page's Loaded handler can then connect up any additional content, such as the PDF viewer shown in your code. If necessary, you can pass arguments to the secondary page by using one of the other Navigate overloads.
I'm writing a music player app for WP8 in XAML and C#, but coming from a winforms background I don't know how to do the following.
My main page opens a separate page to display the tracks of a music album. The user can select some of these tracks which then get added to a central playlist which is working fine, but I want the main page to call its playlist refresh function when the song selection page is closed.
If I was doing this as winforms I would do something like:
private void ShowAlbumPage(Int16 albumId)
{
albumPage.albumId = albumId;
albumPage.ShowDialog();
RefreshPlaylist();
}
But this will not work for XAML
I currently have this:
private void ShowAlbumPage(Int16 albumId)
{
NavigationService.Navigate(new Uri("/AlbumPage.xaml?albumId=" + albumId.ToString(), UriKind.Relative));
}
Any suggestions on how and when to call RefreshPlaylist?
Its a vague idea ,but i think if i share i can also improve. You could pass the List to the AlbumPage (Current collection) , if user add a song to playlist , add this to the collection , when returning just send back the collection and update the Home Page.
// MainPage.Xaml
private void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SongsList selectedItemData = SelectedItem as SongsList ;
if(selectedItemData != null)
{
NavigationService.Navigate(new Uri(string.Format("/AlbumPage.xaml?parameter={0}",selectedItemData.ID ), UriKind.Relative));
}
}
//AlbumPAge.Xaml
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string parameter = this.NavigationContext.QueryString["parameter"];
SongsList country = null;
// GETS THE SONG COLLECTION HERE , UPDATE WHEN USER ADD TO PLAYLIST , AND RETURN BACK.
}
**If your doing on MVVM Way .This is just an Idea not tested.**
There is view-model it contains Collection of Songs, Maintain the same view-model over the two pages , Update the Song collection so Mainpage UI will automatically gets updated.
ViewModel Maintain the same over the Album Page.
ObservableCollection<Songs> _songs=new ObservableCollection<Songs>();
_songs.Add(new songs{Artist="ArtistName",Album="AlbumName"});
// AlbumPage CodeBehind.
private AddSongtoPlaylist(Song currentSong)
{
_songs.Add(currentSong);
}
Reference
Some of the ways you could accomplish this:
Add the newly selected songs on the Application level on the PhoneApplicationService.State Dictionary. Whenever you return to the main page, load the transient state that will include the added songs.
Save them on a variable on the Application level and whenever navigating to the main page load the playlist.
Store them in the IsolatedStorage and load them whenever you navigate to the main page.
Pass them back to your main page through the navigation QueryString.
The method you choose is just a matter of personal preference.
I think I have found a reasonably simple workaround - I have just put a call to RefreshPlayQueue() in the OnNavigatedTo method of MainPage.xaml. This seems to work
I am developing WPF MUI application.I navigate to another page using button onclick and print some text on page1.xaml . after i navigate using another button to print another text on page1.xaml .but i could not do that.my out put was not new text.it is early details only.can't reload page1.xaml .when navigating I pass the parameter and according to parameter print deference text on same page.can anyone help me?
this is my navigation code
var frame = NavigationHelper.FindFrame(null, this);
frame.Source = new Uri("../Content/Sale/SaleInvoice/Nested/saleNested.xaml", UriKind.Relative);
Have the page databound to a ViewModel. Once you want to refresh just create a new Viewmodel. It would make sense to have the execution of the reloading being done in a backgroudworker so your UI stays responsive. This is esspecially usefull if you are refreshing some resource from a webService or some other online source.
The Data bind to the page at the initial time to reload the page I'm using this method
On xaml page just add Loaded Property:
<UserControl x:Class="ModernUINavigationApp1.Pages.Page"
...
Loaded="OnLoad" >
Then Add event handler in code behind to make the page do whatever you want when it's loaded
private void OnLoad(object sender, RoutedEventArgs e)
{
}
Hope this help :D
Make sure your ViewModel implements INotifyPropertyChanged. If your Page1.xaml's DataContext is set to the ViewModel, and your XAML uses bindings properly, any change to the ViewModel will be reflected in the UI. You won't need to refresh anything. Just update the property in the ViewModel object.
If you update your question with example XAML and C#, I can be of more help.
I am using ASP.NET with C# 2.0 and Visual Studio 2005. I am using a Master page and content pages. I have a treeview menu in the master page and when a user selects any menu item I redirect to that content page.
My problem is that after a user navigates to the content page all the treenodes refresh and the structure is collapsed. I want the selected treenode to stay expanded.
Can anybody help me out?
When you refresh the treeview you want to call treeView1.ExpandAll();
Also add an event for the BeforeCollapse and set the event's Cancel property to true, to prevent the user from collapsing your treenodes.
private void treeView1_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
{
e.Cancel = true;
}
Hope this helps.
-jeremy
This is a common enough problem that is automatically handled by ASP.NET if you use a SiteMapDataSource control as the datasource for your TreeView. In this case, you haven't mentioned what the Datasource of your TreeView is.
You also haven't mentioned if the TreeView contains links (the NavigateUrl property is set) or Text items that postback for each click. If it is the former, then as far as I know, you are out of luck! This is because none of the Selection events are raised for TreeNodes which have their NavigateUrl set. They just function as regular hyperlinks.
If however, it is the latter, then you can try out the following steps :
a. Handle the SelectedNodeChanged event of the TreeView. In this event handler, retrieve the current value of the SelectedNode.ValuePath property and store it in ViewState/Session. Use the Value of the of the SelectedNode to conditionally redirect the page to URL mapped to it.
Something like the following:
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
TreeNode selNode = TreeView1.SelectedNode;
string pathToNode = selNode.ValuePath;
Session.Add("SelPath", pathToNode);
switch (selNode.Value)
{
//Redirect to URL accordingly.
}
}
b. On subsequent load of the Master page (the page to which you redirected), retrieve the value of the ValuePath set earlier and find the previously Selected node and Expand it.
Something like the following:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
string pathToNode = (string)Session("SelPath");
Session.Remove("SelPath");
TreeNode selNode = TreeView1.FindNode(pathToNode);
if (selNode != null)
{
selNode.Expand();
}
}
}
Note that I haven't had an opportunity to test the code so this is mostly hypothetical.
Try using the OnTreeNodeDataBound event and the treeView.SelectedNode property
Also, might want to check how/ when you're binding your TreeView to it's DataSource. You might be rebinding it on IsPostBack which will re-render the tree.
The TreeView should maintain its nodes on PostBack.
Even though you are using a Master page, once the user navigates to the content page it is rendered as a new/different page. Because of the Master page the same treeview is loaded but not the same instance. You will need to store and load what nodes were expanded.