Created
September 10, 2015 08:15
-
-
Save MilenPavlov/3849eff76a2d0dcba22f to your computer and use it in GitHub Desktop.
mvvm command usage complete example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//the command: | |
private RelayCommand logInCommand; | |
public RelayCommand LogInCommand | |
{ | |
get | |
{ | |
return logInCommand ?? | |
(logInCommand = new RelayCommand( | |
async () => | |
{ | |
IsBusy = true; | |
ToggleKeypadCommand.Execute(null); | |
using (var httpRequest = new HttpRequestService()) | |
{ | |
LoggedInUser = await httpRequest.GetUserAsync(Username); | |
if (LoggedInUser != null) | |
{ | |
IsBusy = false; | |
Username = string.Empty; | |
_navigationService.NavigateTo("MainMenu", new UserViewModel { User = LoggedInUser }); | |
} | |
} | |
}, | |
LogInCommandCanExecute)); | |
} | |
} | |
//the client (droid) | |
_logInButton.SetCommand("Click", ViewModel.LogInCommand); | |
//the http request service | |
public async Task<User> GetUserAsync(string userName) | |
{ | |
using (var helper = new HttpClientFactory()) | |
{ | |
var result = await helper.GetAsync($"/accounts/user/{userName}"); | |
//todo add validation service | |
if (!string.IsNullOrEmpty(result)) | |
{ | |
var user = JsonConvert.DeserializeObject<User>(result); | |
return user; | |
} | |
return null; | |
} | |
} | |
//http client factory | |
public async Task<string> GetAsync(string relativeUrl) | |
{ | |
var url = GetValidUrl(Constants.BaseUriDeployed, relativeUrl); | |
using (var client = new HttpClient(new NativeMessageHandler())) | |
{ | |
var response = await client.GetAsync(url); | |
if (response.IsSuccessStatusCode) | |
{ | |
var result = await response.Content.ReadAsStringAsync(); | |
return result; | |
} | |
return string.Empty; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment