2016-12-04 61 views
1

我正在爲android和windows-phone的天氣應用程序工作。所以我在「Parsing Class」的「Weather.Api(Portable)」中的另一個項目中編寫通用代碼。 「Weather.Droid」用於Android。 image如何從另一個項目開始活動

ParsingClass的代碼如下:

public static string tempGlobal; 
    public static string cityTextGlobal; 
    private static string GovnoTemperature; 

    public async Task<string> dataByCity(string city) 
    { 
     var url = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&APPID="+AppID; 

     //ТОРМОЗИТ ЗДЕСЬ/BRAKES HERE 
     await FetchAsync(url); 
     return city; 
    } 

    public async Task<double> Data_down(double lat, double lon) 
    { 

     var url = String.Format(
      "http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&units=metric&APPID=" + AppID); 

     //ТОРМОЗИТ ЗДЕСЬ/BRAKES HERE 
     await FetchAsync(url); 

     return lat; 
    } 

    public async Task<string> FetchAsync(string url) 
    { 
     string jsonString; 

     using (var httpClient = new System.Net.Http.HttpClient()) 
     { 
      var stream = await httpClient.GetStreamAsync(url); 
      StreamReader reader = new StreamReader(stream); 
      jsonString = reader.ReadToEnd(); 
     } 

     var json = jsonString; 

     JsonValue firstitem = json; 
     var mydata = JObject.Parse(json); 

     cityTextGlobal = (mydata["name"]).ToString(); 

     string GovnoData = (mydata["main"]).ToString(); 

     //spliting string 
     string[] values = GovnoData.Split(','); 
     for (int i = 0; i < values.Length; i++) 
     { 
      values[i] = values[i].Trim(); 
      if (i == 0) 
      { 
       //tempGlobal = values[i]; 
       GovnoTemperature = values[i]; 
      } 
     } 
     tempGlobal = null; 
     foreach (char c in GovnoTemperature) 
     { 
      if (c == '.') 
      { 
       break; 
      } 
      if (c == '-' || char.IsDigit(c) == true || c == '.') 
      { 
       tempGlobal += c.ToString(); 
      } 
     } 

     return jsonString; 
    } 
} 

public class Coord 
{ 
    public double lon { get; set; } 
    public double lat { get; set; } 
} 

public class Weather 
{ 
    public int id { get; set; } 
    public string main { get; set; } 
    public string description { get; set; } 
    public string icon { get; set; } 
} 

public class Main 
{ 
    public double temp { get; set; } 
    public int pressure { get; set; } 
    public int humidity { get; set; } 
    public int temp_min { get; set; } 
    public int temp_max { get; set; } 
} 

public class Wind 
{ 
    public double speed { get; set; } 
    public double deg { get; set; } 
} 

public class Clouds 
{ 
    public int all { get; set; } 
} 

public class Sys 
{ 
    public int type { get; set; } 
    public int id { get; set; } 
    public double message { get; set; } 
    public string country { get; set; } 
    public int sunrise { get; set; } 
    public int sunset { get; set; } 
} 

public class RootObject 
{ 
    public Coord coord { get; set; } 
    public List<Weather> weather { get; set; } 
    public string @base { get; set; } 
    public Main main { get; set; } 
    public int visibility { get; set; } 
    public Wind wind { get; set; } 
    public Clouds clouds { get; set; } 
    public int dt { get; set; } 
    public Sys sys { get; set; } 
    public int id { get; set; } 
    public string name { get; set; } 
    public int cod { get; set; } 
} 

}

我要開始 「Weather.Droid」 的MainActivity從方法:

公共異步任務FetchAsync(串url)

它之前r eturns:

jsonString

我怎樣才能做到這一點?

回答

0

我需要從方法來啓動 「Weather.Droid」 的MainActivity:

公共異步任務FetchAsync(字符串URL)

您無法直接從Droid項目啓動MainActivity。但正如@SKall說,你可以在你的FetchAsync方法使用Action<Intent>

//add Action<Intent> as and Intent as parameters 
public async Task<string> FetchAsync(string url,Action<Intent> navigationFunc,Intent intent) 
{ 
    string jsonString; 
    ... 
    navigationFunc(intent);//Invoke the action 
    return jsonString; 
} 

然後在你的Droid項目中使用它:

ParsingClass parsing = new ParsingClass(); 
Intent intent = new Intent(this, typeof(MainActivity)); 
await parsing.FetchAsync("url", StartActivity, intent); 
+0

Intent位於Android命名空間中,因此您不能使用它。只需創建一個簡單的動作。 – SKall

+0

工作正常!謝謝你們兩位! –

1

它看起來像你在之後是依賴注入。在便攜式項目中聲明一個接口,然後在Droid/iOS /中實現它?您支持的項目。

我在XLabs社區項目中創建了IOC的抽象。 Matt Whetton寫了一些相當不錯的說明如何使用它:http://www.codenutz.com/getting-started-xamarin-forms-labs-xaml-mvvm-ioc/

以下方法並不理想,但有點不清楚爲什麼你需要調用活動開始,至少這會給你一些想法。

在便攜方面:

public class Class1 
{ 
    public static async Task<string> Fetch(string url, Action onComplete = null) 
    { 
     await Task.Delay(10); 


     onComplete?.Invoke(); 
     return url; 
    } 
} 

在Android上側(這將是您的閃屏或應用程序,如果你的目的是要做到這一點取主屏幕出現之前):

[Activity(Label = "App5", MainLauncher = true, Icon = "@drawable/icon")] 
public class MainActivity : Activity 
{ 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     // Set our view from the "main" layout resource 
     SetContentView (Resource.Layout.Main); 

     Class1.Fetch("test",() => 
     { 
      var intent = new Intent(this, typeof(OtherActivity)); 
      this.RunOnUiThread(() => StartActivity(intent)); 
     }).ContinueWith(t => 
     { 
      if (t.IsCompleted) Log.Debug("Fetch", t.Result); 
     }); 
    } 
} 
+0

我只想從ParsingClass調用被放置在Weater任何方法.Droid項目。這是我的問題。 –

+0

你是從Android項目調用這個方法嗎?如果是這樣,您可以將Action添加到方法參數並在方法返回之前調用它。如果不是那麼國際奧委會是你最好的選擇。 – SKall

+0

我寫了公開的方法,在Android項目中啓動需要的活動。但問題是從Weather.Api項目調用此方法。我試圖做一個參考。但這是不可能的。該錯誤的文本是:無法添加引用Weather.roid。添加這個項目作爲參考會導致循環依賴 –

相關問題