Aufgabe WPF

This commit is contained in:
2025-06-02 15:30:25 +02:00
parent 86006adb9c
commit 80d64c9574
13 changed files with 257 additions and 3 deletions

8
FahzeugWPF/App.xaml Normal file
View File

@@ -0,0 +1,8 @@
<Application x:Class="FahzeugWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FahzeugWPF">
<Application.Resources>
</Application.Resources>
</Application>

44
FahzeugWPF/App.xaml.cs Normal file
View File

@@ -0,0 +1,44 @@
using FahrzeugDatenBank;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.IO;
using System.Windows;
namespace FahzeugWPF
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public static ServiceProvider ServiceProvider { get; private set; }
public App()
{
IConfiguration configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: false, reloadOnChange: true).Build();
ServiceCollection services = new ServiceCollection();
services.AddScoped<IKonfigurationsleser>(sp => new Konfigurationsleser(configuration));
services.AddScoped(sp => new FahrzeugRepository(sp.GetRequiredService<IKonfigurationsleser>().LiesDBVerebindung()));
services.AddScoped<FahrzeugeModell>();
services.AddSingleton<MainWindowViewModel>();
services.AddSingleton<MainWindow>();
ServiceProvider = services.BuildServiceProvider();
}
protected override void OnStartup(StartupEventArgs e)
{
this.MainWindow = ServiceProvider.GetService<MainWindow>();
this.MainWindow.DataContext = ServiceProvider.GetService<MainWindowViewModel>();
this.MainWindow.Show();
}
private string GetConnectionString()
{
return "Server=localhost;User ID=admin;Password=admin;Database=FahrzeugDB";
}
}
}

View File

@@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

View File

@@ -0,0 +1,45 @@
using FahrzeugDatenBank;
namespace FahzeugWPF;
class FahrzeugeModell
{
private readonly FahrzeugRepository _repository;
public FahrzeugeModell(FahrzeugRepository repository)
{
this._repository = repository;
}
public async Task<IEnumerable<Fahrzeug>> LadeAlleFahrzeuge()
{
List<FahrzeugDTO>? fahrzeugs = await Task.Run(() =>
{
return _repository.HoleAlleFahrzeuge();
});
var fahrzeugListe = KonvertiereFahrzeuge(fahrzeugs);
return fahrzeugListe;
}
private IEnumerable<Fahrzeug> KonvertiereFahrzeuge(IEnumerable<FahrzeugDTO> fahrzeugs)
{
return fahrzeugs.Select(fahrzeug => KonvertiereFahrzeuf(fahrzeug));
}
private Fahrzeug KonvertiereFahrzeuf(FahrzeugDTO fahrzeugDTO)
{
switch (fahrzeugDTO.Typ)
{
case "Auto":
var auto = new Auto() { Id = fahrzeugDTO.Id, Name = fahrzeugDTO.Name, };
return auto;
case "Motorrad":
var motorrad = new Motorrad() { Id = fahrzeugDTO.Id, Name = fahrzeugDTO.Name, };
return motorrad;
case "Fahrrad":
var fahrrad = new Fahrrad() { Id = fahrzeugDTO.Id, Name = fahrzeugDTO.Name, };
return fahrrad;
}
throw new Exception($"Unbekannter FahrzeugTyp: {fahrzeugDTO.Typ}");
}
}

View File

@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net9.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FahrzeugDatenBank\FahrzeugDatenBank.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,7 @@
namespace FahzeugWPF
{
public interface IKonfigurationsleser
{
string LiesDBVerebindung();
}
}

View File

@@ -0,0 +1,18 @@
using Microsoft.Extensions.Configuration;
namespace FahzeugWPF;
public class Konfigurationsleser : IKonfigurationsleser
{
private readonly IConfiguration _configuration;
public Konfigurationsleser(IConfiguration configuration)
{
this._configuration = configuration;
}
public string LiesDBVerebindung()
{
return _configuration.GetConnectionString("MariaDB");
}
}

View File

@@ -0,0 +1,25 @@
<Window x:Class="FahzeugWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FahzeugWPF"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=local:MainWindowViewModel, IsDesignTimeCreatable=False}"
Title="Fahrzeuge" Height="450" Width="800">
<StackPanel>
<DataGrid Name="dgTest"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserSortColumns="True"
IsReadOnly="True"
AutoGenerateColumns="False"
ItemsSource="{Binding Path=Fahrzeuge}">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Path=Id}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name}"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Window>

View File

@@ -0,0 +1,24 @@
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace FahzeugWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}

View File

@@ -0,0 +1,26 @@
using FahrzeugDatenBank;
using System.Collections.ObjectModel;
namespace FahzeugWPF;
class MainWindowViewModel
{
private readonly FahrzeugeModell _model;
public MainWindowViewModel(FahrzeugeModell modell)
{
this._model = modell;
this.InitialisiereDasViewModell();
}
public ObservableCollection<Fahrzeug> Fahrzeuge { get; } = new ObservableCollection<Fahrzeug>();
private async void InitialisiereDasViewModell()
{
var fahrzeuge = await _model.LadeAlleFahrzeuge();
foreach (var fahrzeug in fahrzeuge)
{
this.Fahrzeuge.Add(fahrzeug);
}
}
}

View File

@@ -0,0 +1,5 @@
{
"ConnectionStrings": {
"MariaDB": "Server=localhost;User ID=admin;Password=admin;Database=FahrzeugDB"
}
}

View File

@@ -58,6 +58,11 @@ async Task<string> Methode2()
return await Task.Run(() => Arbeiten1("Methode2"));
}
Methode();
Methode1();
Methode2();
Task task = Methode();
var task1 = Methode1();
var task2 = Methode2();
await task;
await task1;
await task2;

View File

@@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Multi", "Multi\Multi.csproj
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FahrzeugDatenBankTest", "FahrzeugDatenBankTest\FahrzeugDatenBankTest.csproj", "{9C442F96-64F6-4CEA-AB7F-2B28BF578471}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FahzeugWPF", "FahzeugWPF\FahzeugWPF.csproj", "{BB365A73-CB9B-471A-BDBE-02843D8C72A3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -45,6 +47,10 @@ Global
{9C442F96-64F6-4CEA-AB7F-2B28BF578471}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C442F96-64F6-4CEA-AB7F-2B28BF578471}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C442F96-64F6-4CEA-AB7F-2B28BF578471}.Release|Any CPU.Build.0 = Release|Any CPU
{BB365A73-CB9B-471A-BDBE-02843D8C72A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BB365A73-CB9B-471A-BDBE-02843D8C72A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB365A73-CB9B-471A-BDBE-02843D8C72A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB365A73-CB9B-471A-BDBE-02843D8C72A3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE