简体   繁体   English

C#桌面应用程序和Parse.com

[英]C# desktop app and Parse.com

I have a desktop application written in C# (not a windows 8 app) that is talking to the Parse database and there is no problem with creating a new account, logging in and transferring information to the user object. 我有一个用C#编写的桌面应用程序(不是Windows 8应用程序)正在与Parse数据库通信,创建新帐户,登录并将信息传输到用户对象没有问题。

However, when it comes to getting that information out,that is where the problem lies. 但是,当涉及到信息发布时,问题就出在这里。

This code works (edited) and returns the Credit value: 此代码有效(已编辑)并返回Credit值:

ParseQuery<ParseObject> query = ParseObject.GetQuery("User");
ParseObject CreditAmount = query.GetAsync("XcaSDFrGGhX");

int Credit = CreditAmount.Get<int>("Credit");

This code does not work: 此代码不起作用:

ParseQuery<ParseObject> query = ParseObject.GetQuery("User");
ParseObject CreditAmount = query.GetAsync(ParseUser.CurrentUser.ObjectID);

int Credit = CreditAmount.Get<int>("Credit");

An exception is thrown: Object with the given objectId found. 引发异常: Object with the given objectId found.

Parse.ParseException.ErrorCode.ObjectNotFound

suggesting the user objectID has been found but there is something wrong, Why? 提示已找到用户objectID,但是出了点问题,为什么?

Also, it would be more advantageous if the Parse.User could be declared as a global variable. 同样,如果可以将Parse.User声明为全局变量,则将更加有利。 How is this done as in the example above it's declared as a local Var within the sub? 如上例所示,如何将其声明为sub中的局部Var?

I don't know C#, but this is my take: 我不知道C#,但这是我的看法:

On the working code, you get a specific CreditAmount object (which is actually a User object), which has an objectId of "XcaSDFrGGhX". 在工作代码上,您将获得一个特定的CreditAmount对象(实际上是一个User对象),它的objectId为“ XcaSDFrGGhX”。

On the second, you try to query for a user object by using the objecId of the current user. 第二,您尝试使用当前用户的objecId查询用户对象。 So, what I think you are trying to do is to get the "Credit" field from the current user? 因此,我认为您要尝试的是从当前用户那里获取“信用”字段? If that is the case, you can use CurrentUser directly. 在这种情况下,您可以直接使用CurrentUser。 ParseUser.CurrentUser is always available to you, and should be able to get the property with something like like this: ParseUser.CurrentUser始终对您可用,并且应该能够通过类似以下方式获取属性:

int Credit = ParseUser.CurrentUser.Get<int>("Credit");

First off, from the code you have posted there's no sign that you are waiting for the Async task to finish. 首先,从您发布的代码来看,没有任何迹象表明您正在等待异步任务完成。 In C# you would use the await keyword so that the following lines of code are not executed until the async operation finishes. 在C#中,您将使用await关键字,以便在异步操作完成之前不执行以下代码行。

// NOTE: the 'await' keyword here after the '='
ParseObject user = await query.GetAsync(ParseUser.CurrentUser.ObjectID);
// this code will be delayed until the async-get is done
int credit = user.Get<int>("Credit");

Secondly, if you want to use the current user , the ParseUser.CurrentUser is already loaded. 其次,如果要使用当前用户 ,则ParseUser.CurrentUser已经加载。 You can tell it to update if needed using FetchAsync() but generally you just use it: 您可以使用FetchAsync()告诉它进行更新,但通常只使用它:

// no query needed
int credit = ParseUser.CurrentUser.Get<int>("Credit");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM