导航到下一页:if 语句不适用于 Xamarin 中的响应消息

问题描述 投票:0回答:1

我在 Xamarin 表单应用程序中有登录页面,该页面将请求发送到 PHP 文件并检查用户名和密码,然后响应消息。

C#代码:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using static JiyanUQuran.Models.appuser;

namespace JiyanUQuran.views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LoginPage : ContentPage
    {
        public LoginPage()
        {
            InitializeComponent();
        }
        private void Signin_Clicked(object sender, EventArgs e)
        {
            string user = username.Text;
            string pass = password.Text;

            Navigation.PushModalAsync(new MonthPage());

            string response = SendRequest("xxx/api/xxx.php?username=" + user + "&password=" + pass);
            
            if (response == "Welcome")
            {
                Navigation.PushModalAsync(new MonthPage());

            }
            else
            {
                message.Text = response;
            }
           

        }
        private string SendRequest(string url)
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    return client.DownloadString(new Uri(url));
                }
            }
            catch (WebException)
            {
                return null;
            }
        }

    }
}

PHP页面是这样的:

<?php
$message = "Empty Field not Allowed";
include_once 'DbConnect.php';
$username = $_GET['username'];
$password = md5($_GET['password']);

    
    $testuser = "SELECT * FROM users Where username = '$username'";
    $testresult=mysqli_query($connection,$testuser);
    $counttest = mysqli_num_rows($testresult);

    if ($counttest == 0){
        $register=mysqli_query($connection,"INSERT INTO users Values ('','$username','$password')");
    }
    else {
        $user = "SELECT * FROM users Where username = '$username' and password = '$password'";
        $result=mysqli_query($connection,$user);
        $count = mysqli_num_rows($result);

        if ($count == 0){
            $message= "username or password is wrong";
        }
        else{
            $message ="Welcome";
        }
    }


echo $message;
?>

当用户名或密码错误时,我正确收到消息,但我不想导航到其他页面,但在所有响应中它都导航到下一页,我该如何解决这个问题?

php xamarin request
1个回答
1
投票

你的

Navigation.PushModalAsync
函数中有两个
Signin_Clicked
方法,第一个将被直接调用,第二个将在
response
等于
"Welcome"
时被调用。

所以你的问题是由第一个

Navigation.PushModalAsync
引起的,删除它并选择是否通过响应导航到MonthPage。

private void Signin_Clicked(object sender, EventArgs e)
{
    string user = username.Text;
    string pass = password.Text;
    
    //remove this line
    //Navigation.PushModalAsync(new MonthPage());

    string response = SendRequest("xxx/api/xxx.php?username=" + user + "&password=" + pass);
    
    if (response == "Welcome")
    {
        Navigation.PushModalAsync(new MonthPage());

    }
    else
    {
        message.Text = response;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.