Dependency Injection, frist UI Reworks

This commit is contained in:
Torben Nehmer 2024-09-11 20:04:20 +02:00
parent a9763be8d2
commit fa82a4fbb3
No known key found for this signature in database
6 changed files with 25 additions and 31 deletions

View File

@ -20,7 +20,9 @@ public class StarsDatabase(string DbPath) : DbContext
#endregion
#region Entities
#region IStarsDatabase Implementation
public DbContext DbContext => this;
/// <summary>
/// List of all Planets read.

View File

@ -1,5 +1,6 @@
using System;
using Avalonia;
using Splat;
using Microsoft.EntityFrameworkCore;
using CsvHelper.Configuration;
@ -19,6 +20,12 @@ sealed class Program
[STAThread]
public static void Main(string[] args)
{
ModeDetector.OverrideModeDetector(Splat.ModeDetection.Mode.Run);
Locator.CurrentMutable.Register(
() => new StarsDatabase("stars.sqlite"),
typeof(StarsDatabase)
);
__createTestData();
BuildAvaloniaApp()
@ -36,13 +43,13 @@ sealed class Program
public static void __createTestData ()
{
using var db = new StarsDatabase("stars.sqlite");
using var db = Locator.Current.GetService<StarsDatabase>()!;
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
// Note: This sample requires the database to be created before running.
Console.WriteLine($"Database path: {db.DbPath}.");
// Console.WriteLine($"Database path: {db.DbPath}.");
Race r = new()
{

View File

@ -27,6 +27,7 @@
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.10" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.3" />
<PackageReference Include="Splat" Version="15.1.1" />
<PackageReference Include="System.Reactive" Version="6.0.1" />
</ItemGroup>

View File

@ -1,15 +1,16 @@
using System.Collections.ObjectModel;
using Microsoft.EntityFrameworkCore;
using StarsAssistant.Model;
using Splat;
namespace StarsAssistant.ViewModels;
public partial class PlanetViewModel(Planet planet) : ViewModelBase
{
public static ObservableCollection<PlanetViewModel> LoadAll() {
var context = new StarsDatabase("stars.sqlite");
using var db = Locator.Current.GetService<StarsDatabase>()!;
var result = new ObservableCollection<PlanetViewModel>();
foreach (Planet planet in context.Planet.Where(p => p.OwnerId == "Atlantis").ToList())
foreach (Planet planet in db.Planet.Where(p => p.OwnerId == "Atlantis").ToList())
{
var vm = new PlanetViewModel(planet);
result.Add(vm);
@ -24,4 +25,12 @@ public partial class PlanetViewModel(Planet planet) : ViewModelBase
public string Owner => planet.OwnerId ?? String.Empty;
public int Value => planet.Value ?? 0;
public int Population => planet.Population ?? 0;
public int SurfaceIronium => planet.SurfaceIronium ?? 0;
public int SurfaceBoranium => planet.SurfaceBoranium ?? 0;
public int SurfaceGermanium => planet.SurfaceGermanium ?? 0;
}

View File

@ -19,18 +19,7 @@
<TabControl>
<TabItem Header="Home">
<StackPanel>
<Border Margin="5" CornerRadius="10" Background="LightBlue">
<TextBlock Margin="5" FontSize="24" HorizontalAlignment="Center" Text="{Binding Greeting}"></TextBlock>
</Border>
<Grid ShowGridLines="True" Margin="5" ColumnDefinitions="120, 100" RowDefinitions="Auto, Auto, Auto">
<Label Grid.Row="0" Grid.Column="0" Margin="10">Celsius</Label>
<TextBox Grid.Row="0" Grid.Column="1" Margin="0 5" Text="0" Name="celsius"/>
<Label Grid.Row="1" Grid.Column="0" Margin="10">Fahrenheit</Label>
<TextBox Grid.Row="1" Grid.Column="1" Margin="0 5" Text="0" Name="fahrenheit"/>
<Button Grid.Row="2" Grid.Column="1" Margin="0 5" Click="ButtonClicked">Calculate</Button>
</Grid>
</StackPanel>
Welcome to Stars Assistant!
</TabItem>
<TabItem Header="DataGrid">
<DataGrid ItemsSource="{Binding Planets}"

View File

@ -11,18 +11,4 @@ public partial class MainWindow : Window
{
InitializeComponent();
}
public void ButtonClicked(object source, RoutedEventArgs args)
{
if (Double.TryParse(celsius.Text, out double C))
{
var F = C * (9d / 5d) + 32;
fahrenheit.Text = F.ToString("0.0");
}
else
{
celsius.Text = "0";
fahrenheit.Text = "0";
}
}
}