I have an Icommand in my view model as shown below:
public ICommand SalaryCommand
{
get
{
return new Command(async () =>
{
await _apiServices.FetchSalary(this);
});
}
}
I have this command bind to a button, to search for the salary history of a certain user so that it'll output the information, which works perfectly.However, I also want to execute it on load with the most recent salary information (current month). is it possible with the Icommand? if not, what other ways can I implement this function on load. *note: the variables are all existent in the same view model as the SalaryCommand and not in the class of FetchSalary();
I tried doing this in the main salary page but it didn't work:
public SalaryPage()
{
InitializeComponent();
Fetching();
}
private async void Fetching()
{
ApiServices _apiServices = new ApiServices();
SalaryViewModel svm = new SalaryViewModel();
string year = DateTime.Now.Year.ToString();
string month = DateTime.Now.Month.ToString();
await _apiServices.FetchSalary(svm);
}
and here is the FetchSalary() function which worked for the button
public async Task FetchSalary(SalaryViewModel svm)
{
Debug.WriteLine("Welcome to the fetch salary method");
var keyValues = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("UserID",App._username),
new KeyValuePair<string, string>("Year",svm.dSYear),
new KeyValuePair<string, string>("Month",svm.dSMonth)
};
var request = new HttpRequestMessage(HttpMethod.Post, "somelink");
request.Content = new FormUrlEncodedContent(keyValues);
var client = new HttpClient();
var response = await client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
Debug.WriteLine(content);
start = content.IndexOf("Sal_Trsp") + "Sal_Trsp".Length;
end = content.IndexOf("Sal_NssfMedPay") - start;
string transportation = content.Substring(start, end);
svm.sTransportation = "Transportation: " + simplify(transportation);
start = content.IndexOf("Sal_Date_Year") + "Sal_Date_Year".Length;
end = content.IndexOf("Sal_Date_Month") - start;
string yyear = content.Substring(start, end);
svm.sYear = "Year: " + simplify(yyear);
start = content.IndexOf("Sal_Date_Month") + "Sal_Date_Month".Length;
end = content.IndexOf("}") - start;
string mmonth = content.Substring(start, end);
svm.sMonth = "Month: " + simplify(mmonth);
start = content.IndexOf("Sal_NetPaid") + "Sal_NetPaid".Length;
end = content.IndexOf("Sal_users_FK") - start;
string retString = content.Substring(start, end);
svm.sNetPay = "Net Paid: " + simplify(retString);
Debug.WriteLine(svm.sNetPay);
}