c# - Xamarin 显示警报未显示
问题描述
我有xamarin表单应用程序,我连接它的信号器没有运行我的 void。我在互联网上搜索,但我找不到任何关于此的内容。这是我的代码 Myhub.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client;
namespace PharmClient
{
class MyHub
{
string url = "https://webapplication11-co5.conveyor.cloud/";
HubConnection Connection;
IHubProxy ProxyOFServer;
public delegate void Error();
public delegate void MessageRecieved(string _data);
public event Error CoonectionError;
public event MessageRecieved OndataRecieved;
public delegate void Completed();
public event Completed OnCompleted;
public void Connect()
{
Connection = new HubConnection(url);
ProxyOFServer = Connection.CreateHubProxy("MuHub");
Start().ContinueWith(task => { if (task.IsFaulted) { CoonectionError?.Invoke(); } else { OnCompleted?.Invoke(); } });
}
public Task Start()
{
return Connection.Start();
}
public void SendData(string data)
{
ProxyOFServer.Invoke<string>("SendMessage", data);
}
public void Recive( )
{
ProxyOFServer.On<string>("Sentdata", data => { OndataRecieved?.Invoke(data); });
}
}
}
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.Threading;
namespace PharmClient
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
MyHub ConnectServer = new MyHub();
public MainPage()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
ConnectServer.OnCompleted += ConnectServer_OnCompleted;
ConnectServer.CoonectionError += ConnectServer_CoonectionError;
ConnectServer.Connect();
}
private void ConnectServer_OnCompleted()
{
DisplayAlert("Connected", "Good", "O");
}
private void ConnectServer_CoonectionError()
{
DisplayAlert("Failed", "Bad", "Ok");
}
private void SerchDrug_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new SearchDrug());
}
}
}
当连接失败时ConnectionError事件运行但连接将成功OnCompleted事件不会运行。我是一个学生。这是小组工作的一部分。有什么问题我的代码任何帮助。我什么也找不到。感谢关注
解决方案
正如您的标题所暗示的,您在显示对话框时遇到了问题。
尝试浏览文档(此处)一次以完全理解,您必须了解await
显示的过程DisplayAlert
。
将await
&添加async
到您的方法中。
尝试这个 -
private async void ConnectServer_OnCompleted()
{
await DisplayAlert("Connected", "Good", "O");
}
private async void ConnectServer_CoonectionError()
{
await DisplayAlert("Failed", "Bad", "Ok");
}
如果您对此有一些问题,请告诉我。
推荐阅读
- reactjs - 反应 this.state.dataSource.map 不是一个函数
- highcharts - 如何在 highcharts 图表上禁用辅助工具提示?
- json - Github - 允许用户从我的 Github 页面打开问题?
- python - 如何在 Python 中定义变量
- java - 客户端-服务器多线程聊天应用程序。客户端到客户端的通信
- variables - 由于 WooCommerce 的 Autoship,可变价格不会改变以反映买家的选择
- dart - 具有相似小部件的几个屏幕 - 如何编码(Flutter、Dart)
- c# - 为什么我不能删除 Excel 中的行?
- c++ - 替换文本文件中的元音
- tensorflow - ImportError:无法导入名称“音频”