我在我的ModelBase類中實現INotifyPropertyChanged,以便我的所有派生類都可以訪問INotifyPropertyChanged方法和事件。不序列化Caliburn.Micro IsNotifying屬性
我在我的項目中使用Caliburn.Micro,所以我通過在IModelBase接口中實現INotifyPropertyChangedEx,然後在ModelBase類中擴展PropertyChangedBase來做到這一點。
這一切都很好,除了PropertyChangedBase的IsNotifying屬性被我的模型序列化。我嘗試了一些東西,但一直無法停止序列化。
我試着在ModelBase中重寫IsNotifying並將[XmlIgnore]添加到屬性中。我也嘗試通過在ModelBase類中使用new關鍵字來隱藏IsNotifying。這些都沒有奏效。
我從github複製了PropertyChangedBase代碼,將它放到我自己的PropertyChangedBase類中,然後將[XmlIgnore]添加到IsNotifying屬性中。這工作,但並不理想。
任何想法?這可以做到嗎?我應該使用Caliburn.Micro PropertyChangedBase廢止並實施我自己的?實現INotifyPropertyChanged並不困難。我只是試圖使用Caliburn.Micro中的一個,因爲我已經在使用該庫。
這是一個簡單的例子是,XML寫入控制檯
using System;
using System.IO;
using System.Xml.Serialization;
using Caliburn.Micro;
namespace CaliburnPropertyChangedBase
{
internal class Program
{
private static void Main()
{
var myModel = new MyModel {SomeProperty = "Test"};
Console.WriteLine(myModel.SerializeObject());
Console.ReadKey();
}
}
public static class XmlHelper
{
public static string SerializeObject<T>(this T toSerialize)
{
var xmlSerializer = new XmlSerializer(toSerialize.GetType());
using (var textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, toSerialize);
return textWriter.ToString();
}
}
}
public interface IModelBase : INotifyPropertyChangedEx
{
}
public class ModelBase : PropertyChangedBase, IModelBase
{
}
public interface IMyModel : IModelBase
{
string SomeProperty { get; set; }
}
public class MyModel : ModelBase, IMyModel
{
public string SomeProperty { get; set; }
}
}
這是輸出
<?xml version="1.0" encoding="utf-16"?>
<MyModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<IsNotifying>true</IsNotifying>
<SomeProperty>Test</SomeProperty>
</MyModel>
作爲一個說明,但事實上,它的序列化屬性不是一列火車粉碎。它不會對我的應用程序造成任何問題。這是不正確的。如果有其他人使用我們的XML,我們將不得不告訴他們忽略該屬性。 – Marc