diff --git a/.gitignore b/.gitignore index 0cbe78a..10bb22f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,8 @@ WinUINotes/bin/ WinUINotes/obj/ WinUINotes/.vshistory/ +WinUINotes.sln.DotSettings.user +WinUINotes.Grpc/.vshistory/ +WinUINotes.Grpc/bin/ +WinUINotes.Grpc/obj/ +WinUINotes.Grpc/Protos/.vshistory/ diff --git a/WinUINotes.Grpc/Protos/greet.proto b/WinUINotes.Grpc/Protos/greet.proto new file mode 100644 index 0000000..4ae009f --- /dev/null +++ b/WinUINotes.Grpc/Protos/greet.proto @@ -0,0 +1,21 @@ +syntax = "proto3"; + +option csharp_namespace = "WinUINotes"; + +package greet; + +// The greeting service definition. +service Greeter { + // Sends a greeting + rpc SayHello (HelloRequest) returns (HelloReply); +} + +// The request message containing the user's name. +message HelloRequest { + string name = 1; +} + +// The response message containing the greetings. +message HelloReply { + string message = 1; +} diff --git a/WinUINotes.Grpc/Protos/my_diary.proto b/WinUINotes.Grpc/Protos/my_diary.proto new file mode 100644 index 0000000..a2c9919 --- /dev/null +++ b/WinUINotes.Grpc/Protos/my_diary.proto @@ -0,0 +1,35 @@ +syntax = "proto3"; + +option csharp_namespace = "ZR.Admin.Grpc"; + +package my.diary; + +import "google/protobuf/timestamp.proto"; + +// The greeting service definition. +service MyDiary { + // Sends a greeting + rpc Edit (EditRequest) returns (EditReply); + rpc Query (QueryRequest) returns (QueryReply); +} + +// The request message containing the user's name. +message EditRequest { + string content = 1; + int64 user_id = 2; + google.protobuf.Timestamp create_time = 3; // 字段名仍然建议使用 snake_case +} + +// The response message containing the greetings. +message EditReply { + string result_message = 1; +} + +message QueryRequest { + int64 user_id = 1; + google.protobuf.Timestamp create_time = 2; +} + +message QueryReply { + string content = 1; +} \ No newline at end of file diff --git a/WinUINotes.Grpc/WinUINotes.Grpc.csproj b/WinUINotes.Grpc/WinUINotes.Grpc.csproj new file mode 100644 index 0000000..793582c --- /dev/null +++ b/WinUINotes.Grpc/WinUINotes.Grpc.csproj @@ -0,0 +1,24 @@ + + + net8.0-windows10.0.19041.0 + 10.0.17763.0 + WinUINotes.Grpc + win-x86;win-x64;win-arm64 + true + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + \ No newline at end of file diff --git a/WinUINotes.sln b/WinUINotes.sln index 88b5c18..71d12d1 100644 --- a/WinUINotes.sln +++ b/WinUINotes.sln @@ -1,10 +1,12 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 -VisualStudioVersion = 17.14.36203.30 d17.14 +VisualStudioVersion = 17.14.36203.30 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinUINotes", "WinUINotes\WinUINotes.csproj", "{5F69CDC8-B184-45BA-9113-311FB706CF79}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinUINotes.Grpc", "WinUINotes.Grpc\WinUINotes.Grpc.csproj", "{A42CC243-ED38-4744-B1B5-145F6641AF44}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|ARM64 = Debug|ARM64 @@ -33,6 +35,18 @@ Global {5F69CDC8-B184-45BA-9113-311FB706CF79}.Release|x86.ActiveCfg = Release|x86 {5F69CDC8-B184-45BA-9113-311FB706CF79}.Release|x86.Build.0 = Release|x86 {5F69CDC8-B184-45BA-9113-311FB706CF79}.Release|x86.Deploy.0 = Release|x86 + {A42CC243-ED38-4744-B1B5-145F6641AF44}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {A42CC243-ED38-4744-B1B5-145F6641AF44}.Debug|ARM64.Build.0 = Debug|Any CPU + {A42CC243-ED38-4744-B1B5-145F6641AF44}.Debug|x64.ActiveCfg = Debug|Any CPU + {A42CC243-ED38-4744-B1B5-145F6641AF44}.Debug|x64.Build.0 = Debug|Any CPU + {A42CC243-ED38-4744-B1B5-145F6641AF44}.Debug|x86.ActiveCfg = Debug|Any CPU + {A42CC243-ED38-4744-B1B5-145F6641AF44}.Debug|x86.Build.0 = Debug|Any CPU + {A42CC243-ED38-4744-B1B5-145F6641AF44}.Release|ARM64.ActiveCfg = Release|Any CPU + {A42CC243-ED38-4744-B1B5-145F6641AF44}.Release|ARM64.Build.0 = Release|Any CPU + {A42CC243-ED38-4744-B1B5-145F6641AF44}.Release|x64.ActiveCfg = Release|Any CPU + {A42CC243-ED38-4744-B1B5-145F6641AF44}.Release|x64.Build.0 = Release|Any CPU + {A42CC243-ED38-4744-B1B5-145F6641AF44}.Release|x86.ActiveCfg = Release|Any CPU + {A42CC243-ED38-4744-B1B5-145F6641AF44}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/WinUINotes/NotePage.xaml.cs b/WinUINotes/NotePage.xaml.cs index df60073..412dc90 100644 --- a/WinUINotes/NotePage.xaml.cs +++ b/WinUINotes/NotePage.xaml.cs @@ -1,18 +1,10 @@ +using Grpc.Net.Client; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; -using Microsoft.UI.Xaml.Controls.Primitives; -using Microsoft.UI.Xaml.Data; -using Microsoft.UI.Xaml.Input; -using Microsoft.UI.Xaml.Media; -using Microsoft.UI.Xaml.Navigation; using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; -using Windows.Foundation; -using Windows.Foundation.Collections; using Windows.Storage; +using Google.Protobuf.WellKnownTypes; +using ZR.Admin.Grpc; // To learn more about WinUI, the WinUI project structure, // and more about our project templates, see: http://aka.ms/winui-project-info. @@ -24,6 +16,19 @@ namespace WinUINotes /// public sealed partial class NotePage : Page { + private Greeter.GreeterClient _greeterClient; + private MyDiary.MyDiaryClient _myDiaryClient; + + private void InitializeGrpcClient() + { + //AppContext.SetSwitch( + // "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); + // 确保你的 gRPC 服务器正在运行在指定的地址和端口 + var channel = GrpcChannel.ForAddress("https://localhost:8888"); // 或者你的服务器地址 + _greeterClient = new Greeter.GreeterClient(channel); + _myDiaryClient = new MyDiary.MyDiaryClient(channel); + } + private StorageFolder storageFolder = ApplicationData.Current.LocalFolder; private StorageFile? noteFile = null; private string fileName = "note.txt"; @@ -33,14 +38,26 @@ namespace WinUINotes InitializeComponent(); Loaded += NotePage_Loaded; + + InitializeGrpcClient(); } private async void NotePage_Loaded(object sender, RoutedEventArgs e) { - noteFile = (StorageFile)await storageFolder.TryGetItemAsync(fileName); - if (noteFile is not null) + //noteFile = (StorageFile)await storageFolder.TryGetItemAsync(fileName); + //if (noteFile is not null) + //{ + // NoteEditor.Text = await FileIO.ReadTextAsync(noteFile); + //} + + var reply = await _myDiaryClient.QueryAsync(new QueryRequest { - NoteEditor.Text = await FileIO.ReadTextAsync(noteFile); + UserId = 1L, + CreateTime = Timestamp.FromDateTime(DateTime.UtcNow) + }); + if (reply != null) + { + NoteEditor.Text = reply.Content; } } @@ -50,7 +67,22 @@ namespace WinUINotes { noteFile = await storageFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); } - await FileIO.WriteTextAsync(noteFile, NoteEditor.Text); + + + try + { + var request = new EditRequest + { + Content = NoteEditor.Text, + UserId = 1L, + CreateTime = Timestamp.FromDateTime(DateTime.UtcNow) + }; + var reply = await _myDiaryClient.EditAsync(request); + await FileIO.WriteTextAsync(noteFile, request.Content); + } + catch (Exception ex) + { + } } private async void DeleteButton_Click(object sender, RoutedEventArgs e) diff --git a/WinUINotes/WinUINotes.csproj b/WinUINotes/WinUINotes.csproj index d777d23..93adb92 100644 --- a/WinUINotes/WinUINotes.csproj +++ b/WinUINotes/WinUINotes.csproj @@ -1,4 +1,4 @@ - + WinExe net8.0-windows10.0.19041.0 @@ -42,13 +42,16 @@ + + + MSBuild:Compile -