首页 > 解决方案 > 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事件不会运行。我是一个学生。这是小组工作的一部分。有什么问题我的代码任何帮助。我什么也找不到。感谢关注

标签: c#asp.netxamlxamarin.formssignalr

解决方案


正如您的标题所暗示的,您在显示对话框时遇到了问题。

尝试浏览文档(此处)一次以完全理解,您必须了解await显示的过程DisplayAlert

await&添加async到您的方法中。

尝试这个 -

        private async void ConnectServer_OnCompleted()
        {
            await DisplayAlert("Connected", "Good", "O");
        }

        private async void ConnectServer_CoonectionError()
        {
            await DisplayAlert("Failed", "Bad", "Ok");
        }

如果您对此有一些问题,请告诉我。


推荐阅读