Hello Community
I am doing an app, that connects to a server, to register users, and login users, wen the user is logged in, I transition into a master page, where I have three button and my hamburger Icons (those work fine) except the logout, I sometimes get delay.
I am Using the MVVM pattern all the time and x:bindings
Here are my view Models.
LoginVM
private string result;
public ICommand LoginCommand { get; set; }
public ICommand GoToRegisterCommand { get; set; }
public HttpResponseMessage Msg { get; set; }
public JObject Obj { get; set; }
public LoginViewModel() {
IsVisible = true;
IsBusy = false;
IsEnable = false;
LoginCommand = new Command(async () => {
IsVisible = false;
IsBusy = true;
await Task.Run(async () => {
Obj = new JObject {
{ "username", Username.ToLower() },
{ "password", Password.ToLower() }
};
Msg = await NetworkHelpper.SendData(Constants.AUTH, Obj);
if (Msg == null)
return;
MainThread.BeginInvokeOnMainThread(async () => {
if (Msg.IsSuccessStatusCode) {
result = Msg.Content.ReadAsStringAsync().Result;
var details = JObject.Parse(result);
string token = details["token"].ToString();
await SecureStorage.SetAsync(Constants.TOKEN_KEY, token);
IsBusy = false;
await Application.Current.MainPage.Navigation.PushAsync(new HomeMaster());
} else {
IsVisible = true;
IsBusy = false;
result = Msg.Content.ReadAsStringAsync().Result;
await Application.Current.MainPage.DisplayAlert("Error", result, "ok");
}
});
});
});
GoToRegisterCommand = new Command(async () => {
await Application.Current.MainPage.Navigation.PushAsync(new RegisterScreen());
});
}
RegisterVM
public ICommand RegisterCommand { get; set; }
public ICommand FacebookCommand { get; set; }
public ICommand GmailCommand { get; set; }
public JObject Object { get; set; }
public HttpResponseMessage Msg { get; set; }
public RegisterViewModel() {
IsVisible = true;
IsEnable = false;
IsBusy = false;
FacebookCommand = new Command(async () => {
await Application.Current.MainPage.DisplayAlert("", "Calling facebook", "OK");
});
GmailCommand = new Command(async () => {
await Application.Current.MainPage.DisplayAlert("", "Calling Gmail", "OK");
});
RegisterCommand = new Command(async () => {
IsBusy = true;
IsVisible = false;
await Task.Run(async () => {
Object = new JObject {
{ "username", Username.ToLower() },
{ "password", Password.ToLower() },
{ "email", Email.ToLower()}
};
Msg = await NetworkHelpper.SendData(Constants.REGISTER, Object);
MainThread.BeginInvokeOnMainThread(async () => {
if (Msg == null)
return;
if (Msg.IsSuccessStatusCode) {
IsBusy = false;
IsVisible = true;
await Application.Current.MainPage.Navigation.PushAsync(new LoginScreen());
} else {
IsBusy = false;
IsVisible = true;
result = Msg.Content.ReadAsStringAsync().Result;
await Application.Current.MainPage.DisplayAlert("Error", result, "ok");
}
});
});
});
}
HomeVM
class HomeViewModel : BaseViewModel {
public ICommand GoToProfile { get; set; }
public HomeViewModel() {
GoToProfile = new Command(async () => {
Page app = Application.Current.MainPage;
await app.Navigation.PushAsync(new ProfileScreen(), true);
});
}
This is my view for the home
<NavigationPage.TitleView>
<StackLayout Spacing="30"
Orientation="Horizontal"
VerticalOptions="Center"
HorizontalOptions="Center">
<Image Source="LogoTexto.png"
WidthRequest="200" />
<Image Source="CampanaNotificaciones.png" />
</StackLayout>
</NavigationPage.TitleView>
<ContentPage.Content>
<ScrollView>
<StackLayout Padding="20"
HorizontalOptions="Center">
<Frame CornerRadius="100"
WidthRequest="58"
HeightRequest="58"
VerticalOptions="Center"
HorizontalOptions="Center">
<Image Source="mm.png"
VerticalOptions="Center"
HorizontalOptions="Center" />
</Frame>
<Label Text="Megaman" />
<StackLayout Orientation="Vertical"
Spacing="5">
<StackLayout Orientation="Horizontal"
HorizontalOptions="Center"
Margin="0,0,40,0">
<Button Text="Mis mascotas"
ContentLayout="Left,20"
Image="Mis_mascotas.png"
HorizontalOptions="Start"
BackgroundColor="Transparent" />
</StackLayout>
<StackLayout Orientation="Horizontal"
HorizontalOptions="Center">
<Button Text="Agenda de control"
ContentLayout="Left,20"
Image="Agenda_control.png"
HorizontalOptions="Start"
BackgroundColor="Transparent" />
</StackLayout>
<StackLayout Orientation="Horizontal"
HorizontalOptions="Center">
<Button Text="Pareja encontrada"
ContentLayout="Left,20"
Image="Pareja_encontrada.png"
HorizontalOptions="Start"
BackgroundColor="Transparent" />
</StackLayout>
<StackLayout Orientation="Horizontal"
HorizontalOptions="Center"
Margin="0,0,30,0">
<Button Text="Editar mi perfil"
Command="{x:Binding GoToProfile}"
ContentLayout="Left,20"
Image="Editar.png"
HorizontalOptions="Start"
BackgroundColor="Transparent" />
</StackLayout>
</StackLayout>
<StackLayout Margin="0,40,0,0"
Spacing="20">
<Image Source="BotonCompra.png" />
<Button BackgroundColor="{x:StaticResource ThirdColor}"
Text="¿Quieres poner a tu mascota en adopción?"
CornerRadius="20"
TextColor="{x:StaticResource BtonsColor}" />
<Button BackgroundColor="{x:StaticResource ThirdColor}"
Text="¿Quieres poner a tu mascota disponible en busqueda de parejas?"
CornerRadius="20"
TextColor="{x:StaticResource BtonsColor}" />
</StackLayout>
</StackLayout>
</ScrollView>
</ContentPage.Content>
The master for the home
<MasterDetailPage.Master>
<menu:HamburgerMenu />
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage>
<x:Arguments>
<View:HomeScreen />
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
The botton that is on the home, that is bound to my VM transition perfectly, but with a delay of 2 seconds, and also when I comeback and use the hamburger menu, when I come back it will flick white like for 1 millisecond (is anoyin lol)
When I see my console output, I can see the message "This app may be doing to much work on in its main thread"