你有一个看似相当简单的登录表单,我已经绑定了一个viewmodel。
除活动指标外,一切都在游泳。我已经尝试过各种方式让它出现。
据我了解,当设置为true时,IsVisible和IsRunning属性应该使指示符出现。我将这些绑定到bool属性,该属性在整个登录命令中设置为true / false。
要让它显示,我需要做什么?
Login.xaml
ns2
Login.xaml.cs
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TechsportiseApp.Views.Login">
<ContentPage.ToolbarItems>
<ToolbarItem Name="Register" Order="Primary" Icon="addperson.png" Text="Register" Priority="0" Command="{Binding RegisterCommand}" />
<ToolbarItem Name="Help" Order="Primary" Icon="help.png" Text="Help" Priority="1" Command="{Binding HelpCommand}" />
</ContentPage.ToolbarItems>
<ContentPage.Content>
<RelativeLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<StackLayout HorizontalOptions="FillAndExpand">
<StackLayout HorizontalOptions="FillAndExpand" BackgroundColor="Red" Padding="2" IsVisible="{Binding IsOffline}">
<Label Text="OFFLINE" BackgroundColor="Red" TextColor="White" FontAttributes="Bold" FontSize="Small" HorizontalOptions="FillAndExpand" HorizontalTextAlignment="Center" />
</StackLayout>
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Orientation="Vertical">
<ScrollView Orientation="Vertical" VerticalOptions="StartAndExpand">
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Orientation="Vertical" Margin="10">
<Image Source="splash.png" HorizontalOptions="Center" />
<Label Text="Race Director" FontAttributes="Bold" FontSize="Large" HorizontalTextAlignment="Center" HorizontalOptions="CenterAndExpand" />
<Label Text="by Techsportise" HorizontalTextAlignment="Center" HorizontalOptions="CenterAndExpand" />
<BoxView HeightRequest="20" HorizontalOptions="FillAndExpand" />
<Entry x:Name="email" Text="{Binding Email}" Placeholder="Email address" />
<Entry x:Name="password" Text="{Binding Password}" IsPassword="true" Placeholder="Password" />
<StackLayout Padding="3" Orientation="Horizontal">
<Label Style="{StaticResource EntryFormLabels}" Text="REMEMBER ME" FontSize="Small" HorizontalOptions="StartAndExpand" VerticalTextAlignment="Center" />
<Switch IsToggled="{Binding RememberMe}" HorizontalOptions="End" />
</StackLayout>
<Button x:Name="loginButton" Text="Login" Command="{Binding LoginCommand}" Style="{StaticResource Buttons}" />
<ActivityIndicator IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}" Color="#80000000" />
<Label Text="{Binding ValidationError}" HorizontalTextAlignment="Center" HorizontalOptions="CenterAndExpand" TextColor="Red" FontSize="Small" IsVisible="{Binding Invalid}" />
</StackLayout>
</ScrollView>
</StackLayout>
</StackLayout>
<ActivityIndicator IsVisible="{Binding IsBusy}"
IsRunning="{Binding IsBusy}"
Color="Black"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Width,
Factor=1}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Height,
Factor=1}" />
</RelativeLayout>
</ContentPage.Content>
</ContentPage>
Loginviewmodel
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechsportiseApp.Views;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using TechsportiseApp.API;
using TechsportiseApp.ViewModels;
using TechsportiseApp.Models;
using Newtonsoft.Json;
namespace TechsportiseApp.Views
{
public partial class Login : ContentPage
{
public Login ()
{
InitializeComponent ();
var viewModel = new LoginViewModel();
BindingContext = viewModel;
}
public Login(string email)
{
InitializeComponent();
var viewModel = new LoginViewModel(email);
BindingContext = viewModel;
}
}
}
答案 0 :(得分:2)
尝试将ViewModel方法Login
更改为:
private void Login()
{
Task.Run(() =>
{
try
{
Device.BeginInvokeOnMainThread(() => IsBusy = true);
var isvalidemail = GlobalFunctions.IsValidEmail(Email);
var IsThereConnection = GlobalFunctions.CheckForInternetConnection();
if (IsThereConnection == false)
throw new Exception("You cannot login whilst you are offline");
else if (Email == "")
throw new Exception("You must enter an email address");
else if (Password == "")
throw new Exception("You must enter a password");
else if (isvalidemail == false)
throw new Exception("You must enter a valid email address");
//We are good to go
else
{
var LoginStatus = AccountsAPI.Login(Email, Password);
if (LoginStatus.Code == "OK")
{
var tokenobject = JsonConvert.DeserializeObject<TokenModel>(LoginStatus.Content);
Application.Current.Properties["Token"] = tokenobject.Access_token;
Application.Current.Properties["IsRemembered"] = RememberMe;
string token = Application.Current.Properties["Token"].ToString();
if ((bool)Application.Current.Properties["ShowHelpOnStartup"] == true)
Device.BeginInvokeOnMainThread(() => App.Current.MainPage = new StartupHelp(true));
else
Device.BeginInvokeOnMainThread(() => App.Current.MainPage = new MainMenuMasterDetail());
}
//Error response
else
throw new Exception("Your login has failed. Please check your details and try again.");
}
}
catch(Exception ex)
{
Device.BeginInvokeOnMainThread(() =>
{
ValidationError = ex.Message;
Invalid = true;
});
}
finally
{
Device.BeginInvokeOnMainThread(() => IsBusy = true);
}
});
}