Uwp 序列化联系人时出现奇怪行为。请使用JSON.NET在Win 10 Mobile上联系
在UWP应用程序的代码隐藏页面中,我正在序列化类型为Uwp 序列化联系人时出现奇怪行为。请使用JSON.NET在Win 10 Mobile上联系,uwp,win-universal-app,windows-10-universal,Uwp,Win Universal App,Windows 10 Universal,在UWP应用程序的代码隐藏页面中,我正在序列化类型为Windows.ApplicationModel.Contacts.Contact的对象,并使用方法ProtocolForResultsOperation.ReportCompleted(ValueSet)(最初启动的应用程序是另一个应用程序)将ValueSet对象中的结果发送回另一个应用程序 代码如下: protected override async void OnNavigatedTo(NavigationEventArgs e)
Windows.ApplicationModel.Contacts.Contact
的对象,并使用方法ProtocolForResultsOperation.ReportCompleted(ValueSet)
(最初启动的应用程序是另一个应用程序)将ValueSet对象中的结果发送回另一个应用程序
代码如下:
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
var protocolForResultsArgs = e.Parameter as ProtocolForResultsActivatedEventArgs;
if (protocolForResultsArgs != null)
{
var contactPicker = new Windows.ApplicationModel.Contacts.ContactPicker();
Contact contact = await contactPicker.PickContactAsync();
if (contact != null)
{
string c = JsonConvert.SerializeObject(contact);
ValueSet result = new ValueSet();
result.Add("ContactTaken", c);
protocolForResultsArgs.ProtocolForResultsOperation.ReportCompleted(result);
}
else
{
ValueSet result = new ValueSet();
result.Add("ContactTaken", "No contact selected");
protocolForResultsArgs.ProtocolForResultsOperation.ReportCompleted(result);
}
}
}
它可以在PC上正常工作。但当我在装有Windows10Mobile(Lumia640)的移动设备上运行它时,它崩溃了
即使我返回的不是序列化的JSON字符串,它也会崩溃,即它也会崩溃,如下所示:
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
var protocolForResultsArgs = e.Parameter as ProtocolForResultsActivatedEventArgs;
if (protocolForResultsArgs != null)
{
var contactPicker = new Windows.ApplicationModel.Contacts.ContactPicker();
Contact contact = await contactPicker.PickContactAsync();
if (contact != null)
{
string c = JsonConvert.SerializeObject(contact);
ValueSet result = new ValueSet();
result.Add("ContactTaken", "hello world");
protocolForResultsArgs.ProtocolForResultsOperation.ReportCompleted(result);
}
else
{
ValueSet result = new ValueSet();
result.Add("ContactTaken", "No contact selected");
protocolForResultsArgs.ProtocolForResultsOperation.ReportCompleted(result);
}
}
}
因此,我认为,由于某种原因,它可能无法序列化给定对象。因此,在将序列化结果返回给调用方应用程序之前,我尝试在应用程序中显示序列化结果,如下所示:
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
var protocolForResultsArgs = e.Parameter as ProtocolForResultsActivatedEventArgs;
if (protocolForResultsArgs != null)
{
var contactPicker = new Windows.ApplicationModel.Contacts.ContactPicker();
Contact contact = await contactPicker.PickContactAsync();
if (contact != null)
{
string c = JsonConvert.SerializeObject(contact);
textBlock.text = c;
}
else
{
ValueSet result = new ValueSet();
result.Add("ContactTaken", "No contact selected");
protocolForResultsArgs.ProtocolForResultsOperation.ReportCompleted(result);
}
}
}
序列化文本显示正确
有什么解释吗?是的,应该输入值到。对不起?你的确切意思是什么?嗨,我试着测试你的代码,它工作得很好,但不是使用
Json.NET
,而是使用Newtonsoft.Json
,你可以尝试用这个nuget包序列化它。此外,我的软件最低平台是14393,目标平台是15063,测试设备是Lumia 950(操作系统构建:15254)。是的,我的意思是Newtonsoft.Json
,它们是相同的,即Json.NET
已经变成Newtonsoft.Json
。您是否尝试由另一个应用程序启动该应用程序并返回序列化的JSON,还是仅在应用程序内部尝试序列化?是的,我创建了两个应用程序,一个应用程序通过启动器唤醒另一个应用程序。LaunchUriForResultsAsync()
并接收从另一个应用程序传递的JSON数据(读取联系人),您可以提供测试设备的系统版本详细信息吗?