Thread Error on Button Click
I'm on MVVM AvaloniaUI EF SQLite. I need to update a boolean from an entity based on a selected item in a combobox.
I'm getting,
After reaching,
I'm getting,
throw new InvalidOperationException("Call from invalid thread"); once the "+" button is clicked.throw new InvalidOperationException("Call from invalid thread"); once the "+" button is clicked.After reaching,
if (!String.IsNullOrWhiteSpace(emulatorName))if (!String.IsNullOrWhiteSpace(emulatorName)), the process halts. emulatorNameemulatorName comes with a value.<ComboBox SelectedItem="{Binding SelectorDomain.SelectorDTO.Name}"/>
<Button Content="+" Command="{Binding SelectorVM.AddEmulatorCommand}"/><ComboBox SelectedItem="{Binding SelectorDomain.SelectorDTO.Name}"/>
<Button Content="+" Command="{Binding SelectorVM.AddEmulatorCommand}"/>c#
public ViewModel(){
var canAddEmulator = this.WhenAnyValue(vm => vm.SelectorDomain.SelectorDTO.Name).Select(name => name != "Select an Emulator");
AddEmulatorCommand = ReactiveCommand.Create(AddEmulator, canAddEmulator);
}
private async void AddEmulator()
{
try
{
String selectedEmulator = SelectorDomain.SelectorDTO.Name?.Trim();
if (String.IsNullOrWhiteSpace(selectedEmulator))
return;
String unformattedEmulator = NameFormatter.UnformatEmulatorName(selectedEmulator);
await SelectorDomain.SelectorModel.SelectEmulatorAsync(unformattedEmulator);
}
catch (Exception ex)
{
Debug.WriteLine($"Exception in AddEmulator: {ex}");
}
}c#
public ViewModel(){
var canAddEmulator = this.WhenAnyValue(vm => vm.SelectorDomain.SelectorDTO.Name).Select(name => name != "Select an Emulator");
AddEmulatorCommand = ReactiveCommand.Create(AddEmulator, canAddEmulator);
}
private async void AddEmulator()
{
try
{
String selectedEmulator = SelectorDomain.SelectorDTO.Name?.Trim();
if (String.IsNullOrWhiteSpace(selectedEmulator))
return;
String unformattedEmulator = NameFormatter.UnformatEmulatorName(selectedEmulator);
await SelectorDomain.SelectorModel.SelectEmulatorAsync(unformattedEmulator);
}
catch (Exception ex)
{
Debug.WriteLine($"Exception in AddEmulator: {ex}");
}
}c#
public async Task SelectEmulatorAsync(string emulatorName)
{
if (!String.IsNullOrWhiteSpace(emulatorName))
{
emulatorName = emulatorName.Trim();
var emulator = await Context.Emulators
.Where(e => e.Name.Trim() == emulatorName)
.FirstOrDefaultAsync();
if (emulator != null)
{
await Context.SaveChangesAsync();
}
}
}
}c#
public async Task SelectEmulatorAsync(string emulatorName)
{
if (!String.IsNullOrWhiteSpace(emulatorName))
{
emulatorName = emulatorName.Trim();
var emulator = await Context.Emulators
.Where(e => e.Name.Trim() == emulatorName)
.FirstOrDefaultAsync();
if (emulator != null)
{
await Context.SaveChangesAsync();
}
}
}
}