0

アプリにキーの連絡先リストを保存するために IsolatedStorageSettings を使用しています。ただし、アプリは、アプリがアクティブになるまでリストを保存するだけです(つまり、あるページから別のページに移動するなど)。アプリを終了して再起動すると、保存されているキー/連絡先リストが見つかりません。アプリがインストールされるまで、アプリのリストを永続的に保存するにはどうすればよいですか? これが私のビューモデルのコードです、私は使用しています:

   public class ContactsViewModel : ViewModelBase
    {
        private static IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
        private List<SavedContact> _SavedContactsList;
        public ContactsViewModel()
        {

            Contacts cons = new Contacts();
            cons.SearchAsync(String.Empty, FilterKind.None, null);
            cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(OnContactSearchCompleted);
            SaveSavedContactsCommand = new RelayCommand(OnSaveSavedContacts);
        }


        private List<SavedContact> GetSavedContacts()
        {

            if (appSettings.Contains("SavedContacts"))
            {
                var savedContacts = (List<SavedContact>)appSettings["SavedContacts"];
                return savedContacts;
            }
            else
            {
                return new List<SavedContact>();
            }
        }
        public RelayCommand SaveSavedContactsCommand { get; set; }

        private void OnSaveSavedContacts()
        {
            if (!SavedContactsList.Any(x => x.IsSelected == true))
            {
                MessageBox.Show("Please select some contacts and then click on Save button.");
            }
            else
            {
                var selectedSavedContacts = SavedContactsList.Where(x => x.IsSelected == true).ToList();
                SavedContacts = selectedSavedContacts;
                MessageBox.Show("Emergency contact list added successfully.");

                App.RootFrame.GoBack();
            }
        }

        void OnContactSearchCompleted(object sender, ContactsSearchEventArgs e)
        {
            try
            {
                SavedContactsList = new List<SavedContact>();
                var allContacts = new List<Contact>(e.Results.Where(x => x.PhoneNumbers.Count() > 0).OrderBy(c => c.DisplayName));
                // var savedContacts = GetSavedContacts();
                var savedContacts = SavedContacts;
                foreach (Contact contact in allContacts)
                {
                    SavedContact SavedContact = new SavedContact() { Contact = contact };
                    if (savedContacts.Any(x => x.Contact.PhoneNumbers.ElementAt(0).PhoneNumber == contact.PhoneNumbers.ElementAt(0).PhoneNumber))
                    {
                        SavedContact.IsSelected = true;
                    }
                    else
                    {
                        SavedContact.IsSelected = false;
                    }
                    SavedContactsList.Add(SavedContact);
                }

            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error in retrieving contacts : " + ex.Message);
            }

        }

        [DataMember]
        public List<SavedContact> SavedContactsList
        {
            get { return _SavedContactsList; }
            set
            {
                _SavedContactsList = value;
                RaisePropertyChanged("SavedContactsList");
            }
        }
        private List<SavedContact> SavedContacts
        {
            get
            {
                if (appSettings.Contains("SavedContacts"))
                {
                    var savedContacts = (List<SavedContact>)appSettings["SavedContacts"];
                    return savedContacts;
                }
                else
                {
                    return new List<SavedContact>();
                }
            }
            set
            {
                appSettings["SavedContacts"] = value;
            }
        }


    }

