Setup Test UI and Added Prototype DataGrid

This commit is contained in:
Torben Nehmer 2024-07-23 20:20:01 +02:00
parent 2a55193289
commit da94963c4f
No known key found for this signature in database
9 changed files with 102 additions and 11 deletions

View File

@ -11,5 +11,6 @@
<Application.Styles>
<FluentTheme />
<StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml"/>
</Application.Styles>
</Application>

View File

@ -2,7 +2,7 @@
using System.Globalization;
using CSV = CsvHelper.Configuration.Attributes;
namespace net.nehmer.sa.model;
namespace net.nehmer.sa.Model;
public class Planet
{

View File

@ -3,7 +3,7 @@ using System;
using System.Collections.Generic;
using System.Numerics;
namespace net.nehmer.sa.model;
namespace net.nehmer.sa.Model;
public class StarsDatabase(string DbPath) : DbContext
{

View File

@ -1,6 +1,12 @@
using System;
using Avalonia;
using Avalonia.ReactiveUI;
using Microsoft.EntityFrameworkCore;
using CsvHelper.Configuration;
using CsvHelper;
using net.nehmer.sa.Model;
using System.Globalization;
namespace net.nehmer.sa;
@ -10,16 +16,49 @@ sealed class Program
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args) => BuildAvaloniaApp()
public static void Main(string[] args)
{
// __createTestData();
BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
}
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace()
.UseReactiveUI();
.LogToTrace();
public static void __createTestData ()
{
using var db = new StarsDatabase("stars.sqlite");
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
// Note: This sample requires the database to be created before running.
Console.WriteLine($"Database path: {db.DbPath}.");
var config = CsvConfiguration.FromAttributes<Planet>(CultureInfo.InvariantCulture);
config.Delimiter = "\t";
config.Escape = '\0';
config.MissingFieldFound = null;
using (var reader = new StreamReader("/home/torben/goingth/GOINGTH.p1", System.Text.Encoding.Latin1))
using (var csv = new CsvReader(reader, config))
{
List<Planet> records = csv.GetRecords<Planet>().ToList();
foreach (Planet p in records)
{
db.Add(p);
}
db.SaveChanges();
}
}
}

View File

@ -23,12 +23,12 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.7" />
<PackageReference Include="Avalonia" Version="11.0.10" />
<PackageReference Include="Avalonia.Controls.DataGrid" Version="11.0.10" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.10" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.10" />
<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.0.10" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.0.10" />
</ItemGroup>
</Project>

View File

@ -1,8 +1,13 @@
namespace net.nehmer.sa.ViewModels;
using System.Collections.ObjectModel;
public class MainWindowViewModel : ViewModelBase
namespace net.nehmer.sa.ViewModels;
public partial class MainWindowViewModel : ViewModelBase
{
#pragma warning disable CA1822 // Mark members as static
public string Greeting => "Welcome to Stars Assistant!";
#pragma warning restore CA1822 // Mark members as static
public ObservableCollection<PlanetViewModel> Planets { get; } = PlanetViewModel.LoadAll();
}

View File

@ -0,0 +1,26 @@
using System.Collections.ObjectModel;
using net.nehmer.sa.Model;
namespace net.nehmer.sa.ViewModels;
public partial class PlanetViewModel(Planet planet) : ViewModelBase
{
public static ObservableCollection<PlanetViewModel> LoadAll() {
var context = new StarsDatabase("stars.sqlite");
var result = new ObservableCollection<PlanetViewModel>();
foreach (Planet planet in context.Planets.ToList())
{
var vm = new PlanetViewModel(planet);
result.Add(vm);
}
return result;
}
private readonly Planet planet = planet;
public string Name => planet.Name;
public string Owner => planet.Owner ?? String.Empty;
public int Value => planet.Value ?? 0;
}

View File

@ -1,7 +1,7 @@
using ReactiveUI;
using CommunityToolkit.Mvvm.ComponentModel;
namespace net.nehmer.sa.ViewModels;
public class ViewModelBase : ReactiveObject
public class ViewModelBase : ObservableObject
{
}

View File

@ -27,6 +27,26 @@
<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>
<DataGrid Margin="20" ItemsSource="{Binding Planets}"
AutoGenerateColumns="True" IsReadOnly="True"
GridLinesVisibility="All"
BorderThickness="1" BorderBrush="Gray">
</DataGrid>
<!-- ScrollViewer>
<ItemsControl ItemsSource="{Binding Planets}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="vm:PlanetViewModel">
<Grid ColumnDefinitions="200, 100, 50">
<TextBlock Grid.Column="0" Text="{Binding Name}"/>
<TextBlock Grid.Column="1" Text="{Binding Owner}"/>
<TextBlock Grid.Column="2" Text="{Binding Value}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer-->
</StackPanel>
</Window>