Paging with Xamarin - c#

I am trying to load old Instagram pictures when the user pulls the UITableView to the bottom of the screen using Xamarin. However I seem to have no clue how to do it. I got it working fine in Objective-C it just seems different in C#. Here is how I am getting the initial posts:
public void getInstagramFeed(UITableView table){
IEnumerable<Account> accounts = AccountStore.Create ().FindAccountsForService ("Instagram");
var enumerable = accounts as IList<Account> ?? accounts.ToList ();
if (enumerable.Any ()) {
Account instagram = enumerable.First ();
var instagramAccessToken = instagram.Properties ["access_token"].ToString ();
var request = new RestRequest { RootElement = "data", Resource = "/users/self/feed" };
request.AddParameter ("access_token", instagramAccessToken);
var client = new RestClient ("https://api.instagram.com/v1");
client.ExecuteAsync (request, response => {
table.InvokeOnMainThread (() => {
RootObject rootObject = JsonConvert.DeserializeObject<RootObject> (response.Content);
var dataSource = new ObservableDataSource<Datum> (rootObject.data);
dataSource.Bind (table);
});
});
}
}
Here is the bind method for my datasource:
public void Bind(Datum datum)
{
this.datum = datum;
if (this.datum == null || this.datum.caption == null)
{
this.captionLabel.Text = "";
}
else
{
this.captionLabel.Text = datum.caption.text;
}
this.pictureImage.InvokeOnMainThread (() => this.pictureImage.SetImage (
url: new NSUrl (datum.images.standard_resolution.url)
)
);
this.profileImage.InvokeOnMainThread (() => this.profileImage.SetImage (
url: new NSUrl (datum.user.profile_picture)
)
);
this.nameLabel.Text = this.datum.user == null ? "user is null" : datum.user.full_name;
}
Here is the class for my tableView:
public TableView ()
{
}
public TableView (IntPtr handle) : base(handle)
{
}
public UITableViewCell GetCell (Datum item)
{
var newCell = this.DequeueReusableCell(InstagramCell.Key)
as InstagramCell ?? InstagramCell.Create();
newCell.Bind (item);
return newCell;
}
public float GetHeightForRow (NSIndexPath indexPath)
{
return 340f;
}
Thanks for all the help before hand, sorry for the long post :)
// PS This is how I did it in OBJc:
NSDictionary *page = instagramResponse[#"pagination"];
NSString *nextPage = page[#"next_url"];
[[InstagramClient sharedClient] getPath:[NSString stringWithFormat:#"%#",nextPage] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
instagramResponse = [responseObject mutableCopy];
[instagramResponse addEntriesFromDictionary:responseObject];
[instapics addObjectsFromArray:responseObject[#"data"]];
[self updateArrays];
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Failure: %#", error);
}];
// Class for InstagramClient:
public class RootObject
{
public Pagination pagination { get; set; }
public Meta meta { get; set; }
public List<Datum> data { get; set; }
}
public class Datum
{
public object attribution { get; set; }
public List<string> tags { get; set; }
public string type { get; set; }
public object location { get; set; }
public Comments comments { get; set; }
public string filter { get; set; }
public string created_time { get; set; }
public string link { get; set; }
public Likes likes { get; set; }
public Images images { get; set; }
public List<object> users_in_photo { get; set; }
public Caption caption { get; set; }
public bool user_has_liked { get; set; }
public string id { get; set; }
public User user { get; set; }
public Videos videos { get; set; }
public override string ToString()
{
if (user == null)
{
return "User is null";
}
return user.full_name;
}
}
public class Videos
{
public LowResolution2 low_resolution { get; set; }
public StandardResolution2 standard_resolution { get; set; }
}
public class StandardResolution2
{
public string url { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class LowResolution2
{
public string url { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class User
{
public string username { get; set; }
public string website { get; set; }
public string profile_picture { get; set; }
public string full_name { get; set; }
public string bio { get; set; }
public string id { get; set; }
}
public class Caption
{
public string created_time { get; set; }
public string text { get; set; }
public From from { get; set; }
public string id { get; set; }
}
public class From
{
public string username { get; set; }
public string profile_picture { get; set; }
public string id { get; set; }
public string full_name { get; set; }
}
public class Images
{
public LowResolution low_resolution { get; set; }
public Thumbnail thumbnail { get; set; }
public StandardResolution standard_resolution { get; set; }
}
public class StandardResolution
{
public string url { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class Thumbnail
{
public string url { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class LowResolution
{
public string url { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class Likes
{
public int count { get; set; }
public List<Datum2> data { get; set; }
}
public class Datum2
{
public string username { get; set; }
public string profile_picture { get; set; }
public string id { get; set; }
public string full_name { get; set; }
}
public class Comments
{
public int count { get; set; }
public List<object> data { get; set; }
}
public class Meta
{
public int code { get; set; }
}
public class Pagination
{
public string next_url { get; set; }
public string next_max_id { get; set; }
}

I would recommend using a UiCollectionViewController for this if possible. Or nowadays preferably the equivalent in Xamarin.Forms. With UiCollectionViewController you can easily load more items as the user scrolls down

Related

E.F Core does not return all values When Include another table

public IEnumerable<Parties> GetAll()
{
return database.Parties;
}
Works very well and the output is:
But when I Include another table by foreignkey like this:
public IEnumerable<Parties> GetAll()
{
return database.Parties.Include(i=>i.User);
}
It does not work, it returns first value of the table and nothing else,the output is :
Users.cs :
public partial class Users
{
public Users()
{
Parties = new HashSet<Parties>();
PartyParticipants = new HashSet<PartyParticipants>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string Avatar { get; set; }
public string Biography { get; set; }
public string Password { get; set; }
public virtual ICollection<Parties> Parties { get; set; }
public virtual ICollection<PartyParticipants> PartyParticipants { get; set; }
}
Parties.cs :
public partial class Parties
{
public Parties()
{
Image = new HashSet<Image>();
PartyParticipants = new HashSet<PartyParticipants>();
}
public int Id { get; set; }
public string Name { get; set; }
public DateTime PartyDate { get; set; }
public DateTime CreatedDate { get; set; }
public int ParticipantCount { get; set; }
public int MaxParticipant { get; set; }
public string PartySplash { get; set; }
public string ShortDescription { get; set; }
public string Description { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public bool EntranceFree { get; set; }
public int? FreeParticipant { get; set; }
public int? FreeParticipantMax { get; set; }
public int UserId { get; set; }
public virtual Users User { get; set; }
public virtual ICollection<Image> Image { get; set; }
public virtual ICollection<PartyParticipants> PartyParticipants { get; set; }
}
As you can see on the 2nd picture it interrupts at first row of the table.
I have added this answer based on Vidmantas's comment. ReferenceLoopHandling should be ignored like this in startup.cs:
services.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});

Creating Viewmodels and mapping to entities

I am creating ViewModels for my entities. The FirmViewModel contains two list collections called Addressess and Websites. I have also created viewmodels for Addresses and Websites. At the moment in the controller code, I am getting trouble assigning Addresses and Wesbite collections
of the Firm Entity to FirmViewModels Addresses and Websites collection. Its says cant cast it implicitly. This is the line in the controller that it complains Addresses = firm.Addresses; How do aassign
Systems.Collections.Generic.ICollection<Manager.Model.Address> to Systems.Collections.Generic.ICollection<Manager.WebUI.ViewModels.AddressViewModel>
NewFirmViewModel
public class NewFirmViewModel
{
public int FirmId { get; set; }
public string FirmName { get; set;}
public Nullable<DateTime> DateFounded { get; set; }
public ICollection<AddressViewModel> Addresses { get; set; }
public ICollection<WebsiteViewModel> Websites { get; set; }
public bool hasIntralinks { get; set; }
}
AddressViewModel
public class AddressViewModel
{
public int AddressId { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
public string Phone { get; set; }
public bool IsHeadOffice { get; set; }
public int FirmId { get; set; }
}
WebsiteViewModel
public class WebsiteViewModel
{
private int FirmWebsiteId { get; set; }
private string WebsiteUrl { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public int FirmId { get; set; }
}
Entities - Firm
public class FIRM: Entity,IHasAUMs<FIRM_AUM>
{
public FIRM()
{
//this.FIRM_PERSON = new HashSet<FIRM_PERSON>();
this.MANAGERSTRATEGies = new HashSet<MANAGERSTRATEGY>();
this.FIRM_ACTIVITY = new HashSet<FIRM_ACTIVITY>();
this.FIRM_AUMs = new HashSet<FIRM_AUM>();
this.FIRM_REGISTRATION = new HashSet<FIRM_REGISTRATION>();
//this.ACTIVITies = new HashSet<ACTIVITY>();
Addresses = new HashSet<ADDRESS>();
//People = new HashSet<PERSON>();
// Websites = new HashSet<FIRM_WEBSITE>();
}
//public decimal ID { get; set; }
//
//
//
//
public string NAME { get; set; }
public string SHORT_NAME { get; set; }
public string ALTERNATE_NAME { get; set; }
public string WEBSITE { get; set; }
public string WEBSITE_USERNAME { get; set; }
public string WEBSITE_PASSWORD { get; set; }
public bool? INTRALINKS_FIRM { get; set; }
public string NOTES_TEXT { get; set; }
public string NOTES_HTML { get; set; }
public string HISTORY_TEXT { get; set; }
public string HISTORY_HTML { get; set; }
public string HISTORY_SUM_TEXT { get; set; }
public string HISTORY_SUM_HTML { get; set; }
public Nullable<decimal> OLD_ORG_REF { get; set; }
public Nullable<decimal> SOURCE_ID { get; set; }
[DisplayFormat(DataFormatString = PermalConstants.DateFormat)]
public Nullable<DateTime> DATE_FOUNDED { get; set; }
public virtual ICollection<ADDRESS> Addresses { get; set; }
// public ICollection<FIRM_WEBSITE> Websites { get; set; }
// public ICollection<PERSON> People { get; set; }
//public SOURCE SOURCE { get; set; }
// public ICollection<FIRM_PERSON> FIRM_PERSON { get; set; }
public ICollection<MANAGERSTRATEGY> MANAGERSTRATEGies { get; set; }
public ICollection<FIRM_ACTIVITY> FIRM_ACTIVITY { get; set; }
public ICollection<FIRM_REGISTRATION> FIRM_REGISTRATION { get; set; }
//public ICollection<ACTIVITY> ACTIVITies { get; set; }
public ICollection<FIRM_WEBSITE> Websites { get; set; }
public Nullable<int> KEY_CONTACT_ID { get; set; }
[NotMapped]
public ICollection<FIRM_AUM> AUMs
{
get
{
return this.FIRM_AUMs;
}
}
public ICollection<FIRM_AUM> FIRM_AUMs { get; set; }
}
ADDRESS
public class ADDRESS : Entity
{
public ADDRESS()
{
// DATE_CREATED = DateTime.Now;
}
public string LINE1 { get; set; }
public string LINE2 { get; set; }
public string LINE3 { get; set; }
public int CITY_ID { get; set; }
public string POSTAL_CODE { get; set; }
public string SWITCHBOARD_INT { get; set; }
public string NOTES { get; set; }
public int? OLD_ADDRESS_REF { get; set; }
public int? SOURCE_ID { get; set; }
public int FIRM_ID { get; set; }
[ForeignKey("FIRM_ID")]
public FIRM FIRM { get; set; }
[ForeignKey("CITY_ID")]
public CITY City { get; set; }
public ICollection<PERSON> People { get; set; }
// public SOURCE SOURCE { get; set; }
public bool IS_HEAD_OFFICE { get; set; }
[NotMapped]
public string AddressBlurb
{
get
{
return string.Join(",", new[] { LINE1, LINE2, City != null ? City.NAME : "", City != null && City.Country != null ? City.Country.NAME : "" }.Where(x => !string.IsNullOrEmpty(x)));
}
}
}
FIRM_WEBSITE
public class FIRM_WEBSITE : Entity
{
public FIRM_WEBSITE()
{
}
private string _WEBSITE_URL;
public string WEBSITE_URL
{
get
{
if (string.IsNullOrEmpty(_WEBSITE_URL))
return _WEBSITE_URL;
try
{
var ubuilder = new System.UriBuilder(_WEBSITE_URL ?? "");
return ubuilder.Uri.AbsoluteUri;
}
catch (UriFormatException ex)
{
return _WEBSITE_URL;
}
}
set { _WEBSITE_URL = value; }
}
public string USERNAME { get; set; }
public string PASSWORD { get; set; }
public int FIRM_ID { get; set; }
[ForeignKey("FIRM_ID")]
public FIRM FIRM { get; set; }
}
FirmController
public class FirmController : ApiControllerBase
{
[HttpGet]
[Route("api/Firm/{id}")]
public IHttpActionResult Details(int id)
{
var viewModel = GetFirmViewModel(id);
return Ok(viewModel);
}
private NewFirmViewModel GetFirmViewModel(int id)
{
var firmSvc = GetService<FIRM>();
var firm = firmSvc.GetWithIncludes(id, f => f.Addresses, f => f.Websites);
var firmVm = new NewFirmViewModel()
{
FirmId = firm.ID,
FirmName = firm.NAME,
DateFounded = firm.DATE_FOUNDED,
Addresses = firm.Addresses;
};
}
public virtual T GetWithIncludes(int id, params Expression<Func<T, object>>[] paths)
{
try
{
using (new TimedLogger(_perfLogger, GetCompletedText("GetWithIncludes"), typeof(T).Name))
{
return Authorize(_repo.GetWithIncludes(id, paths), AuthAccessLevel.Read);
}
}
catch (Exception ex) { Log(ex); throw; }
}

Trouble with List (UWP)

I writing app for UWP platform.
I using Binding in code.
Here code for my ViewModel:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Milano.Annotations;
using WooCommerceNET;
using WooCommerceNET.WooCommerce;
using Newtonsoft.Json;
namespace Milano.Classes
{
public class InWorkViewModel: INotifyPropertyChanged
{
private List<LineItem> productList;
public List<LineItem> ProductList
{
get { return productList; }
set { productList = value; OnPropertyChanged(); }
}
private List<RootObject> ordersList;
public List<RootObject> OrdersList
{
get { return ordersList; }
set { ordersList = value; OnPropertyChanged(); }
}
private RootObject ordersChange;
public RootObject OrdersChange
{
get { return ordersChange; }
set { ordersChange = value; OnPropertyChanged(); }
}
private LineItem ordersChange2;
public LineItem OrdersChange2
{
get { return ordersChange2; }
set { ordersChange2 = value;OnPropertyChanged(); }
}
public InWorkViewModel()
{
Inwork_down();
// var interval = 100000;
//UpdateWithImterwal(interval);
}
/* private async void UpdateWithImterwal(int interval)
{
// OrdersList.Clear();
Inwork_down();
await Task.Delay(interval).ContinueWith(_ => UpdateWithImterwal(interval));
}*/
public async void Inwork_down()
{
RestAPI rest = new RestAPI("http://simplegames.com.ua/wp-json/wc/v1/", "ck_9d64c027d2c5f81b8bed3342eeccc6d337be813d", "cs_60697b1e6cbdeb8d62d19e0765e339f8e3334754");
WCObject wc = new WCObject(rest);
//Get all products
var orders = await wc.GetOrders(new Dictionary<string, string>() {
{ "per_page", "100" }, { "status","processing"} }); // Dodelat filtr dlaya teh chto v rabote
string products = orders.ToFormattedJsonString();
Debug.WriteLine(products);
List<RootObject> rootObjectData = JsonConvert.DeserializeObject<List<RootObject>>(products);
OrdersList = new List<RootObject>(rootObjectData);
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Billing
{
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address_1 { get; set; }
public string address_2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postcode { get; set; }
public string country { get; set; }
public string email { get; set; }
public string phone { get; set; }
}
public class Shipping
{
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address_1 { get; set; }
public string address_2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postcode { get; set; }
public string country { get; set; }
}
public class LineItem
{
public int id { get; set; }
public string name { get; set; }
public string sku { get; set; }
public int product_id { get; set; }
public int variation_id { get; set; }
public int quantity { get; set; }
public string tax_class { get; set; }
public double price { get; set; }
public double subtotal { get; set; }
public double subtotal_tax { get; set; }
public double total { get; set; }
public double total_tax { get; set; }
public List<object> taxes { get; set; }
public List<object> meta { get; set; }
}
public class ShippingLine
{
public int id { get; set; }
public string method_title { get; set; }
public string method_id { get; set; }
public double total { get; set; }
public double total_tax { get; set; }
public List<object> taxes { get; set; }
}
public class RootObject
{
public int id { get; set; }
public int parent_id { get; set; }
public string status { get; set; }
public string order_key { get; set; }
public string currency { get; set; }
public string version { get; set; }
public bool prices_include_tax { get; set; }
public string date_created { get; set; }
public string date_modified { get; set; }
public int customer_id { get; set; }
public double discount_total { get; set; }
public double discount_tax { get; set; }
public double shipping_total { get; set; }
public double shipping_tax { get; set; }
public double cart_tax { get; set; }
public double total { get; set; }
public double total_tax { get; set; }
public Billing billing { get; set; }
public Shipping shipping { get; set; }
public string payment_method { get; set; }
public string payment_method_title { get; set; }
public string transaction_id { get; set; }
public string customer_ip_address { get; set; }
public string customer_user_agent { get; set; }
public string created_via { get; set; }
public string customer_note { get; set; }
public string date_completed { get; set; }
public string date_paid { get; set; }
public string cart_hash { get; set; }
public List<LineItem> line_items { get; set; }
public List<object> tax_lines { get; set; }
public List<ShippingLine> shipping_lines { get; set; }
public List<object> fee_lines { get; set; }
public List<object> coupon_lines { get; set; }
}
Where is my problem.
As you see I have this public List<LineItem> line_items { get; set; } in classes. I need to take this list from RootObject and make Binding for values in it like I do for rootObject.
So what is logic of View in app. I BinŠ² some data to Left panel, when I tap element on left panel, I make visible right panel, where I bind properties from public RootObject OrdersChange.
Here is some screens:
How I can do this?
If I understand correctly, you want bind 2 classes to one view. If so, I can suggest 2 ways:
1) Create base class for this 2 classes.
class BaseClass { }
class RootObject: BaseClass { }
class LineItem : BaseClass { }
class MainViewModel
{
public List<LineItem> ProductList { get; set; }
public List<RootObject> OrdersList { get; set; }
public BaseClass LeftListViewSelectedItem { get; set; }
}
2) Or you can set return type of property LeftListViewSelectedItem to object. For binding it's no matter what you return.

Convert Json Http request to c# obj

I`m new in programming winodws 8 app , i have a webservice when i try to connect it using httprequest (Using URL with variables), this service return this:
{"d":"{\"sessionid\":\"twzv50okccwvgggeesjje2wa\",\"VersionInfo\":{\"Rel\":0,\"Ver\":0,\"Patch\":0,\"ForceUpdate\":0,\"UpdateType\":0,\"Globals\":{\"MultiSessionsAllowed\":true,\"CommCalcType\":2,\"PriceChangedTimer\":25,\"ValidLotsLocation\":2,\"CustumizeTradeMsg\":false,\"FirstWhiteLabeledOffice\":null,\"DealerTreePriv\":0,\"ClientConnectTimer\":200,\"ClientTimeoutTimer\":500,\"DefaultLots\":0.01,\"WebSecurityID\":\"agagag\",\"ServerGMT\":3}},\"SystemLockInfo\":{\"MinutesRemaining\":0,\"HoursRemaining\":0,\"DaysRemaining\":0,\"Maintanance\":0,\"WillBeLocked\":1},\"FirstWhiteLabel\":\"VertexFX 10\",\"WLID\":\"3\",\"CheckWhiteLabel\":true,\"Password\":\"1444\",\"Username\":\"test\",\"LastTickTime\":\"\/Date(1396307573431)\/\",\"SelectedAccount\":78821860,\"Name\":0,\"ServicePath\":null,\"GWSessionID\":\"56630\",\"IP\":\"Web (212.35.90.211)\",\"SessionDateStart\":\"01/04/2014 02:12:53\",\"CompanyName\":\"Hybrid Solutions\",\"UserId\":6119,\"DemoClient\":\"0\",\"FName\":\"omqrstu\",\"SName\":\"\",\"TName\":\"\",\"LName\":\"\",\"Sms\":null,\"isReadOnly\":\"0\",\"SchSms\":\"2\",\"AlertSms\":\"2\",\"Temp\":null,\"GMTOffset\":\"2\",\"SvrGMT\":\"3\",\"ClientType\":null,\"EnableNews\":\"1\",\"PublicSlideNews\":\"\",\"PrivateSlideNews\":\"Welcome to V 10\",\"DealerTreePriv\":1}"}
i have simple windows app with one button when i click the button i send the url with variables and i got the obj above , i want to use content of this object like UserID in if statement but with no vain i dont know how to use it in C# , can and body help me??
I use this code. I know it has many errors but I need someone guide me.
private void Button_Click(object sender, RoutedEventArgs e)
{
String uriString = "url";
var uri = new Uri(uriString);
var httpWebRequest = HttpWebRequest.Create(uri);
httpWebRequest.BeginGetResponse(new AsyncCallback(OnGettingResponse), httpWebRequest);
}
private void OnGettingResponse(IAsyncResult ar)
{
var outerRoot = JsonConvert.DeserializeObject<OuterRootObject>( json );
var root = JsonConvert.DeserializeObject<RootObject>( outerRoot.d );
MessageBox.Show(root.UserId);
}
This is kind of a nasty situation. What you're getting back is a JSON object with a single property (i.e. d) and that property contains a string of JSON. So you basically need to unwrap the inner JSON from it's envelope. The following classes/code should work (using JSON.NET to do the deserialization).
public class OuterRootObject
{
public string d { get; set; }
}
public class Globals
{
public bool MultiSessionsAllowed { get; set; }
public int CommCalcType { get; set; }
public int PriceChangedTimer { get; set; }
public int ValidLotsLocation { get; set; }
public bool CustumizeTradeMsg { get; set; }
public object FirstWhiteLabeledOffice { get; set; }
public int DealerTreePriv { get; set; }
public int ClientConnectTimer { get; set; }
public int ClientTimeoutTimer { get; set; }
public double DefaultLots { get; set; }
public string WebSecurityID { get; set; }
public int ServerGMT { get; set; }
}
public class VersionInfo
{
public int Rel { get; set; }
public int Ver { get; set; }
public int Patch { get; set; }
public int ForceUpdate { get; set; }
public int UpdateType { get; set; }
public Globals Globals { get; set; }
}
public class SystemLockInfo
{
public int MinutesRemaining { get; set; }
public int HoursRemaining { get; set; }
public int DaysRemaining { get; set; }
public int Maintanance { get; set; }
public int WillBeLocked { get; set; }
}
public class RootObject
{
public string sessionid { get; set; }
public VersionInfo VersionInfo { get; set; }
public SystemLockInfo SystemLockInfo { get; set; }
public string FirstWhiteLabel { get; set; }
public string WLID { get; set; }
public bool CheckWhiteLabel { get; set; }
public string Password { get; set; }
public string Username { get; set; }
public DateTime LastTickTime { get; set; }
public int SelectedAccount { get; set; }
public int Name { get; set; }
public object ServicePath { get; set; }
public string GWSessionID { get; set; }
public string IP { get; set; }
public string SessionDateStart { get; set; }
public string CompanyName { get; set; }
public int UserId { get; set; }
public string DemoClient { get; set; }
public string FName { get; set; }
public string SName { get; set; }
public string TName { get; set; }
public string LName { get; set; }
public object Sms { get; set; }
public string isReadOnly { get; set; }
public string SchSms { get; set; }
public string AlertSms { get; set; }
public object Temp { get; set; }
public string GMTOffset { get; set; }
public string SvrGMT { get; set; }
public object ClientType { get; set; }
public string EnableNews { get; set; }
public string PublicSlideNews { get; set; }
public string PrivateSlideNews { get; set; }
public int DealerTreePriv { get; set; }
}
var outerRoot = JsonConvert.DeserializeObject<OuterRootObject>( json );
var root = JsonConvert.DeserializeObject<RootObject>( outerRoot.d );

Instagram to TableView w/ RestSharp

Hello Im trying to load the individual names of people in my Instagram feed onto a UITableView using Xamarin.iOS. However it only shows the name of the classes as you can see here:
Here is my request that I am using to try to pull the instagram feed:
var request = new RestRequest {RootElement = "data", Resource = "/users/self/feed"};
request.AddParameter("access_token", instagramAccessToken);
var client = new RestClient ("https://api.instagram.com/v1");
client.ExecuteAsync<List<RootObject>>(request, response => {
Console.WriteLine(response.Content);
Console.WriteLine(response.Data);
InvokeOnMainThread(delegate {
// pass the data to the TableSource class
((TableSource<RootObject>)table.Source).Data = response.Data;
// make the TableView reload the data
table.ReloadData();
});
});
Here is the class file for the incoming Instagram Feed:
public class Pagination
{
public string next_url { get; set; }
public string next_max_id { get; set; }
}
public class Meta
{
public int code { get; set; }
}
public class Location
{
public double latitude { get; set; }
public double longitude { get; set; }
public string name { get; set; }
public int? id { get; set; }
}
public class Comments
{
public int count { get; set; }
public List<object> data { get; set; }
}
public class Datum2
{
public string username { get; set; }
public string profile_picture { get; set; }
public string id { get; set; }
public string full_name { get; set; }
}
public class Likes
{
public int count { get; set; }
public List<Datum2> data { get; set; }
}
public class LowResolution
{
public string url { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class Thumbnail
{
public string url { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class StandardResolution
{
public string url { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class Images
{
public LowResolution low_resolution { get; set; }
public Thumbnail thumbnail { get; set; }
public StandardResolution standard_resolution { get; set; }
}
public class From
{
public string username { get; set; }
public string profile_picture { get; set; }
public string id { get; set; }
public string full_name { get; set; }
}
public class Caption
{
public string created_time { get; set; }
public string text { get; set; }
public From from { get; set; }
public string id { get; set; }
}
public class User
{
public string username { get; set; }
public string website { get; set; }
public string profile_picture { get; set; }
public string full_name { get; set; }
public string bio { get; set; }
public string id { get; set; }
public override string ToString ()
{
return string.Format ("[User: username={0}, website={1}, profile_picture={2}, full_name={3}, bio={4}, id={5}]", username, website, profile_picture, full_name, bio, id);
}
}
public class LowResolution2
{
public string url { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class StandardResolution2
{
public string url { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class Videos
{
public LowResolution2 low_resolution { get; set; }
public StandardResolution2 standard_resolution { get; set; }
}
public class Datum
{
public object attribution { get; set; }
public List<object> tags { get; set; }
public string type { get; set; }
public Location location { get; set; }
public Comments comments { get; set; }
public string filter { get; set; }
public string created_time { get; set; }
public string link { get; set; }
public Likes likes { get; set; }
public Images images { get; set; }
public List<object> users_in_photo { get; set; }
public Caption caption { get; set; }
public bool user_has_liked { get; set; }
public string id { get; set; }
public User user { get; set; }
public Videos videos { get; set; }
}
public class RootObject
{
public Pagination pagination { get; set; }
public Meta meta { get; set; }
public List<Datum> data { get; set; }
}
Here is the getCell method under the TableSource class:
public class TableSource<RootObject> : UITableViewSource
{
public List<RootObject> Data { get; set; }
protected string cellIdentifier = "TableCell";
public TableSource ()
{
Data = new List<RootObject> ();
}
public TableSource(List<RootObject> data)
{
Data = data;
}
public override int RowsInSection (UITableView tableview, int section)
{
if (Data == null) {
return 0;
} else {
return Data.Count;
}
}
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
// request a recycled cell to save memory
UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
// if there are no cells to reuse, create a new one
if (cell == null)
cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
cell.TextLabel.Text = Data[indexPath.Row].data[0].user.username;
return cell;
}
}
You are passing a List<RootObject> to your TableSource. Each RootObject has a data property that is a List<Datum>; Each Datum has a User property, and each User has a username property.
So you want to do something like this (I'm arbitrarily grabbing the first Datum, this may not be correct)
cell.TextLabel.Text = Data[indexPath.Row].data[0].user.username;
Your least amount effort to display meaningful text would be to override the ToString() method on the DTO's you wish to display. You have already done this to User so if you want to show the user info just overrider the Datum class, something like this:
public class Datum
{
...
override ToString()
{
return this.User.ToString();
}
}

Categories

Resources