简体   繁体   English

它是活的吗? Xamarin、C# 上的服务器连接

[英]Is it alive? server connection on Xamarin, C#

I wold like to do a is it alive?我想做一个它还活着吗? for check for my server connection on Xamarin.Forms.用于检查我在 Xamarin.Forms 上的服务器连接。 If the connection breaks, it will show an alert message.如果连接中断,它将显示一条警告消息。 When the connection is restore the alert message will desapier.当连接恢复时,警报消息将变得更加黯淡。

I have a http get request who checks the connection but I don't know how code the rest of the code.我有一个 http 获取请求来检查连接,但我不知道代码的 rest 是如何编码的。 I need a fuction who will running on the backgound checking the connection on all the pages of my app.我需要一个将在后台运行的功能,以检查我应用程序所有页面上的连接。

This is my request:这是我的要求:

    public async void Alive() {
        try {
            Uri uri = new Uri(Settings.basePath + "/alive");
            return await client.GetAsync(uri);
        } catch {
            return null;
        }
    }

How could I do this?我怎么能这样做?

I wold like to do a is it alive?我想做一个它还活着吗? for check for my server connection on Xamarin.Forms.用于检查我在 Xamarin.Forms 上的服务器连接。 If the connection breaks, it will show an alert message.如果连接中断,它将显示一条警告消息。 When the connection is restore the alert message will desapier.当连接恢复时,警报消息将变得更加黯淡。

You can create method in App.cs.您可以在 App.cs 中创建方法。 every 5 second to check server connection.每 5 秒检查一次服务器连接。

IDynamicAlert alert = DependencyService.Get<IDynamicAlert>();
    public App()
    {
        InitializeComponent();

        var seconds = TimeSpan.FromSeconds(5);
        Xamarin.Forms.Device.StartTimer(seconds, () =>
            {

               
                if (CheckConnection())
                {
                    if (alert != null)
                    {
                        alert.Dismiss();
                    }

                    return true;
                }
                else
                {
                    alert.Show("alert message", "connection is break!");
                    return true;

                }
            });

        MainPage =new NavigationPage( new simplecontrol.Page26());
       
    }
   
    private bool  CheckConnection()
    {
        //check your server connection
       if ()
        {
            //connection is alive.

        }
        else
        {
            //connection is break. 
        }
    }

I create custom ALertDialog, please following code;我创建自定义 ALertDialog,请遵循代码;

create interface in project share code.在项目共享代码中创建接口。

 public interface IDynamicAlert
{
    void Show(string title, string message);
  
    void Dismiss();
}

Then implementing this interface in Android platform.然后在Android平台实现这个接口。

[assembly: Dependency(typeof(DynamicAlert))]
 namespace FormsSample.Droid
{
public  class DynamicAlert : IDynamicAlert
{
    Android.App.AlertDialog.Builder builder;
    AlertDialog alert;
    public void Dismiss()
    {
        alert.Hide();
    }

    public void Show(string title, string message)
    {
        alert.SetTitle(title);
        alert.SetMessage(message);
        alert.SetButton("OK", (sender, args) => { });
        alert.SetCancelable(false);
        alert.Show();                  
    }
  
    public DynamicAlert()
    {
        builder = new AlertDialog.Builder(MainActivity.mac);
         alert = builder.Create();
    }           
}
 }

Create static MainActivity mac im MianActivity.cs.在 MianActivity.cs 中创建 static MainActivity mac。

 public static MainActivity mac;
    protected override void OnCreate(Bundle savedInstanceState)
    {
       
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;
        
        base.OnCreate(savedInstanceState);
        mac = this;               
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState); 
        LoadApplication(new App());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM