也许我在这里遗漏了一些非常简单的东西,但无论如何我都会问......
我正在使用 Xamarin 表单(.NET 标准项目)、MVVMLight、Realm DB 和 ZXing Barcode Scanner。
我有一个像这样的领域对象......
public class Participant : RealmObject
{
public string FirstName {get; set;}
public string LastName {get; set;}
public string Email {get; set;}
public string RegistrationCode {get; set;}
//More properties skipped out for brevity
}
我有相应的视图模型如下:
public class ParticipantViewModel
{
Realm RealmInstance
public ParticipantViewModel()
{
RealmInstance = Realms.Realm.GetInstance();
RefreshParticipants();
}
private async Task RefreshParticipants()
{
//I have code here that GETS the list of Participants from an API and saves to the device.
//I am using the above-defined RealmInstance to save to IQueryable<Participant> Participants
}
}
以上所有工作都很好,我对此没有任何问题。在同一个视图模型中,我还能够启动 ZXing 扫描仪并扫描代表 RegistrationCode 的条形码。
这反过来会在扫描后填充以下属性(也在视图模型中)......
private ZXing.Result result;
public ZXing.Result Result
{
get { return result; }
set { Set(() => Result, ref result, value); }
}
并调用以下方法(通过 ScanResultCommand 连接)以获取带有扫描的 RegistrationCode 的参与者。
private async Task ScanResults()
{
if (Result != null && !String.IsNullOrWhiteSpace(Result.Text))
{
string regCode = Result.Text;
await CloseScanner();
SelectedParticipant = Participants.FirstOrDefault(p => p.RegistrationCode.Equals(regCode, StringComparison.OrdinalIgnoreCase));
if (SelectedParticipant != null)
{
//Show details for the scanned Participant with regCode
}
else
{
//Display not found message
}
}
}
我不断收到以下错误....
System.Exception:从不正确的线程访问的领域。
由下面的行生成....
SelectedParticipant = Participants.FirstOrDefault(p => p.RegistrationCode.Equals(regCode, StringComparison.OrdinalIgnoreCase));
我不确定这是一个不正确的线程,但是任何关于如何从已经填充的 IQueryable 或直接从 Realm 表示中获取扫描参与者的想法将不胜感激。
谢谢