2015-01-30 2 views
0

저는 Azure 모바일 서비스 및 Xamarin Android로 작업 해 왔으며 현재 모바일 서비스 클라이언트를 재사용하는 방법에 대해 잘 알고 있지 않습니다. 지금까지 본 모든 Xamarin Android 예제는 클라이언트에 대한 새로운 참조를 만드는 단일 액티비티를 사용합니다. 한 번 클라이언트에 대한 참조를 만들고 여러 활동에서 다시 사용하고자합니다.azure 모바일 서비스 클라이언트 재사용

나는 지금까지 this tutorial을 따라 갔지만 여러 가지 활동에 대해이 작업을 수행하는 방법에 대해 난처하지 않습니다. 나는 앱 전체에서이 클라이언트의 새로운 인스턴스를 계속 만들고 싶지 않습니다.

이 작업을 수행하는 동기 중 하나는 새 클라이언트 참조를 만들 때마다 다시 인증을 유지하고 싶지 않기 때문입니다. 이상적으로는 클라이언트를 한 번 생성하고 인증 한 다음 내 활동 전체에서 해당 클라이언트를 다시 사용하는 것이 좋습니다.

나는이 도구로 작업하는 경험이 거의 없으므로이 요소를 배제하고 있으므로이 작업을 수행하는 방법에 대한 지침 (또는이 작업을 수행하지 않는 이유와 올바르게 수행하는 방법)을 높이 평가할 수 있습니다.

답변

0

는 응용 프로그램에서이

public static class AzureMobileService 
 
    { 
 
     /// <summary> 
 
     /// Initializes static members of the <see cref="AzureMobileService"/> class. 
 
     /// </summary> 
 
     static AzureMobileService() 
 
     { 
 

 
      Instance = new MobileServiceClient("http://<your url>.azure-mobile.net/", "< you key>") 
 
      { 
 
       SerializerSettings = new MobileServiceJsonSerializerSettings() 
 
       { 
 
        CamelCasePropertyNames = true 
 
       } 
 
      }; 
 
     } 
 

 
     /// <summary> 
 
     /// Gets or sets the Instance. 
 
     /// </summary> 
 
     /// <value> 
 
     /// The customers service. 
 
     /// </value> 
 
     public static MobileServiceClient Instance { get; set; } 
 
    }

같은 정적 클래스를 만들 수 있습니다 그리고 당신이 그것을 필요로 할 때마다 당신은 사용해야

AzureMobileService.Instance

이 방법으로 클라이언트 인스턴스가 현재 페이지와 상관없이 동일하게 적용됩니다.

0

정적 클래스를 사용하는 대신 다른 방법은 Application 클래스를 사용하는 것입니다. Application 클래스는 앱의 루트이며 앱의 수명주기 동안 메모리에 유지됩니다.

[Application] 
public class AppInitializer : Application 
{ 
    private static Context _appContext; 

    public MobileServiceClient ServiceClient { get; set; } 

    public override void OnCreate() 
    { 
     base.OnCreate(); 

     ServiceClient = new MobileServiceClient("http://<your url>.azure-mobile.net/", "< you key>") 
     { 
      SerializerSettings = new MobileServiceJsonSerializerSettings() 
      { 
       CamelCasePropertyNames = true 
      } 
     }; 
    } 

    public static Context GetContext() 
    { 
     return _appContext; 
    } 
} 

그런 활동 안에 당신은 다음과 같이 사용할 수 있습니다 :

public class MainActivity : Activity 
{ 
    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 

     var appState = (AppInitializer) ApplicationContext; 

     //appState.ServiceClient 
    } 
} 
관련 문제