クラス SavedContact は次のとおりです。

  [DataContract]
    public class SavedContact : INotifyPropertyChanged
    {
        public SavedContact() { }
        private bool _isSelected;
        private Contact _contact;

        [DataMember]
        public bool IsSelected
        {
            get
            {
                return _isSelected;
            }
            set
            {
                if (_isSelected != value)
                {
                    _isSelected = value;
                    OnPropertyChanged("IsSelected");
                }

            }
        }

        [DataMember]
        public Contact Contact
        {
            get
            {
                return _contact;
            }
            set
            {
                if (_contact != value)
                {
                    _contact = value;
                    OnPropertyChanged("Contact");
                }

            }
        }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

ビュー モデルが にバインドされている場所<toolKit: LongListMultiSelector />。機能は、ロングリスト マルチセレクターからいくつかの連絡先を選択し、後で再利用するためにストレージに保存するようなものです。しかし、アプリを終了して再起動すると、savedContacts は null を返します。アプリで他のページをナビゲートしているときに、savedContacts が印刷されます。初めてリストを保存すると、アプリの再起動時に GetSavedContacts() が新しいリストを返します。

4

2 に答える 2

1

この問題は、IsolatedStorageSettingsまたはあなたのRelayCommand. さらに詳しく見ると、問題はオブジェクトのシリアル化と逆シリアル化にありContactます。以下の例のように実装を更新すれば、問題ないはずです。

public class ContactsViewModel : ViewModelBase
{
    private static IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

    private List<UserContact> _contactList;

    public ContactsViewModel()
    {

        Contacts cons = new Contacts();
        cons.SearchAsync(String.Empty, FilterKind.None, null);
        cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(OnContactSearchCompleted);
        SaveSavedContactsCommand = new RelayCommand(OnSaveSavedContacts);
    }

    public RelayCommand SaveSavedContactsCommand { get; private set; }

    private void OnSaveSavedContacts()
    {
        if (!Contacts.Any(x => x.IsSelected == true))
        {
            MessageBox.Show("Please select some contacts and then click on Save button.");
        }
        else
        {
            var selectedSavedContacts = Contacts.Where(x => x.IsSelected == true).Select(x => new SavedContact{ Name = x.Contact.DisplayName, PhoneNumber = x.Contact.PhoneNumbers.ElementAt(0).PhoneNumber}).ToList();
            SavedContacts = selectedSavedContacts;
            MessageBox.Show("Emergency contact list added successfully.");

            App.RootFrame.GoBack();
        }
    }

    void OnContactSearchCompleted(object sender, ContactsSearchEventArgs e)
    {
        try
        {
            Contacts = new List<UserContact>();
            var allContacts = new List<Contact>(e.Results.Where(x => x.PhoneNumbers.Count() > 0).OrderBy(c => c.DisplayName));

            foreach (Contact contact in allContacts)
            {
                UserContact SavedContact = new UserContact() { Contact = contact };
                if (SavedContacts.Any(x => x.PhoneNumber == contact.PhoneNumbers.ElementAt(0).PhoneNumber))
                {
                    SavedContact.IsSelected = true;
                }
                else
                {
                    SavedContact.IsSelected = false;
                }
                Contacts.Add(SavedContact);
            }

        }
        catch (System.Exception ex)
        {
            MessageBox.Show("Error in retrieving contacts : " + ex.Message);
        }

    }

    [DataMember]
    public List<UserContact> Contacts
    {
        get { return _contactList; }
        set
        {
            _contactList = value;
            RaisePropertyChanged("Contacts");
        }
    }

    public List<SavedContact> SavedContacts
    {
        get
        {
            if (appSettings.Contains("SavedContacts"))
            {
                var savedContacts = (List<SavedContact>)IsolatedStorageSettings.ApplicationSettings["SavedContacts"];
                return savedContacts;
            }
            else
            {
                return new List<SavedContact>();
            }
        }
        set
        {
            if (value != null)
            {
                IsolatedStorageSettings.ApplicationSettings["SavedContacts"] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }
    }
}

public class SavedContact
{
    public string Name { get; set; }

    public string PhoneNumber { get; set; }
}

[DataContract]
public class UserContact : INotifyPropertyChanged
{
    public UserContact() { }
    private bool _isSelected;
    private Contact _contact;

    [DataMember]
    public bool IsSelected
    {
        get
        {
            return _isSelected;
        }
        set
        {
            if (_isSelected != value)
            {
                _isSelected = value;
                OnPropertyChanged("IsSelected");
            }

        }
    }

    [DataMember]
    public Contact Contact
    {
        get
        {
            return _contact;
        }
        set
        {
            if (_contact != value)
            {
                _contact = value;
                OnPropertyChanged("Contact");
            }

        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
于 2013-10-13T15:21:47.073 に答える