【发布时间】:2021-10-13 05:22:34
【问题描述】:
我正在编写一个Speech To Text 应用程序,它从用户的麦克风中捕获音频,将音频流发送到服务器,并从服务器接收文本。
我正在使用 WebSockets 与服务器通信,我希望应用程序和服务器之间的连接仅在应用程序关闭时结束(当用户按下 UWP 上的“X”按钮时)。
快速的谷歌搜索指示我使用 System.ComponentModel.CancelEventHandler。我在编写代码时使用了这个文档:https://docs.microsoft.com/en-us/dotnet/api/system.windows.window.closing?view=net-5.0
旁注:我也尝试使用 Application.Suspending 事件,但注意到它在录制用户音频后立即关闭了我的 WebSocket 连接。
我现在收到以下错误:
严重性代码描述项目文件行抑制状态 错误 XDG0012 成员“Closing”无法识别或无法访问。
严重性代码描述项目文件行抑制状态 错误 XLS0413 在类型“页面”中找不到属性“关闭”。
严重性代码描述项目文件行抑制状态 元素“页面”上的错误未知成员“正在关闭”
-here is a picture of the errors-
这是 MainPage.xaml:
<Page
x:Class="SpeechToTextApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SpeechToTextApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Height="168" Width="410"
Closing="OnClosing">
<Page.Resources>
<SolidColorBrush x:Key="TranslucentBlackBrush" Color="Black" Opacity="1"/>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button Content="Start Recording" Click="btnRec_Click" Visibility="Visible" Background="Green" x:Name="btnRec" Height="47" Width="155" Margin="31,106,0,0" VerticalAlignment="Top"/>
<Button Content="Stop Recording" Click="btnStop_Click" Visibility="Visible" Background="Red" x:Name="btnStop" Margin="245,106,0,0" VerticalAlignment="Top" Height="47" Width="139"/>
</Grid>
</Page>
这里是MainPage.xaml.cs的重要部分
using System;
using System.Threading.Tasks;
using Windows.Media.Capture;
using Windows.Media.MediaProperties;
using Windows.Storage;
using Windows.System.Display;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System.IO;
using Windows.Storage.Streams;
namespace SpeechToTextApp
{
public sealed partial class MainPage : Page
{
public delegate System.ComponentModel.CancelEventHandler Closing();
private static Client.Client client;
...
public MainPage()
{
this.InitializeComponent();
client = new Client.Client();
Task task = client.ConnectToServer();
...
}
private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
Task task = client.DisconnectFromServer();
task.Wait();
}
...
void btnRec_Click(object sender, RoutedEventArgs e)
{
...
}
async void btnStop_Click(object sender, RoutedEventArgs e)
{
...
}
}
}
'Client' 是我在 Client.cs 文件中编写的用于 App-Server 通信的命名空间。命名空间内的 Client 类有一个打开 WebSocket 连接的 ConnectToServer() 方法和一个关闭 WebSocket 连接的 DisconnectFromServer() 方法。
我想在关闭应用程序时运行 DisconnectFromServer() 方法,无论如何。 (就像我试图在 OnClosing() 函数中做的那样)
【问题讨论】:
-
Window.Closing Event 文档提到此 API 适用于 .NET Core 3.0 和 3.1。 UWP 仅支持 .NET Core 2.0。您无法在 UWP 应用中使用此 API,这就是您收到此错误消息的原因。
标签: c# visual-studio uwp win-universal-app speech-to-text