locked
LoadData for App.ViewModel in runtime RRS feed

  • Question

  • hello.

    i'm new to WP Development and i'm just starting to learn it.

    i have a very simple question.

    i have an app.that user click the add button , app navigates to Page1.xaml and it allows user to enter a title and a info.

    i save those values with Json into IsolatedStorageSettings.ApplicationSettings.

    until here it's okay.it shows my added titles in the MainPage.xaml.

    i added the ability to delete those entries.when user click them.(it shows for example Test1ForTitle and when user clicks it , it will be deleted.)

    it delete that entry from IsolatedStorageSettings.ApplicationSettings.

    but my problem is that it still shows it for user.if i close the app and rerun it you can see that , that entry is deleted and it doesn't show that entry.(you can close the app and rerun it or navigate to that Page1.xaml and come back to MainPage.xaml.both of them works.)

    so.what did i do wrong?

    (i have Events.cs , EventGroup.cs and i have EventModel.cs for my model)

    MainPage.xaml:

    <phone:PhoneApplicationPage.Resources>
            <DataTemplate x:Name="EventTemplate">
                <Grid Height="100" Margin="10,10,10,10">
                    <Grid Height="100" Background="{StaticResource PhoneAccentBrush}">
                        <TextBlock Text="{Binding Title}" FontSize="25" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
                    </Grid>
                </Grid>
            </DataTemplate>
        </phone:PhoneApplicationPage.Resources>
    
        <!--LayoutRoot is the root grid where all page content is placed-->
        <Grid x:Name="LayoutRoot" Background="Transparent">
    
            <!--Pivot Control-->
            <phone:Pivot Title="MY APPLICATION">
                <!--Pivot item one-->
                <phone:PivotItem Header="{Binding Mine.Title}">
                    <!--Double line list with text wrapping-->
                    <phone:LongListSelector ItemsSource="{Binding Mine.AllEvents}" ItemTemplate="{StaticResource EventTemplate}">
                    </phone:LongListSelector>
                </phone:PivotItem>
    
                <!--Pivot item two-->
                <phone:PivotItem Header="{Binding Yours.Title}">
                    <!--Double line list with text wrapping-->
                    <phone:LongListSelector ItemsSource="{Binding Yours.AllEvents}" ItemTemplate="{StaticResource EventTemplate}">
                    </phone:LongListSelector>
                </phone:PivotItem>
    
                <phone:PivotItem Header="Added">
                    <phone:LongListSelector ItemsSource="{Binding Added.AllEvents}" ItemTemplate="{StaticResource EventTemplate}" SelectionChanged="Pivot_SelectionChanged">
                    </phone:LongListSelector>
                </phone:PivotItem>
    
                <phone:PivotItem Header="Add">
                    <Button Height="100" Width="150" FontSize="40" FontWeight="ExtraBold" Background="white" Foreground="red" Name="btn_Nav" Click="btn_Nav_Click">Add</Button>
                </phone:PivotItem>
            </phone:Pivot>
    
        </Grid>


    MainPage.xaml.cs (my events):

    private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                LongListSelector GroupSelector = sender as LongListSelector;
                if (GroupSelector == null)
                {
                    return;
                }
                Events events = GroupSelector.SelectedItem as Events;
                if (events == null)
                {
                    return;
                }
                EventGroup eventGroup = App.ViewModel.Added;
    
                string DataFromAppSetting;
                if (IsolatedStorageSettings.ApplicationSettings.TryGetValue("AddedEvents", out DataFromAppSetting))
                {
                    eventGroup = JsonConvert.DeserializeObject<EventGroup>(DataFromAppSetting);
                }
    
                eventGroup.AllEvents.RemoveAll(t => t.Title == events.Title);
    
                var data = JsonConvert.SerializeObject(eventGroup);
                IsolatedStorageSettings.ApplicationSettings["AddedEvents"] = data;
                IsolatedStorageSettings.ApplicationSettings.Save();
    
                App.ViewModel.Added.AllEvents = JsonConvert.DeserializeObject<EventGroup>(data).AllEvents;
    
                App.ViewModel.LoadData();
            }
    
            private void btn_Nav_Click(object sender, RoutedEventArgs e)
            {
                NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
            }


    if you want i can upload my project files so you can look closer.

    thank you in advance for your attention.

    Monday, October 28, 2013 6:36 AM

Answers

  • You should change your List to a ObservableCollection<t>Class to reflect the changes of the collection to your bindings.

    Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

    Of course , if the propertys itself changes (titel etc.) then you must implement the inotifyproperty changed like pkr2000 sayed.


    Monday, October 28, 2013 1:13 PM

