![](/img/trans.png)
[英]Custom Settings-class exception on first use (ApplicationSettingsBase)
[英]ApplicationSettingsBase writes empty tag for custom collection
我已經斷斷續續地與這個作斗爭了好幾天了。 我需要將一組自定義對象存儲為用戶設置的一部分。 基於大量谷歌工作,似乎從ApplicationSettingsBase
構建首選項 class 是一種合適的方法。 我遇到的問題是,一旦我嘗試存儲自定義類型的集合,就沒有為該屬性保存任何數據。 如果我保持 collections 的基本類型(例如字符串),則一切正常。 我有一個單獨的概念驗證項目,過去一天我一直在使用它來隔離這個問題。
該項目由 WPF window 組成,帶有一個列表框和兩個按鈕,“+”和“-”。 我有一個 Prefs class,其中三個屬性定義了不同類型的 collections。 在我的 window 代碼中,我將列表框綁定到這些列表之一,並且按鈕可以在列表中添加或刪除項目。 關閉 window 應該將列表的內容保存到當前用戶的 users.config 文件中。 重新打開它應該顯示列表的保存內容。
如果我使用 List1 ( ObservableCollection<string>
) 並單擊 + 按鈕幾次然后關閉 window 數據正確保存到 user.config。 但是,如果我更改並使用 List2( ObservableCollection<Foo>
) 或 List3( FooCollection
) 我只會得到一個空值標簽。 我試圖實現ISerializable
和IXmlSerializable
以試圖在不改變行為的情況下使其正常工作。 事實上,在帶斷點的調試模式下運行表明,當調用 save 方法時集合包含數據,但從不調用序列化接口方法。
我錯過了什么?
更新:我已經改變了我的方法,暫時將數據寫入 user.config 旁邊的另一個文件,以便我可以在應用程序的其他部分取得一些進展。 但我仍然想知道為什么應用程序設置庫無法記錄我的收集數據。
這是所有相關代碼,如果有人認為省略的部分很重要,我會將其添加回帖子中。
向每個列表屬性添加幾個元素后的 user.config
<setting name="List1" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>0</string>
<string>1</string>
<string>2</string>
</ArrayOfString>
</value>
</setting>
<setting name="List2" serializeAs="Xml">
<value />
</setting>
<setting name="List3" serializeAs="Xml">
<value />
</setting>
Window XAML
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox Name="l1" Grid.Column="0" Grid.Row="0" ItemsSource="{Binding}"></ListBox>
<StackPanel Grid.Column="1" Grid.Row="0">
<Button Name="bP" Margin="5" Padding="5" Click="bP_Click">+</Button>
<Button Name="bM" Margin="5" Padding="5" Click="bM_Click">-</Button>
</StackPanel>
</Grid>
Window 的代碼隱藏
public partial class Window1 : Window
{
Prefs Prefs = new Prefs();
public Window1()
{
InitializeComponent();
//l1.DataContext = Prefs.List1;
//l1.DataContext = Prefs.List2;
l1.DataContext = Prefs.List3;
}
private void Window_Closed(object sender, EventArgs e)
{
Prefs.Save();
}
private void bP_Click(object sender, RoutedEventArgs e)
{
//Prefs.List1.Add(Prefs.List1.Count.ToString());
//Prefs.List2.Add(new Foo(Prefs.List2.Count.ToString()));
Prefs.List3.Add(new Foo(Prefs.List3.Count.ToString()));
}
private void bM_Click(object sender, RoutedEventArgs e)
{
//Prefs.List1.RemoveAt(Prefs.List1.Count - 1);
//Prefs.List2.RemoveAt(Prefs.List2.Count - 1);
Prefs.List3.RemoveAt(Prefs.List3.Count - 1);
}
}
首選項 class
class Prefs : ApplicationSettingsBase
{
[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute(null)]
public System.Collections.ObjectModel.ObservableCollection<string> List1
{
get
{
System.Collections.ObjectModel.ObservableCollection<string> Value = this["List1"] as System.Collections.ObjectModel.ObservableCollection<string>;
if (Value == null)
{
Value = new System.Collections.ObjectModel.ObservableCollection<string>();
this["List1"] = Value;
}
return Value;
}
}
[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute(null)]
public System.Collections.ObjectModel.ObservableCollection<Foo> List2
{
get
{
System.Collections.ObjectModel.ObservableCollection<Foo> Value = this["List2"] as System.Collections.ObjectModel.ObservableCollection<Foo>;
if (Value == null)
{
Value = new System.Collections.ObjectModel.ObservableCollection<Foo>();
this["List2"] = Value;
}
return Value;
}
}
[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute(null)]
public FooCollection List3
{
get
{
FooCollection Value = this["List3"] as FooCollection;
if (Value == null)
{
Value = new FooCollection();
this["List3"] = Value;
}
return Value;
}
}
}
富 Class
[Serializable()]
class Foo : System.ComponentModel.INotifyPropertyChanged, ISerializable, IXmlSerializable
{
private string _Name;
private const string PropName_Name = "Name";
public string Name
{
get { return this._Name; }
set
{
if (value != this._Name)
{
this._Name = value;
RaisePropertyChanged(Foo.PropName_Name);
}
}
}
public override string ToString()
{
return Name;
}
public Foo() { }
public Foo(string name)
{
this._Name = name;
}
#region INotifyPropertyChanged Members
/***Omitted for space***/
#endregion
#region ISerializable Members
public Foo(SerializationInfo info, StreamingContext context)
{
this._Name = (string)info.GetValue(Foo.PropName_Name, typeof(string));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(Foo.PropName_Name, this._Name);
}
#endregion
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
reader.MoveToContent();
_Name = reader.GetAttribute(Foo.PropName_Name);
bool Empty = reader.IsEmptyElement;
reader.ReadStartElement();
if (!Empty)
{
reader.ReadEndElement();
}
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteAttributeString(Foo.PropName_Name, _Name);
}
#endregion
}
和 FooCollection class
[Serializable()]
class FooCollection : ICollection<Foo>, System.ComponentModel.INotifyPropertyChanged, INotifyCollectionChanged, ISerializable, IXmlSerializable
{
List<Foo> Items;
private const string PropName_Items = "Items";
public FooCollection()
{
Items = new List<Foo>();
}
public Foo this[int index]
{
/***Omitted for space***/
}
#region ICollection<Foo> Members
/***Omitted for space***/
#endregion
public void RemoveAt(int index)
{
/***Omitted for space***/
}
#region IEnumerable Members
/***Omitted for space***/
#endregion
#region INotifyCollectionChanged Members
/***Omitted for space***/
#endregion
#region INotifyPropertyChanged Members
/***Omitted for space***/
#endregion
#region ISerializable Members
public FooCollection(SerializationInfo info, StreamingContext context)
{
this.Items = (List<Foo>)info.GetValue(FooCollection.PropName_Items, typeof(List<Foo>));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(FooCollection.PropName_Items, this.Items);
}
#endregion
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
XmlSerializer FooSerializer = new XmlSerializer(typeof(Foo));
reader.MoveToContent();
bool Empty = reader.IsEmptyElement;
reader.ReadStartElement();
if (!Empty)
{
if (reader.IsStartElement(FooCollection.PropName_Items))
{
reader.ReadStartElement();
while (reader.IsStartElement("Foo"))
{
this.Items.Add((Foo)FooSerializer.Deserialize(reader));
}
reader.ReadEndElement();
}
reader.ReadEndElement();
}
}
public void WriteXml(System.Xml.XmlWriter writer)
{
XmlSerializer FooSerializer = new XmlSerializer(typeof(Foo));
writer.WriteStartElement(FooCollection.PropName_Items);
foreach (Foo Item in Items)
{
writer.WriteStartElement("Foo");
FooSerializer.Serialize(writer, Item);
writer.WriteEndElement();//"Foo"
}
writer.WriteEndElement(); //FooCollection.PropName_Items
}
#endregion
}
我也遇到了類似的問題,我設法解決了這個問題,但我沒有使用ObservableCollection
,而是使用普通的List<Column>
, Column
是我的 class ,它包含為公共成員:string、int 和 bool,它們是所有 xmlserializable。 List<>
,因為它實現了IEnumerable
也是XMLSerializable
。
在自定義 Foo Class 中,您唯一需要注意的是:您必須將成員設為公共、無參數構造函數,並且 class 本身必須是公共的。 您不需要為 xml 序列化添加[Serializable]
標簽。
我不需要實現 FooCollection class,因為我使用的 List 與 xml 序列化沒有問題。
另一件事:
ApplicationSettingsBase
的 class 可以內部密封 - 無需公開。在列表道具上方添加它是 xml 可序列化的事實:
[global::System.Configuration.UserScopedSettingAttribute()] [SettingsSerializeAs(SettingsSerializeAs.Xml)] [global::System.Configuration.DefaultSettingValueAttribute("")] public List Columns { get { return ((List)this["Columns" ]); } set { this["Columns"] = (List)value; } }
如果這不起作用,您還可以嘗試實現TypeConverter
。
我也為一個非常相似的問題苦苦掙扎了兩天,但現在我發現了一個可能對這里有所幫助的缺失鏈接。 如果您和我的情況實際上相似,那么“Foo”和“FooCollection”類需要明確公開!
我假設,在 List2( ObservableCollection<Foo>
) 和List3(FooCollection)
的情況下,.NET 會遇到IXmlSerializable
,這需要以某種方式進行顯式公共訪問(跨越自己程序集的邊界)。
相反的選項 List1 ( ObservableCollection<string>
)似乎在一個扁平的字符串類型轉換上運行,它對(內部)class 'Foo'感到滿意......
(來自 ApplicationsettingsBase 文檔: ApplicationSettingsBase
使用兩種主要機制來序列化設置:1)如果存在可以轉換為字符串和從字符串轉換的TypeConverter
,我們使用它。 2)如果沒有,我們回XmlSerializer
)
親切的問候,魯迪
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.