多分私はここで本当に単純なものを見逃しているかもしれませんが、とにかく尋ねます.....
Xamarin フォーム (.NET Standard プロジェクト)、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 = 参加者.FirstOrDefault(p => p.RegistrationCode.Equals(regCode, StringComparison.OrdinalIgnoreCase));
これがどのように間違ったスレッドであるかはわかりませんが、スキャンされた参加者を既に取り込まれた IQueryable から、または Realm 表現から直接フェッチする方法についてのアイデアは大歓迎です。
ありがとう