-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added WPF example - non-working (#75)
- Loading branch information
1 parent
995b646
commit 89697c1
Showing
27 changed files
with
744 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Application x:Class="TodoApp.WPF.App" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="clr-namespace:TodoApp.WPF" | ||
StartupUri="MainWindow.xaml"> | ||
<Application.Resources> | ||
|
||
</Application.Resources> | ||
</Application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// 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 Microsoft.Data.Sqlite; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System.Windows; | ||
using TodoApp.WPF.Database; | ||
using TodoApp.WPF.ViewModels; | ||
|
||
namespace TodoApp.WPF; | ||
|
||
/// <summary> | ||
/// Interaction logic for App.xaml | ||
/// </summary> | ||
public partial class App : Application, IDisposable | ||
{ | ||
private readonly SqliteConnection dbConnection; | ||
|
||
/// <summary> | ||
/// The IoC service provider | ||
/// </summary> | ||
public IServiceProvider Services { get; } | ||
|
||
public App() | ||
{ | ||
// Create the connection to the SQLite database | ||
this.dbConnection = new SqliteConnection("Data Source=:memory:"); | ||
this.dbConnection.Open(); | ||
|
||
// Create the IoC Services provider. | ||
Services = new ServiceCollection() | ||
.AddTransient<TodoListViewModel>() | ||
.AddScoped<IDbInitializer, DbContextInitializer>() | ||
.AddDbContext<AppDbContext>(options => options.UseSqlite(this.dbConnection)) | ||
.BuildServiceProvider(); | ||
|
||
// Initialize the database | ||
InitializeDatabase(); | ||
} | ||
|
||
private void InitializeDatabase() | ||
{ | ||
// using IServiceScope scope = Ioc.Default.CreateScope(); | ||
using IServiceScope scope = Services.CreateScope(); | ||
IDbInitializer initializer = scope.ServiceProvider.GetRequiredService<IDbInitializer>(); | ||
initializer.Initialize(); | ||
} | ||
|
||
/// <summary> | ||
/// A helper method for getting a service from the services collection. | ||
/// </summary> | ||
/// <remarks> | ||
/// You can see this in action in the <see cref="TodoListPage"/> class. | ||
/// </remarks> | ||
/// <typeparam name="TService">The type of the service.</typeparam> | ||
/// <returns>An instance of the service</returns> | ||
public static TService GetRequiredService<TService>() where TService : notnull | ||
=> ((App)App.Current).Services.GetRequiredService<TService>(); | ||
|
||
#region IDisposable | ||
private bool hasDisposed; | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!this.hasDisposed) | ||
{ | ||
if (disposing) | ||
{ | ||
this.dbConnection.Close(); | ||
} | ||
|
||
this.hasDisposed = true; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
#endregion | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// 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.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) | ||
)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 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 CommunityToolkit.Datasync.Client.Http; | ||
using CommunityToolkit.Datasync.Client.Offline; | ||
using Microsoft.EntityFrameworkCore; | ||
using TodoApp.WPF.Services; | ||
|
||
namespace TodoApp.WPF.Database; | ||
|
||
public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options) | ||
{ | ||
public DbSet<TodoItem> TodoItems => Set<TodoItem>(); | ||
|
||
// protected override void OnDatasyncInitialization(DatasyncOfflineOptionsBuilder optionsBuilder) | ||
// { | ||
// HttpClientOptions clientOptions = new() | ||
// { | ||
// Endpoint = new Uri("https://Y.azurewebsites.net/"), | ||
// HttpPipeline = [new LoggingHandler()] | ||
// }; | ||
// _ = optionsBuilder.UseHttpClientOptions(clientOptions); | ||
// } | ||
|
||
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}"); | ||
// } | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Use this class to initialize the database. In this sample, we just create | ||
/// the database using <see cref="DatabaseFacade.EnsureCreated"/>. However, you | ||
/// may want to use migrations. | ||
/// </summary> | ||
/// <param name="context">The context for the database.</param> | ||
public class DbContextInitializer(AppDbContext context) : IDbInitializer | ||
{ | ||
/// <inheritdoc /> | ||
public void Initialize() | ||
=> context.Database.EnsureCreated(); | ||
|
||
/// <inheritdoc /> | ||
public Task InitializeAsync(CancellationToken cancellationToken = default) | ||
=> context.Database.EnsureCreatedAsync(cancellationToken); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// 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. | ||
|
||
namespace TodoApp.WPF.Database; | ||
|
||
/// <summary> | ||
/// An interface to initialize a database. | ||
/// </summary> | ||
public interface IDbInitializer | ||
{ | ||
/// <summary> | ||
/// Synchronously initialize the database. | ||
/// </summary> | ||
void Initialize(); | ||
|
||
/// <summary> | ||
/// Asynchronously initialize the database. | ||
/// </summary> | ||
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param> | ||
/// <returns>A task that resolves when complete.</returns> | ||
Task InitializeAsync(CancellationToken cancellationToken = default); | ||
} |
19 changes: 19 additions & 0 deletions
19
samples/todoapp/TodoApp.WPF/Database/OfflineClientEntity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// 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.ComponentModel.DataAnnotations; | ||
|
||
namespace TodoApp.WPF.Database; | ||
|
||
/// <summary> | ||
/// An abstract class for working with offline entities. | ||
/// </summary> | ||
public abstract class OfflineClientEntity | ||
{ | ||
[Key] | ||
public string Id { get; set; } = Guid.NewGuid().ToString("N"); | ||
public DateTimeOffset? UpdatedAt { get; set; } | ||
public string? Version { get; set; } | ||
public bool Deleted { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// 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.Text.Json; | ||
|
||
namespace TodoApp.WPF.Database; | ||
|
||
public class TodoItem : OfflineClientEntity | ||
{ | ||
public string Title { get; set; } = string.Empty; | ||
public bool IsComplete { get; set; } = false; | ||
|
||
public override string ToString() | ||
=> JsonSerializer.Serialize(this); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.