From 7685d1b4dfef0710b178b44cdbef94cd321dba14 Mon Sep 17 00:00:00 2001 From: gil zhang Date: Tue, 20 Dec 2022 16:57:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=80=89=E6=8B=A9=E5=99=A8?= =?UTF-8?q?=E5=88=B0=E7=94=B5=E5=AD=90=E7=9A=84=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ClockViews/RandomContentView.xaml | 59 ++++++++++++ .../ClockViews/RandomContentView.xaml.cs | 36 ++++++++ .../ElectronBot.BraincasePreview.csproj | 4 + .../GestureClassificationViewModel.cs | 90 ++++++++++++------- .../Views/GestureClassificationPage.xaml | 47 +--------- 5 files changed, 161 insertions(+), 75 deletions(-) create mode 100644 src/ElectronBot.BraincasePreview/ClockViews/RandomContentView.xaml create mode 100644 src/ElectronBot.BraincasePreview/ClockViews/RandomContentView.xaml.cs diff --git a/src/ElectronBot.BraincasePreview/ClockViews/RandomContentView.xaml b/src/ElectronBot.BraincasePreview/ClockViews/RandomContentView.xaml new file mode 100644 index 0000000..af61ee5 --- /dev/null +++ b/src/ElectronBot.BraincasePreview/ClockViews/RandomContentView.xaml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/ElectronBot.BraincasePreview/ClockViews/RandomContentView.xaml.cs b/src/ElectronBot.BraincasePreview/ClockViews/RandomContentView.xaml.cs new file mode 100644 index 0000000..a727059 --- /dev/null +++ b/src/ElectronBot.BraincasePreview/ClockViews/RandomContentView.xaml.cs @@ -0,0 +1,36 @@ +// Copyright (c) Microsoft Corporation and Contributors. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using ElectronBot.BraincasePreview; +using ElectronBot.BraincasePreview.ViewModels; +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 Windows.Foundation; +using Windows.Foundation.Collections; + +// To learn more about WinUI, the WinUI project structure, +// and more about our project templates, see: http://aka.ms/winui-project-info. + +namespace ClockViews; +public sealed partial class RandomContentView : UserControl +{ + public GestureClassificationViewModel ViewModel + { + get; + } + public RandomContentView(GestureClassificationViewModel viewModel) + { + ViewModel = viewModel; + this.InitializeComponent(); + } +} diff --git a/src/ElectronBot.BraincasePreview/ElectronBot.BraincasePreview.csproj b/src/ElectronBot.BraincasePreview/ElectronBot.BraincasePreview.csproj index f971264..753c21a 100644 --- a/src/ElectronBot.BraincasePreview/ElectronBot.BraincasePreview.csproj +++ b/src/ElectronBot.BraincasePreview/ElectronBot.BraincasePreview.csproj @@ -43,6 +43,7 @@ + @@ -186,6 +187,9 @@ PreserveNewest + + MSBuild:Compile + MSBuild:Compile diff --git a/src/ElectronBot.BraincasePreview/ViewModels/GestureClassificationViewModel.cs b/src/ElectronBot.BraincasePreview/ViewModels/GestureClassificationViewModel.cs index 6bcb65b..085a800 100644 --- a/src/ElectronBot.BraincasePreview/ViewModels/GestureClassificationViewModel.cs +++ b/src/ElectronBot.BraincasePreview/ViewModels/GestureClassificationViewModel.cs @@ -1,6 +1,7 @@ using System.Collections.ObjectModel; using System.Diagnostics; using System.Runtime.InteropServices; +using ClockViews; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Controls; @@ -33,14 +34,30 @@ public partial class GestureClassificationViewModel : ObservableRecipient, INavi DispatcherTimer dispatcherTimer = new DispatcherTimer(); + private readonly DispatcherTimer _dispatcherTimer; + private readonly string modelPath = Package.Current.InstalledLocation.Path + $"\\Assets\\MLModel1.zip"; private readonly ILocalSettingsService _localSettingsService; - public GestureClassificationViewModel(ILocalSettingsService localSettingsService) + public GestureClassificationViewModel(DispatcherTimer dispatcherTimer1, + ILocalSettingsService localSettingsService) { calculator = new HandsCpuSolution(); dispatcherTimer.Interval = TimeSpan.FromMilliseconds(400); dispatcherTimer.Tick += DispatcherTimer_Tick; _localSettingsService = localSettingsService; + _dispatcherTimer = dispatcherTimer1; + _dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 40); + + _dispatcherTimer.Tick += DispatcherTimer_ClockTick; + Element = new RandomContentView(this); + } + + private async void DispatcherTimer_ClockTick(object? sender, object e) + { + if (ElectronBotHelper.Instance.EbConnected) + { + await EbHelper.ShowClockCanvasToDeviceAsync(Element); + } } private Storyboard storyboard = new(); @@ -58,12 +75,16 @@ public partial class GestureClassificationViewModel : ObservableRecipient, INavi } private void DispatcherTimer_Tick(object sender, object e) { - var index = new Random().Next(0, RandomContentList.Count); - var text = RandomContentList[index].Content; - RandomContentText = $"{text}"; - storyboard.Children[0].SetValue(DoubleAnimation.FromProperty, 0); - storyboard.Children[0].SetValue(DoubleAnimation.ToProperty, 180); - storyboard.Begin(); + if (RandomContentList.Count > 0) + { + var index = new Random().Next(0, RandomContentList.Count); + + var text = RandomContentList[index].Content; + RandomContentText = $"{text}"; + storyboard.Children[0].SetValue(DoubleAnimation.FromProperty, 0); + storyboard.Children[0].SetValue(DoubleAnimation.ToProperty, 180); + storyboard.Begin(); + } } [ObservableProperty] @@ -72,6 +93,12 @@ public partial class GestureClassificationViewModel : ObservableRecipient, INavi [ObservableProperty] private string randomContentResultText; + + /// + /// 随机内容 + /// + [ObservableProperty] + UIElement element; [RelayCommand] private async Task TestGestureClassficationAsync() { @@ -161,7 +188,7 @@ public partial class GestureClassificationViewModel : ObservableRecipient, INavi public async void OnNavigatedTo(object parameter) { await InitAsync(); - //dispatcherTimer.Start(); + _dispatcherTimer.Start(); } [ObservableProperty] @@ -228,28 +255,31 @@ public partial class GestureClassificationViewModel : ObservableRecipient, INavi { ResultLabel = e; - if (e == "back" && _isBeginning == false) + if(RandomContentList.Count > 0) { - //todo:启动动画 并播放 - _isBeginning = true; - RandomContentResultText = ""; - dispatcherTimer.Start(); - Debug.WriteLine("启动动画"); - } - else if (e == "back" && _isBeginning == true) - { - //当前处于启动状态 - //不做处理 - } - else if (e == "land" && _isBeginning == true) - { - var index = new Random().Next(0, RandomContentList.Count); - var text = RandomContentList[index].Content; - RandomContentResultText = $"{text}"; - _isBeginning = false; - Debug.WriteLine("停止动画"); - RandomContentText = ""; - dispatcherTimer.Stop(); + if (e == "back" && _isBeginning == false) + { + //todo:启动动画 并播放 + _isBeginning = true; + RandomContentResultText = ""; + dispatcherTimer.Start(); + Debug.WriteLine("启动动画"); + } + else if (e == "back" && _isBeginning == true) + { + //当前处于启动状态 + //不做处理 + } + else if (e == "land" && _isBeginning == true) + { + var index = new Random().Next(0, RandomContentList.Count); + var text = RandomContentList[index].Content; + RandomContentResultText = $"{text}"; + _isBeginning = false; + Debug.WriteLine("停止动画"); + RandomContentText = ""; + dispatcherTimer.Stop(); + } } }); } @@ -265,7 +295,6 @@ public partial class GestureClassificationViewModel : ObservableRecipient, INavi e.SoftwareBitmap = SoftwareBitmap.Convert( e.SoftwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } - var service = App.GetService(); _ = await service.HandPredictResultUnUseQueueAsync(calculator, modelPath, e.SoftwareBitmap); @@ -275,6 +304,7 @@ public partial class GestureClassificationViewModel : ObservableRecipient, INavi { await CleanUpAsync(); dispatcherTimer.Stop(); + _dispatcherTimer.Stop(); } private async Task CleanUpAsync() diff --git a/src/ElectronBot.BraincasePreview/Views/GestureClassificationPage.xaml b/src/ElectronBot.BraincasePreview/Views/GestureClassificationPage.xaml index 4383b9a..77ae01b 100644 --- a/src/ElectronBot.BraincasePreview/Views/GestureClassificationPage.xaml +++ b/src/ElectronBot.BraincasePreview/Views/GestureClassificationPage.xaml @@ -6,30 +6,10 @@ 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:i="using:Microsoft.Xaml.Interactivity" - xmlns:ic="using:Microsoft.Xaml.Interactions.Core" xmlns:local="using:Views" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ui="using:CommunityToolkit.WinUI.UI" mc:Ignorable="d"> - - - - - - - - - - - - - - @@ -74,31 +54,8 @@ ui:UIElementExtensions.ClipToBounds="True" BorderBrush="{ThemeResource SystemAccentColor}" BorderThickness="4" - CornerRadius="8"> - - - - - - - - - - + Child="{x:Bind ViewModel.Element, Mode=OneWay}" + CornerRadius="8" />