All replies

  • Look at NotifiyPropertyChanged, when you bind you have to let the view know the data has changed

    http://pauliom.wordpress.com

    Monday, October 28, 2013 10:31 AM
  • Either reset Itemsource after delete or set mode twoway and use NotifyPropertyChanged in Viewmodels
    Monday, October 28, 2013 12:37 PM
  • Thank you both.

    i think that using NotifyPropertyChanged is the best way.

    is there any tutorial for it? cause i haven't used it and i don't know how to use it.i read some articles about it today.

    where should i use it? in my EventModel.cs or EventGroup.cs?

    here is my ViewModel:

    Events.cs:

    public class Events
        {
            public string Title { get; set; }
            public string OtherInfo { get; set; }
        }

    EventGroup.cs:

    public class EventGroup
        {
            public EventGroup()
            {
                AllEvents = new List<Events>();
            }
            public List<Events> AllEvents { get; set; }
            public string Title { get; set; }
        }

    EventModel.cs (i use it as ViewModel):

    public class EventModel
    {
    	public EventGroup Mine { get; set; }
            public EventGroup Yours { get; set; }
            public EventGroup Added { get; set; }
            public bool IsDataLoaded { get; set; }
    
            public void LoadData()
            {
                //Load Data
                Mine = CreateMine();
                Yours = CreateYours();
                Added = CreateAdded();
                IsDataLoaded = true;
            }
    
            private EventGroup CreateAdded()
            {
                EventGroup Events;
    
                string DataFromAppSetting;
    
                if (IsolatedStorageSettings.ApplicationSettings.TryGetValue("AddedEvents", out DataFromAppSetting))
                {
                    Events = JsonConvert.DeserializeObject<EventGroup>(DataFromAppSetting);
                }
                else
                {
                    Events = new EventGroup();
                    Events.Title = "Added";
                }
    
                return Events;
            }
    
            private EventGroup CreateYours()
            {
                EventGroup Events = new EventGroup();
    
                Events.Title = "Mine";
    
                Events.AllEvents.Add(new Events() { Title = "First Of Mine", OtherInfo = "First Of Mine Details & Info" });
                Events.AllEvents.Add(new Events() { Title = "Second Of Mine", OtherInfo = "Second Of Mine Details & Info" });
                Events.AllEvents.Add(new Events() { Title = "Third Of Mine", OtherInfo = "Third Of Mine Details & Info" });
    
                return Events;
            }
    
            private EventGroup CreateMine()
            {
                EventGroup Events = new EventGroup();
    
                Events.Title = "Yours";
    
                Events.AllEvents.Add(new Events() { Title = "First Of Yours", OtherInfo = "First Of Yours Details & Info" });
                Events.AllEvents.Add(new Events() { Title = "Second Of Yours", OtherInfo = "Second Of Yours Details & Info" });
                Events.AllEvents.Add(new Events() { Title = "Third Of Yours", OtherInfo = "Third Of Yours Details & Info" });
    
                return Events;
            }
        }

    Monday, October 28, 2013 12:47 PM
  • Can you post your delete function ?

    Monday, October 28, 2013 12:58 PM
  • Can you post your delete function ?

    i use some code in MainPage.xaml.cs

    i know it's not the best way for deleting.but it was the one that crossed my mind:

    private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                LongListSelector GroupSelector = sender as LongListSelector;
                if (GroupSelector == null)
                {
                    return;
                }
                Events events = GroupSelector.SelectedItem as Events;
                if (events == null)
                {
                    return;
                }
                EventGroup eventGroup = App.ViewModel.Added;
    
                string DataFromAppSetting;
                if (IsolatedStorageSettings.ApplicationSettings.TryGetValue("AddedEvents", out DataFromAppSetting))
                {
                    eventGroup = JsonConvert.DeserializeObject<EventGroup>(DataFromAppSetting);
                }
    
                eventGroup.AllEvents.RemoveAll(t => t.Title == events.Title);
    
                var data = JsonConvert.SerializeObject(eventGroup);
                IsolatedStorageSettings.ApplicationSettings["AddedEvents"] = data;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }


    Monday, October 28, 2013 1:03 PM
  • You should change your List to a ObservableCollection<t>Class to reflect the changes of the collection to your bindings.

    Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

    Of course , if the propertys itself changes (titel etc.) then you must implement the inotifyproperty changed like pkr2000 sayed.


    Monday, October 28, 2013 1:13 PM
  • First of all.thank all of you.

    it worked.

    i used ObservableCollection<t> in my EventGroup.cs.

    and used NotifyPropertyChanged in EventGroup.cs and EventModel.cs and it went all ok.thank to all of you. ;)

    Tuesday, October 29, 2013 10:02 AM