Basically I have a longlistselector and it contains writings when one of them is selected I am going to navigate to a page and its going to show the all detailed information of this writing (article , date , image etc.)
Don't worry about the data, I checked and data contains the details all I need is how to send this Writing object (named data) with navigating.Or can you give any suggestion how can I do this but without using uri query
Thank you
private void longList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LongListSelector selector = sender as LongListSelector;
Writing data = selector.SelectedItem as Writing;
NavigationService.Navigate(new Uri("/WritingPage.xaml", UriKind.Relative));
}
To pass an Object from one page to another PhoneApplicationservice is easiest way. Here is the simple example to pass Object from one page to another. Its tested.
private void longList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (PhoneApplicationService.Current.State.ContainsKey("Data"))
if (PhoneApplicationService.Current.State["Data"] != null)
PhoneApplicationService.Current.State.Remove("Data");
LongListSelector selector = sender as LongListSelector;
Writing data = selector.SelectedItem as Writing;
PhoneApplicationService.Current.State["Data"] = data ;
NavigationService.Navigate(new Uri("/WritingPage.xaml", UriKind.Relative));
}
//On second page
//I assume you want to Data on page load
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Writing data = new Writing ();
data =(Writing)PhoneApplicationService.Current.State["Data"]
PhoneApplicationService.Current.State.Remove("Data");
}
Are you coding in MVVM ?
If yes, you can send and register to an Event (published by your ViewModel linked to the LongList and subscribe this event in your ViewModel linked to WrittingPage.)
Simple and better way is to make "Writing" object scope Global.
Here, I added a class file to solution "Global.cs" and I declared "data" variable of "Writing" type.
public static Writing data = new Writing();
Use the same code you did and access the "data" variable here. I have done some minor change to your code.
private void longList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LongListSelector selector = sender as LongListSelector;
Global.data = selector.SelectedItem as Writing;
NavigationService.Navigate(new Uri("/WritingPage.xaml", UriKind.Relative));
}
Now access "data" variable on "WritingPage.xaml" after you navigate to it. Use value of this variable and then make it null.
Related
For some time i'm trying to create navigation service in my app while studying Xamarin. My approach here don't foolow mvvm desing pattern but at first i treid to have functionality working on which i will study further. I figured out how to navigate using ItemSelected property in listview through event fired in code-behind file and pass parameter from object placed in listview as a string. I want to changed it for more custom way by adding tap gesture on image not (imagebutton don't even fired event for new page when i added tap gesture on it in xaml file)
This worked for itemselected:
private async void SampleItem_Tapped(object sender, ItemTappedEventArgs e)
{
var details = e.Item as SampleModel;
await Navigation.PushAsync(new Page1(details));
}
new page codebehind:
public Page1(SimpleModel sample)
{
InitializeComponent();
var pageServices = new PageServices();
BackgroundImage = sample.SampleMainBackgroundImage;
}
This is where i need your help : ( app throwing exception when i assing as constructor of new page a string property and bound it to my string backgroundimagebinding (object reference not set to an instance of an object new page) )
private async void SampleIcon_Tapped(object sender, ItemTappedEventArgs e)
{
var details = e.Item as SampleModel;
await Navigation.PushAsync(newPage1(details));
}
e.Item on code below would be the visual element which I think is the image. Hence, it can't be casted as SampleModel.
private async void SampleIcon_Tapped(object sender, ItemTappedEventArgs e)
{
var details = e.Item as SampleModel;
await Navigation.PushAsync(newPage1(details));
}
It worked on ListView maybe because you've set the ItemsSource to a list of PageModels. Btw, It would be really helpful next time if you include all related codes. E.g. xaml part. Also, try out debugger tools so you can view the details of the objects passed on the method.
I'm having a hard time building a filtering system in Sitecore 7.
I have 2 sublayouts, on the same level of the page.
Sublayout A is a sidebar that contains a list of checkboxes and has an event that populates a List with the selected values.
Sublayout B displays a set of items.
What I would like to do, is send the populated List from sublayout A to sublayout B in order to filter the items list based on what the user selected.
I was able to do this by passing the data through Session, but this is not an optimal way of handling that data.
I've tried defining a property for sublayout A and loading the list there, but I can't get the exact instance of sublayout A from sublayout B in order to read the populated property.
Also, trying to Page.FindControl("IdOfSomeElementFromSublayoutA") always returns null in Sublayout B. Even though I've casted Page as the .aspx page that contains both Sublayouts.
I'm using Sitecore 7 Update 2.
Thanks a lot for your time.
The best way to do this is by raising (and subscribing to) events using the Sitecore.Events.Event class. Your sidebar sublayout would raise an event using something like the following within a button's click event handler:
Sitecore.Events.Event.RaiseEvent("YourEventName", new YourEventArgsClass { Property = "SomeValue" });
then in the other sublayout you would need to have the following set up in order to handle the event:
public partial class YourOtherSublayout : System.Web.UI.UserControl
{
private System.EventHandler eventHandlerRef;
protected void Page_Load(object sender, EventArgs e)
{
eventHandlerRef = EventHandlerMethod;
Sitecore.Events.Event.Subscribe("YourEventName", eventHandlerRef);
}
protected void Page_Unload(object sender, EventArgs e)
{
if (eventHandlerRef != null)
{
Sitecore.Events.Event.Unsubscribe("YourEventName", eventHandlerRef);
}
}
private void EventHandlerMethod(object sender, EventArgs e)
{
if (e != null)
{
//do stuff here
}
}
}
Note: it is important to keep the Page_Unload code there otherwise you will see the EventHandler method being called multiple times.
I have a silverlight page with a listbox and a combobox...
Based on what the user clicks in the list box, I want to populate the dropdownbox. The completed event is the same for each item in the list box (items include "BaseTypes", "Bays", "Face", etc)
How can I make the completed method generic so I don't have to have one for each call?
private void lstEdits_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ServiceCylinderClient obj = new ServiceCylinderClient();
obj.GetBaysCompleted += new EventHandler<GetBaysCompletedEventArgs>(GetBaysCompleted(this, baysEventArgs));
string selectedItem = lstEdits.SelectedItem as string;
switch selectedItem
{
case "BaseTypes":
obj.GetBaseTypesCompleted += new EventHandler<GetBaseTypesCompletedEventArgs>(GetBaysCompleted(this, baysEventArgs));
obj.getGetBaseTypesAsync();
break;
case "Bays":
obj.GetBaysCompleted += new EventHandler<GetBaysCompletedEventArgs>(GetBaysCompleted(this, baysEventArgs));
obj.getGetBaysAsync();
break;
}
}
As it stands now, I will have to have a "completed method" for each call, but since they would all do the same thing (just set the list box items source)..i'd like to make it generic to simplify things.
void GetBaseTypesCompleted(object sender, getBaseTypesCompletedEventArgs e)
{
lstEdits.ItemsSource = e.Result;
}
void GetBaysCompleted(object sender, getBaysCompletedEventArgs e)
{
lstEdits.ItemsSource = e.Result;
}
Thanks in advance!
I believe you would need to use reflection to read the 'Result' property off the 'CompletedEventArgs' as they do not all come from a base type that exposes 'Result'.
You should be able to do something like the following:
lstEdits.ItemsSource = (IEnumerable)e.GetType().GetProperty("Result").GetValue(e, null);
I think it dont have easy solution for this problem,because every completedmethod has different EventArgs for different result.
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.
I have a UserControl, which is basically a wrapper for a GridView that needs to send a message every time a cell content (of the GridView) is changed. Usually, that could be solved like this:
private void MainDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
var editingTextBox = e.EditingElement as TextBox;
doSomething(editingTextBox.Text);
}
The problem is that I don't know the type of EditingElement (which comes as a FrameworkElement) so I cannot do the conversion. And in that moment, the currentCell.SelectedValue is still the original value. I also don't have control over the model (where I could implement INotifyPropertyChanged and use it to catch the changes).
Is there some simple way I am missing? Or how would you go about implementing this? Thank you for any suggestions.
Wrapping your model within a CollectionView and use this for the binding.
myCollectionView = (CollectionView)
CollectionViewSource.GetDefaultView(rootElem.DataContext);
This will provide you a INotifyPropertyChanged interface.
Update
Sorry my first answer was somewhat misleading.
If you're not able to change the model, you should create a view model. The view model, implementing INotifyPropertyChanged can provide the needed change events without any knowledge of the current view. This way the view doesn't depend directly on the model.
Futher reading:
The role of the model in MVVM
I have found a very simple solution (can't belive I haven't seen that one) composed of catching two events from the DataGrid.
Here is the code:
private object changedRow;
private void MainDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
changedRow = e.Row.Item;
}
private void MainDataGrid_CurrentCellChanged(object sender, EventArgs e)
{
if (changedRow != null)
{
// do something with the edited row here
changedRow = null;
}
}