2016-10-04 6 views
0

私はXamarinフォームからREST APIにアクセスしています。私はこのようなポータブルクラスのインターフェイスを作成しました。Xamarinからの依存関係サービスへの挑戦

public interface IRestService<T> 
    { 
     void Post(T item, string resourceURL); 
    } 

    public interface IRepository 
    { 

    } 

とDrod Projectで私はこのようなインターフェイスを実装しています。 Xamarin.Formsに呼び出し

[assembly:Xamarin.Forms.Dependency(typeof(RestOperationsDroid))] 
namespace VLog.Droid.DependencyServices 
{ 
    public class RestOperationsDroid : IRestService<IRepository> 
    { 
     private const string BaseURL = "http://127.0.0.1/logger/v1/"; 
     HttpClient client; 

     public RestOperationsDroid() 
     { 
      client = new HttpClient(); 
      client.MaxResponseContentBufferSize = 256000; 
     } 

     public async void Post(object item, string url) 
     { 
      var uri = new Uri(string.Format(BaseURL + url)); 
      var jsoncontent = new StringContent(JsonConvert.SerializeObject(item), Encoding.UTF8, "application/json"); 
      HttpResponseMessage response = null; 
      response = await client.PostAsync(uri, jsoncontent); 

      if (response.IsSuccessStatusCode) 
      { 
       //Debug(@"    TodoItem successfully saved."); 
      } 
      else 
      { 
       //throw new Exception("Failed to Post data"); 
      } 
     } 

    } 
} 

(PCLプロジェクト)

DependencyService.Get<IRestService<Log>>().Post(_logitem, "sync"); 

私はエラーを取得しています氏は述べています:

オブジェクト参照がオブジェクトのインスタンスに設定されていません。

この実装で何が問題になりましたか?

+0

'IRepository'と' Log'の関係は何ですか? – hvaughan3

答えて

0

あなたのクラスがIRestService<IRepository>を実装している間にIRestService<Log>DependencyServiceから解決しようとしています。これらは異なるタイプです。 IRestService<T>に必要な特定のタイプを実装するクラスを実装する必要があります。

+0

@NaveenBathina私は自分の答えを変えました。実際には同じ理由があります。 –

関連する問題