序列化Json添加“InitializeTask”:null元素

问题描述 投票:-1回答:1

更新:它是由MvxViewModel引起的。改为MvxNotifyPropertyChanged解决了它(因为它实际上是我想要的)!

当我将Json序列化为我的CategoriesJson类型时,它会添加一个“InitializeTask”:null元素。那么,我的CategoriesJson代码:

public class CategoriesJson
{
    public CategoriesJson()
    {
        Categories = new List<CategoryViewModel>();
    }
    public List<CategoryViewModel> Categories { get; set; }
}

CategoryViewModel在这里:

public class CategoryViewModel : MvxViewModel
        , IHandle<CategorySelectedEvent>
        , IHandle<MainMonthChangedEvent>
    {
        private IEventAggregator events;
        private IConfigurationReader configurationReader;

        public CategoryViewModel()
        {
            Transfers = new List<Transfer>();
        }

        private MvxCommand addTransferClicked;
        [JsonIgnore]
        public IMvxCommand AddTransferClicked => addTransferClicked;

        public Guid Id { get; set; }
        public string Name { get; set; }
        public List<Transfer> Transfers { get; set; }
        [JsonIgnore]
        public string TotalAmount => $"{GetTotalAmount()}";

        private Guid selectedMainMonthId;

        [JsonIgnore]
        public Guid SelectedMainMonthId
        {
            get => selectedMainMonthId;
            set
            {
                if (SetProperty(ref selectedMainMonthId, value))
                {
                    Transfers = configurationReader.ReadTransfers(Id, value);
                    RaisePropertyChanged(nameof(TotalAmount));
                }

            }
        }

        [JsonIgnore]
        private string transferAmount;
        [JsonIgnore]
        public string TransferAmount
        {
            get => transferAmount;
            set => SetProperty(ref transferAmount, value);
        }

        [JsonIgnore]
        private bool isExpanded;
        [JsonIgnore]
        public bool IsExpanded
        {
            get { return isExpanded; }
            set => SetProperty(ref isExpanded, value);
        }

        //public List<MonthViewModel> Months => configurationReader.ReadMonths();
        [JsonIgnore]
        private MonthViewModel selectedMonth;

        [JsonIgnore]
        public MonthViewModel SelectedMonth
        {
            get { return selectedMonth; }
            set => SetProperty(ref selectedMonth, value);
        }

        public string GetTotalAmount()
        {
            double amount = 0;
            foreach (var transfer in Transfers)
            {
                amount += transfer.Amount;
            }

            return $"£ {amount}";
        }

        public void Handle(CategorySelectedEvent message)
        {
            if (Id != message.Id)
                return;

            IsExpanded = true;
        }

        private void AddTransfer()
        {
            var parsed = Double.TryParse(TransferAmount, out double amountResult);
            if (!parsed)
                return;

            configurationReader.AddTransfer(Id, amountResult, SelectedMonth.Id, DateTime.Now, "notes here");
            TransferAmount = String.Empty;
            IsExpanded = false;
        }

        public void Handle(MainMonthChangedEvent message)
        {
            SelectedMainMonthId = message.Id;
        }

        public override void ViewAppearing()
        {
            events = Mvx.Resolve<IEventAggregator>();

            events.Subscribe(this);
            configurationReader = Mvx.Resolve<IConfigurationReader>();
            Transfers = new List<Transfer>();
            addTransferClicked = new MvxCommand(AddTransfer);
            base.ViewAppearing();
        }

我将JsonIgnore标记放在我不想序列化的所有属性和字段中 - 这没有帮助。所以,当我序列化Json时,我得到了,例如:

{"Categories":[{"Id":"2d48427c-c170-4932-96e9-7668418ba008","Name":"test","Transfers":[],"InitializeTask":null}]}

我不明白InitializeTask来自哪里..它之前没有,但是在我添加了一些代码后,它开始出现,我无法追溯到我的代码的早期版本,看看是什么导致它。

先感谢您!

c# json
1个回答
0
投票

可能MvxViewModel基类具有该属性并导致问题。

© www.soinside.com 2019 - 2024. All rights reserved.