I'm still learning this C# MVVM WPF thing and I'm having some troubles in understanding some concepts I already tried some things similarly but sadly without results.
So I want to >>> if the Login was successful to open a new ViewModel passing one class (you can see in comment, the exact place) to the GeneralViewModel can someone help me here some code so that you can understand better.
Thanks in advance :)
LoginViewModel
WifiAP wa;
#region fields
private TokenRequest tk;
public DelegateCommand _loginCommand { get; set; }
public LoginViewModel()
{
wa = new WifiAP();
_loginCommand = new DelegateCommand(
(s) => { login(); }, //Execute
(s) => { return !string.IsNullOrEmpty(_email); } //CanExecute
);
}
public DelegateCommand LoginCommand
{
get { return _loginCommand; }
}
public async void login()
{
var sendRequest = new TokenRequest
{
email = Email,
mac = getMac(),
platform="windowsdesktop"
};
//Get Token
var response = await CommunicationWebServices.PostASM("self/token", "",sendRequest);
if(response.StatusCode == HttpStatusCode.OK)
{
string responseS = await response.Content.ReadAsStringAsync();
var aux = JsonConvert.DeserializeObject<TokenOK>(responseS); // I have the Token in the Aux
var passwordMD5 = hashingMD5(Password);
var stringConcat = aux.token + passwordMD5;
var finalMD5 = hashingMD5(stringConcat);
// Now you can login
var sendRequestLogin = new LoginRequest
{
email = Email,
tokenpassword = finalMD5,
mac = getMac(),
platform = "windowsdesktop"
};
var responseLogin = await CommunicationWebServices.PostASM("self/login", aux.token, sendRequestLogin);
if(responseLogin.StatusCode == HttpStatusCode.OK)
{
string responseL = await responseLogin.Content.ReadAsStringAsync();
var auth = JsonConvert.DeserializeObject<LoginAnswer>(responseL);
// Change to GeneralViewModel and send the auth variable
}else {//...}}
LoginView
<UserControl.DataContext>
<ViewModel:LoginViewModel/>
</UserControl.DataContext>
<Button x:Name="buttonLogin" Content="Login"
Command="{Binding LoginCommand}" HorizontalAlignment="Left" Margin="274,305,0,0" VerticalAlignment="Top" Width="248" Style="{DynamicResource FlatButtonStyle}" Height="30" />