private void SetAddressBookAndUpdateViewModel(AddressBook addressBook) { Entries = new ObservableCollection<EntryViewModel>(); foreach (AddressBookEntry abe in addressBook.Entries) { EntryViewModel evm = new EntryViewModel(); evm.Source = abe; foreach (EntryDataItem edi in abe.Data) { EntryDataItemViewModel edivm = new EntryDataItemViewModel(evm); edivm.IsEnabled = edi.IsEnabled; edivm.Source = edi; edivm.Editor = CustomDataEditors.CustomDataEditorCache.CreateTypeEditor(edi.Identifier); if (edivm.Editor != null) { try { edivm.Editor.Value = edi.Data; } catch (Exception ex) { Logger.Instance.LogFormat(LogType.Error, this, Properties.Resources.ErrorWhileLoadingDataItemValue, edi.Identifier); Logger.Instance.LogException(this, ex); } } evm.DataItems.Add(edivm); } Entries.Add(evm); } }
private AddressBook CreateAddressBookWithOneEntryAndOneDataItem(bool dataItemIsEnabled) { AddressBook addressBook = new AddressBook(); AddressBookEntry entry = new AddressBookEntry(); entry.FirstName = "John"; entry.LastName = "Doe"; MailAddressEntryObject data = new MailAddressEntryObject() { Address = new MailAddress("*****@*****.**") }; entry.Data.Add(new EntryDataItem() { Data = data, Identifier = MailEntryObjectTypeIdentifier, IsEnabled = dataItemIsEnabled }); addressBook.Entries.Add(entry); return addressBook; }
private AddressBook CompileAddressBookFromViewModel() { AddressBook addressBook = new AddressBook(); foreach (EntryViewModel evm in this.Entries) { AddressBookEntry abe = new AddressBookEntry(); abe.FirstName = evm.FirstName; abe.LastName = evm.LastName; foreach (EntryDataItemViewModel edivm in evm.DataItems) { EntryDataItem edi = new EntryDataItem(); // Decide which value to use if (edivm.Editor != null) { edi.IsEnabled = edivm.IsEnabled; edi.Identifier = CustomDataEditors.CustomDataEditorCache.GetTypeEditorIdentifier(edivm.Editor.GetType()); edi.Data = edivm.Editor.Value; // If there is no data available, skip this entry. if (edi.Data == null) { continue; } } else { if (edivm.Source != null) { edi = edivm.Source; } // TODO: What happens if there is no editor, AND no value? continue; } abe.Data.Add(edi); } addressBook.Entries.Add(abe); } return addressBook; }
public void SimpleConversionTest() { string sourceValue = null; // Create and convert { AddressBook addressBook = CreateAddressBookWithOneEntryAndOneDataItem(true); sourceValue = ((IStringSettingConvertible)addressBook).ConvertBack(); } // Convert to address book and check values { AddressBook addressBook = new AddressBook(); ((IStringSettingConvertible)addressBook).Convert(sourceValue); Assert.AreEqual(1, addressBook.Entries.Count); Assert.AreEqual(1, addressBook.Entries[0].Data.Count); Assert.AreEqual(MailEntryObjectTypeIdentifier, addressBook.Entries[0].Data[0].Identifier); Assert.AreEqual(true, addressBook.Entries[0].Data[0].IsEnabled); Assert.AreEqual(typeof(MailAddressEntryObject), addressBook.Entries[0].Data[0].Data.GetType()); } }
/// <summary> /// Initializes a new instance of the <see cref="AddressBookViewModel"/> class. /// </summary> /// <param name="addressBook"></param> public AddressBookViewModel(AddressBook addressBook) { AddressBookEditWrapper = addressBook; EnableLiveShaping(); }
/// <summary> /// Initializes a new instance of the <see cref="AddressBookViewModel"/> class. /// </summary> /// <param name="addressBook"></param> public AddressBookViewModel(AddressBook addressBook) { AddressBookEditWrapper = addressBook; }