验证Internet连接或Azure服务 - Xamarin

时间:2017-06-12 18:44:31

标签: c# azure xamarin uwp xamarin.forms

我在Xamarin(跨平台)开发了一个应用程序,现在运行得很好!!但总是需要互联网连接。 如果互联网连接失败,我的应用程序会出现意外关机。我想控制这一点。 首先,我有“AzureDataService”类:

public class AzureDataService 
{ 
    //Conexion to backend  
    public MobileServiceClient MobileService { get; set; } 

    //Object of "Ficha" class.  
    IMobileServiceSyncTable<Ficha> tablaFicha; 

    public async Task Initialize() 
    {
        if (isInitialized) 
            return;
        MobileService = new MobileServiceClient("http://linkbdd");

        //Save data in a local DB, later upload with internet connection
        const string path = "bbddMuestra.db";

        var store = new MobileServiceSQLiteStore(path);//Create DB

        store.DefineTable<Ficha>();

        //async initialization
        await MobileService.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler());

        //Inicializate table
        tablaFicha = MobileService.GetSyncTable<Ficha>();

        isInitialized = true;
    }

    //Here get data 
    public async Task<IEnumerable<Ficha>> GetData() 
    {
        await Initialize();
        await SyncFicha();
        //Select data...
        return await tablaFicha.OrderBy(a => a.Id).ToEnumerableAsync();
    }   

    public async Task SyncFicha()
    {
        await tablaVehiculos.PullAsync("Ficha", tablaFicha.CreateQuery());
        await MobileService.SyncContext.PushAsync();
    }

“AzureDataService”类的结尾。现在是实现AzureDataService的类。

public partial class ListaFichas : ContentPage 
{ 
    public static ObservableCollection ficha;

    public ListaFichas () 
    { 
        InitializeComponent ();  
        ficha = new ObservableCollection(); 
    }  

    protected async override void OnAppearing()
    {   
        base.OnAppearing();
        ficha.Clear();      

        //Next line get data from previous method of class "AzureDataService"
        var ligas = await App.AzureService.GetData();
        foreach(var item in ligas)
        {
            Ficha fi = item;
            ficha.Add(fi);
        }   
        //Here bind listview with data that previous get
        lsvFichas.ItemsSource = ficha;
    }

请帮帮我。我想显示一个Display或DisplayActionSheet来通知用户......但绝不会意外关机。 谢谢!...

2 个答案:

答案 0 :(得分:0)

您必须检查互联网连接

以下链接对您有用

https://forums.xamarin.com/discussion/comment/276931#Comment_276931

答案 1 :(得分:0)

AFAIK,如果您致电PushAsync 推送同步内容,那么针对离线表的创建,更新和删除列表将逐个发​​送到Azure应用服务。 PullAsyncPushAsync操作都需要您的移动客户端在线。

根据我的理解,您可以在致电SyncFicha之前检查互联网连接,以便同步您的数据。此外,您可以使用SyncFicha方法将try-catch方法用于处理互联网连接,并在推送离线数据时发生冲突。我建议你可以参考adrian hall的书中的以下教程:

  • Detecting Connection State

    使用Xam.Plugin.Connectivity检查连接状态,如下所示:

    await Initialize();
    
    if (!(await CrossConnectivity.Current.IsRemoteReachable(Client.MobileAppUri.Host, 443)))
    {
        Debug.WriteLine($"Cannot connect to {Client.MobileAppUri} right now - offline");
        return;
    }
    
    await tablaVehiculos.PullAsync("Ficha", tablaFicha.CreateQuery());
    await MobileService.SyncContext.PushAsync();
    
  • Handling Conflict Resolution