Skip to content

(#271) WPF Sample Update to .NET 9 #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions samples/todoapp/Samples.TodoApp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TodoApp.Avalonia.iOS", "Tod
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TodoApp.Avalonia.Browser", "TodoApp.Avalonia\TodoApp.Avalonia.Browser\TodoApp.Avalonia.Browser.csproj", "{E8BB1310-477D-44B0-B13E-77F09433D0A3}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Files", "Solution Files", "{E0CB420D-EEFC-415C-B10A-A01233115A3C}"
ProjectSection(SolutionItems) = preProject
..\Directory.Packages.props = ..\Directory.Packages.props
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Server", "Server", "{AF5C479C-1BDE-4783-8707-A3440FC0B89D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Clients", "Clients", "{32025F6A-2D8A-4BA2-93BE-B989DFD216FC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -59,22 +68,6 @@ Global
{D783EDE7-D3A2-44D3-8C97-38AFFF9A4555}.Release|x86.ActiveCfg = Release|x86
{D783EDE7-D3A2-44D3-8C97-38AFFF9A4555}.Release|x86.Build.0 = Release|x86
{D783EDE7-D3A2-44D3-8C97-38AFFF9A4555}.Release|x86.Deploy.0 = Release|x86
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Debug|ARM64.Build.0 = Debug|Any CPU
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Debug|x64.ActiveCfg = Debug|Any CPU
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Debug|x64.Build.0 = Debug|Any CPU
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Debug|x86.ActiveCfg = Debug|Any CPU
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Debug|x86.Build.0 = Debug|Any CPU
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|Any CPU.Build.0 = Release|Any CPU
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|ARM64.ActiveCfg = Release|Any CPU
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|ARM64.Build.0 = Release|Any CPU
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|x64.ActiveCfg = Release|Any CPU
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|x64.Build.0 = Release|Any CPU
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|x86.ActiveCfg = Release|Any CPU
{2AC73FBE-9E76-4702-B551-B5884383CC68}.Release|x86.Build.0 = Release|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E67734DD-B397-4A65-AA50-D62F37EF05DD}.Debug|ARM64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -216,7 +209,11 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2AC73FBE-9E76-4702-B551-B5884383CC68} = {7183ECEC-9F44-48CE-BB97-AA2445170D5E}
{D783EDE7-D3A2-44D3-8C97-38AFFF9A4555} = {32025F6A-2D8A-4BA2-93BE-B989DFD216FC}
{E67734DD-B397-4A65-AA50-D62F37EF05DD} = {AF5C479C-1BDE-4783-8707-A3440FC0B89D}
{00430043-04C5-4F8F-87A9-98ECC0051808} = {32025F6A-2D8A-4BA2-93BE-B989DFD216FC}
{A0996FB8-890D-4E90-A881-01F9EF709711} = {32025F6A-2D8A-4BA2-93BE-B989DFD216FC}
{9A8B7D7F-1AF1-4C1C-A74A-E422BB680C6E} = {32025F6A-2D8A-4BA2-93BE-B989DFD216FC}
{539C6E0F-8F23-4AE0-B8E6-7E72C53B890A} = {9A8B7D7F-1AF1-4C1C-A74A-E422BB680C6E}
{9C2BA2A4-4AD6-4B67-BB6B-29A9024C33C4} = {9A8B7D7F-1AF1-4C1C-A74A-E422BB680C6E}
{3D741850-6FAA-4F36-BD58-F6ECE0CE55D7} = {9A8B7D7F-1AF1-4C1C-A74A-E422BB680C6E}
Expand Down
51 changes: 31 additions & 20 deletions samples/todoapp/TodoApp.WPF/Database/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,50 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#undef OFFLINE_SYNC_ENABLED

using CommunityToolkit.Datasync.Client.Http;
using CommunityToolkit.Datasync.Client.Offline;
using Microsoft.EntityFrameworkCore;
using TodoApp.WPF.Services;

namespace TodoApp.WPF.Database;

#if OFFLINE_SYNC_ENABLED
public class AppDbContext(DbContextOptions<AppDbContext> options) : OfflineDbContext(options)
#else
public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
#endif
{
public DbSet<TodoItem> TodoItems => Set<TodoItem>();

//protected override void OnDatasyncInitialization(DatasyncOfflineOptionsBuilder optionsBuilder)
//{
// HttpClientOptions clientOptions = new()
// {
// Endpoint = new Uri("https://YOURSITEHERE.azurewebsites.net/"),
// HttpPipeline = [new LoggingHandler()]
// };
// _ = optionsBuilder.UseHttpClientOptions(clientOptions);
//}
#if OFFLINE_SYNC_ENABLED
protected override void OnDatasyncInitialization(DatasyncOfflineOptionsBuilder optionsBuilder)
{
HttpClientOptions clientOptions = new()
{
Endpoint = new Uri("https://YOUR_SITE_HERE.azurewebsites.net/"),
HttpPipeline = [new LoggingHandler()]
};
_ = optionsBuilder.UseHttpClientOptions(clientOptions);
}
#endif

public async Task SynchronizeAsync(CancellationToken cancellationToken = default)
{
//PushResult pushResult = await this.PushAsync(cancellationToken);
//if (!pushResult.IsSuccessful)
//{
// throw new ApplicationException($"Push failed: {pushResult.FailedRequests.FirstOrDefault().Value.ReasonPhrase}");
//}

//PullResult pullResult = await this.PullAsync(cancellationToken);
//if (!pullResult.IsSuccessful)
//{
// throw new ApplicationException($"Pull failed: {pullResult.FailedRequests.FirstOrDefault().Value.ReasonPhrase}");
//}
#if OFFLINE_SYNC_ENABLED
PushResult pushResult = await this.PushAsync(cancellationToken);
if (!pushResult.IsSuccessful)
{
throw new ApplicationException($"Push failed: {pushResult.FailedRequests.FirstOrDefault().Value.ReasonPhrase}");
}

PullResult pullResult = await this.PullAsync(cancellationToken);
if (!pullResult.IsSuccessful)
{
throw new ApplicationException($"Pull failed: {pullResult.FailedRequests.FirstOrDefault().Value.ReasonPhrase}");
}
#endif
}
}

Expand Down
43 changes: 43 additions & 0 deletions samples/todoapp/TodoApp.WPF/Services/LoggingHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Net.Http;

namespace TodoApp.WPF.Services;

/// <summary>
/// A delegating handler that logs the request/response to stdout.
/// </summary>
public class LoggingHandler : DelegatingHandler
{
public LoggingHandler() : base()
{
}

public LoggingHandler(HttpMessageHandler innerHandler) : base(innerHandler)
{
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Debug.WriteLine($"[HTTP] >>> {request.Method} {request.RequestUri}");
await WriteContentAsync(request.Content, cancellationToken);

HttpResponseMessage response = await base.SendAsync(request, cancellationToken);

Debug.WriteLine($"[HTTP] <<< {response.StatusCode} {response.ReasonPhrase}");
await WriteContentAsync(response.Content, cancellationToken);

return response;
}

private static async Task WriteContentAsync(HttpContent? content, CancellationToken cancellationToken = default)
{
if (content is not null)
{
Debug.WriteLine($"[HTTP] >>> {await content.ReadAsStringAsync(cancellationToken)}");
}
}
}
8 changes: 4 additions & 4 deletions samples/todoapp/TodoApp.WPF/TodoApp.WPF.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net9.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
Expand All @@ -19,7 +19,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.0" />
<PackageReference Include="CommunityToolkit.Datasync.Client" Version="8.0.4" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="CommunityToolkit.Datasync.Client" Version="9.0.0" />
</ItemGroup>
</Project>