暂停的DelegateCommand序列化的UWP失败

时间:2017-10-06 01:27:03

标签: c# uwp prism

我有一个视图和视图模型,代码是

<prism:SessionStateAwarePage
    x:Class="MyApp.Views.SettingPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyApp.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:prism="using:Prism.Windows.Mvvm"
    prism:ViewModelLocator.AutoWireViewModel="True"
    mc:Ignorable="d">

    <RelativePanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="10,0,0,0">
        <TextBox x:Name="Box1" RelativePanel.AlignTopWithPanel="True" Text="{Binding Path=Text1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
        <TextBox x:Name="Box2" RelativePanel.Below="Box1" Text="{Binding Path=Text2, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
        <Button RelativePanel.Below="Box2" Command="{Binding Path=ButtonClickedCommand}">Click me</Button>
    </RelativePanel>
</prism:SessionStateAwarePage>

并查看模型

public class SettingPageViewModel : ViewModelBase
{
    private readonly ISessionStateService sessionService;
    private string text1;
    private string text2;

    public SettingPageViewModel(ISessionStateService sessionService)
    {
        this.ButtonClickedCommand = new DelegateCommand<ItemClickEventArgs>(this.OnButtonClicked, this.CanButtonClick);
        this.sessionService = sessionService;
    }

    [RestorableState]
    public string Text1
    {
        get
        {
            return this.text1;
        }

        set
        {
            this.SetProperty(ref this.text1, value);
        }
    }

    [RestorableState]
    public string Text2
    {
        get
        {
            return this.text2;
        }

        set
        {
            this.SetProperty(ref this.text2, value);
        }
    }

    public ICommand ButtonClickedCommand { get; private set; }

    public override void OnNavigatedTo(NavigatedToEventArgs e, Dictionary<string, object> viewModelState)
    {
        base.OnNavigatedTo(e, viewModelState);
    }

    private void OnButtonClicked(ItemClickEventArgs argument)
    {
        this.Text1 = "The value is set by button click";
    }

    private bool CanButtonClick(ItemClickEventArgs argument)
    {
        return true;
    }
}

当我在VS2017中启动它并选择生命周期事件时 - &gt;暂停我有异常

  

{System.Runtime.Serialization.InvalidDataContractException:Type   &#39; Prism.Commands.DelegateCommand`1 [Windows.UI.Xaml.Controls.ItemClickEventArgs]&#39;   无法序列化。考虑用它标记它   DataContractAttribute属性,并标记其所有成员   想要使用DataMemberAttribute属性进行序列化。或者,   您可以确保该类型是公共的并且具有无参数   构造函数 - 然后将序列化该类型的所有公共成员,   并且不需要任何属性。在   System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(字符串   消息,类型类型)at   System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(的Int32   id,RuntimeTypeHandle typeHandle,Type type)at   System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(的Int32   id,RuntimeTypeHandle typeHandle,Type type)at   System.Runtime.Serialization.DataContract.GetDataContract(的RuntimeTypeHandle   typeHandle,Type type,SerializationMode mode)at   System.Runtime.Serialization.XmlObjectSerializerContext.GetDataContract(的RuntimeTypeHandle   typeHandle,Type type)at   System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator   xmlWriter,Object obj,RuntimeTypeHandle objectTypeHandle,Type   objectType,Int32 declaredTypeID,RuntimeTypeHandle   declaredTypeHandle,Type declaredType)at   System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator   xmlWriter,Object obj,Boolean isDeclaredType,Boolean writeXsiType,   Int32 declaredTypeID,RuntimeTypeHandle declaredTypeHandle)at   WriteKeyValueOfstringanyTypeToXml(XmlWriterDelegator,Object,   XmlObjectSerializerWriteContext,ClassDataContract)at   System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator   xmlWriter,Object obj,XmlObjectSerializerWriteContext context)at   WriteArrayOfKeyValueOfstringanyTypeToXml(XmlWriterDelegator,Object,   XmlObjectSerializerWriteContext,CollectionDataContract)at   System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator   xmlWriter,Object obj,XmlObjectSerializerWriteContext context)at   System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract   dataContract,XmlWriterDelegator xmlWriter,Object obj,   RuntimeTypeHandle declaredTypeHandle)at   System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract   dataContract,XmlWriterDelegator xmlWriter,Object obj,Boolean   verifyKnownType,RuntimeTypeHandle declaredTypeHandle,Type   declaredType)at   System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator   xmlWriter,Object obj,RuntimeTypeHandle objectTypeHandle,Type   objectType,Int32 declaredTypeID,RuntimeTypeHandle   declaredTypeHandle,Type declaredType)at   System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator   xmlWriter,Object obj,Boolean isDeclaredType,Boolean writeXsiType,   Int32 declaredTypeID,RuntimeTypeHandle declaredTypeHandle)at   WriteKeyValueOfstringanyTypeToXml(XmlWriterDelegator,Object,   XmlObjectSerializerWriteContext,ClassDataContract)at   System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator   xmlWriter,Object obj,XmlObjectSerializerWriteContext context)at   WriteArrayOfKeyValueOfstringanyTypeToXml(XmlWriterDelegator,Object,   XmlObjectSerializerWriteContext,CollectionDataContract)at   System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator   xmlWriter,Object obj,XmlObjectSerializerWriteContext context)at   System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract   dataContract,XmlWriterDelegator xmlWriter,Object obj,   RuntimeTypeHandle declaredTypeHandle)at   System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract   dataContract,XmlWriterDelegator xmlWriter,Object obj,Boolean   verifyKnownType,RuntimeTypeHandle declaredTypeHandle,Type   declaredType)at   System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator   xmlWriter,Object obj,RuntimeTypeHandle objectTypeHandle,Type   objectType,Int32 declaredTypeID,RuntimeTypeHandle   declaredTypeHandle,Type declaredType)at   System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator   xmlWriter,Object obj,Boolean isDeclaredType,Boolean writeXsiType,   Int32 declaredTypeID,RuntimeTypeHandle declaredTypeHandle)at   WriteKeyValueOfstringanyTypeToXml(XmlWriterDelegator,Object,   XmlObjectSerializerWriteContext,ClassDataContract)at   System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator   xmlWriter,Object obj,XmlObjectSerializerWriteContext context)at   WriteArrayOfKeyValueOfstringanyTypeToXml(XmlWriterDelegator,Object,   XmlObjectSerializerWriteContext,CollectionDataContract)at   System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator   xmlWriter,Object obj,XmlObjectSerializerWriteContext context)at   System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract   dataContract,XmlWriterDelegator xmlWriter,Object obj,   RuntimeTypeHandle declaredTypeHandle)at   System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract   dataContract,XmlWriterDelegator xmlWriter,Object obj,   RuntimeTypeHandle declaredTypeHandle)at   System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator   writer,Object graph,DataContractResolver dataContractResolver)at   System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator   writer,Object graph,DataContractResolver dataContractResolver)at   System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator   writer,Object graph,DataContractResolver dataContractResolver)at   System.Runtime.Serialization.XmlObjectSerializer.WriteObject(的XmlDictionaryWriter   作家,对象图)   System.Runtime.Serialization.XmlObjectSerializer.WriteObject(流   流,对象图)   Prism.Windows.AppModel.SessionStateService.d__5.MoveNext()}

但我还没有将事件标记为[RestorableState]。有什么问题?

0 个答案:

没有答案