我开始自学 .net-core 和 Avalonia UI。
我启动了 Avalonia UI 教程,它按预期工作。http://www.avaloniaui.net/docs/tutorial/creating-model-viewmodel
但是大多数时候,您可以从教程中复制代码并且它可以工作,但是您不理解它......
现在我想,可以将“假数据库”更改为真实数据库。所以我开始包含 Microsoft.EmtityFrameworkCore。数据库存在,包含在内,并且可以编译和运行。
namespace Decksumme.Models
{
public class ParticipantsContext : DbContext
{
public DbSet<Participant> Participants { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=decksumme.db");
}
}
}
我更新了 ListViewModel
namespace Decksumme.ViewModels
{
public class DecksummeListViewModel : ViewModelBase
{
public DecksummeListViewModel(DbSet<Participant> participants)
{
Participants = new ObservableCollection<DbSet<Participant>>();
}
public ObservableCollection<DbSet<Participant>> Participants { get; }
}
}
编辑视图模型被增强并且编译没有错误
namespace Decksumme.ViewModels
{
class EditParticipantViewModel : ViewModelBase
{
string forename;
string name;
string discipline;
public EditParticipantViewModel()
{
var okEnabled = this.WhenAnyValue(
x => x.forename,
x => !string.IsNullOrWhiteSpace(x)
);
Ok = ReactiveCommand.Create(
() => new Participant
{
Forename = Forename,
Name = Name,
Discipline = Discipline,
},
okEnabled
);
Cancel = ReactiveCommand.Create(() => { });
}
public string Forename {
get => forename;
set => this.RaiseAndSetIfChanged(ref forename, value);
}
public string Name {
get => name;
set => this.RaiseAndSetIfChanged(ref name, value);
}
public string Discipline {
get => discipline;
set => this.RaiseAndSetIfChanged(ref discipline, value);
}
public string Results { get; set; }
public ReactiveCommand<Unit, Participant> Ok { get; }
public ReactiveCommand<Unit, Unit> Cancel { get; }
}
}
现在我失去了一点,MainWondowViewModel。
namespace Decksumme.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
ViewModelBase content;
public MainWindowViewModel(ParticipantsContext db)
{
Content = List = new DecksummeListViewModel(db.Participants);
}
public ViewModelBase Content
{
get => content;
private set => this.RaiseAndSetIfChanged(ref content, value);
}
public DecksummeListViewModel List { get; }
public void AddParticipant()
{
var vm = new EditParticipantViewModel();
Observable.Merge(
vm.Ok,
vm.Cancel.Select(_ => (Participant)null))
.Take(1)
.Subscribe(model =>
{
if(model != null)
{
List.Participants.Add(model);
}
Content = List;
});
Content = vm;
}
}
}
在 AddParticipant 方法中。List.Participant.add(模型); 让我知道错误
ViewModels\MainWindowViewModel.cs(39,47): error CS1503: Argument "1": Konvertierung von "Decksumme.Models.Participant" in "Microsoft.EntityFrameworkCore.DbSet<Decksumme.Models.Participant>" nicht möglich.
现在的问题是缺乏知识。我理解 Observable 错误了吗?我用错了分贝吗?还是我必须在某个时候进行转换?