如何在Xamarin跨平台应用程序中导航?

时间:2017-08-13 14:58:04

标签: c# wcf xamarin.forms

我正在使用以下步骤构建xamarin跨平台便携式应用程序:

  1. 添加了服务参考。
  2. 点击按钮点击登录页面,我用来验证来自db的用户,如果成功,则转到下一页,否则显示消息。
  3. 这是代码。

    Login.xaml

    <?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="FypXFP.Login">
        <ContentPage.Content>
    
            <StackLayout>
                <Label Text="UserName" />
                <Entry x:Name="tbuserentry" Placeholder="UserName"></Entry>
                <Label Text="Password"></Label>
                <Entry x:Name="Pwd" IsPassword="True" Placeholder="Password"></Entry>
                <Button x:Name="Save" Clicked="Save_Clicked" Text="Login"></Button>
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
    

    Login.cs.xaml

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.ServiceModel;
    using System.Text;
    using System.Threading.Tasks;
    
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    
    namespace FypXFP
    {
        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class Login : ContentPage
        {
            private FypXFP.ServiceReference1.CMSServiceClient _client;
            public Login()
            {
                InitializeComponent();
            }
    
            private void Save_Clicked(object sender, EventArgs e)
            {
                var endpoint = new EndpointAddress("http://192.168.43.101/FYP_Admin/webservices/cmsservice.svc");
                var binding = new BasicHttpBinding
                {
                    Name = "basicHttpBinding",
                    MaxBufferSize = 2147483647,
                    MaxReceivedMessageSize = 2147483647
                };
    
                TimeSpan timeout = new TimeSpan(0, 0, 30);
                binding.SendTimeout = timeout;
                binding.OpenTimeout = timeout;
                binding.ReceiveTimeout = timeout;
    
                _client = new FypXFP.ServiceReference1.CMSServiceClient(binding, endpoint);
    
                string uname = tbuserentry.Text.Trim();
                string password = Pwd.Text.Trim();
                _client.ValidateUserAsync(uname, password);
                _client.ValidateUserCompleted += _client_ValidateUserCompleted;
    
            }
            public async void Redirect(Boolean Result)
            {
                if (Result == true)
                {
                    Navigation.InsertPageBefore(new TicketPage(), this);
                    await Navigation.PopAsync();
                }
                else
                {
                    await DisplayAlert("Alert", "Something wrong", "Cancel");
                }
            }
            private void _client_ValidateUserCompleted(object sender, ServiceReference1.ValidateUserCompletedEventArgs e)
            {
                if (e.Result == true)
                {
                    Device.BeginInvokeOnMainThread(() => {
                        Redirect(true);
                    });
    
    
                }
                else
                {
                    Device.BeginInvokeOnMainThread(() => {
                        Redirect(false);
                    });
                }
            }
        }
    }
    

    问题是如果我输入了错误的凭据,而不是警报显示,但另一方面,如果输入了正确的凭据,则显示

      

    取消处理例外

    我已尝试过所有可能的解决方案。

0 个答案:

没有答案