首页 > 解决方案 > 从登录页面导航时如何在设置页面中禁用或隐藏按钮

问题描述

我有两个按钮MainPage,一个是登录按钮,另一个是导航按钮。当用户点击导航按钮时,它应该显示主页、设置和其他选项卡,但想将设置页面中的按钮之一设置为隐藏或禁用?

我已经使用“MessagingCenter”尝试了以下代码并在“设置”中收到,但它不起作用,有人可以看看!

//Settings.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="soccerapp.Settings" BackgroundColor="#755f89" Title="Settings">
    <AbsoluteLayout>
        <!-- Normal Page Content -->
        <StackLayout AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
             AbsoluteLayout.LayoutFlags="All">
            <Label HeightRequest="50" FontSize="18" TextColor="White" HorizontalOptions="Center" Text="Please enter today's soccer status-" />

            <Label Text="{Binding SoccerStatus}"></Label>
            <Label Text="{Binding CurrentDate}"></Label>
            <Button x:Name="button1"
              Text="Click here !"
              FontSize="Large"
              VerticalOptions="CenterAndExpand"
              HorizontalOptions="Center"
              Clicked="OnButtonClicked" />
        </StackLayout>
    </AbsoluteLayout>
</ContentPage>

//MainPage.xaml.cs :

public async void NavigateButton_OnClicked(object sender, EventArgs e)
        {
            var tabbedPage = new TabbedPage();
            tabbedPage.Children.Add(new Home("Welcome"+' '+emailEntry.Text+' '+",have a nice day!"));
            tabbedPage.Children.Add(new Settings(soccerAvailability, emailEntry.Text));
            await Navigation.PushAsync(tabbedPage);
            MessagingCenter.Send<object>(this, "TurnOffButton");

        }

 public Settings(string emailText)
        {
            InitializeComponent();           
            emailTextVal = emailText;
            MessagingCenter.Subscribe<object>(this, "TurnOffButton", (sender) => 
            {
                button1.IsVisible = false;
            });

        }

// 登录按钮代码如下:

public async void Button_Clicked(object sender, System.EventArgs e)
    {
        this.IsBusy = true;
        await Task.Delay(TimeSpan.FromMilliseconds(1000));
        string emailText = emailEntry.Text;
        string passwordText= passwordEntry.Text; 
        if(!string.IsNullOrWhiteSpace(emailEntry.Text) && !string.IsNullOrWhiteSpace(passwordEntry.Text))
        {

            if(ValidateEmail(emailText) == true)
            {

               int count = (from x in conn.Table<PlayerDetails>().Where(x => x.Email == emailText) select x).Count();
                if (count!= 0)
                {
                    try
                    {
                       List<PlayerDetails> myList = (from x in conn.Table<PlayerDetails>().Where(x => x.Email == emailText && x.Password == passwordText) select x).ToList();
                        if(myList.Count() > 0)
                        {
                                var tabbedPage = new TabbedPage();
                                PlayerDetails playerDetails = new PlayerDetails();
                                SoccerAvailability soccerAvailability = new SoccerAvailability();
                                tabbedPage.Children.Add(new Home(emailEntry.Text));
                                tabbedPage.Children.Add(new Settings(soccerAvailability, emailEntry.Text));
                                await Navigation.PushAsync(tabbedPage);
                                var profilePicTollbarItem = new ToolbarItem()
                                {
                                    Icon = "LF.PNG"
                                };
                                profilePicTollbarItem.Clicked += OnProfilePicClicked;
                                tabbedPage.ToolbarItems.Add(profilePicTollbarItem);
                        }
                        else
                        {
                            this.IsBusy = false;
                            await DisplayAlert("Notification", "No such email or password", "Cancel");

                        }

                    }
                    catch (NullReferenceException ex)
                    {
                      if(ex!=null)
                        Debug.WriteLine("Something is thrown ! " + e.ToString());
                    }
                    finally
                    {
                        IsBusy = false;
                    }
                }
                else
                {
                     this.IsBusy = false;
                    await DisplayAlert("Notification", "Unable to find the user details", "Cancel");
                } 
            }
            else
            {
                this.IsBusy = false;
                await DisplayAlert("Notification", "Email is not a valid one", "Cancel");
            }   
        }
        else
        {
            this.IsBusy = false;
            await DisplayAlert("Notification","Input details cannot be blank", "Cancel");
        }
    }

标签: xamarinxamarin.forms

解决方案


bool在构造函数中传递 a 会简单得多Settings

public Settings(string emailText, bool ShowButton = false)
{
    ...

    button1.IsVisible = ShowButton;
}

然后当您Settings为两个不同的代码路径创建页面时,传入适当的值

// hide button - when called from Navigate
tabbedPage.Children.Add(new Settings(emailEntry.Text, false));

// show button - when called from Login
tabbedPage.Children.Add(new Settings(emailEntry.Text, true));

推荐阅读