Xamarin - 从按钮更改选项卡页面

时间:2015-09-05 06:40:37

标签: c# xaml xamarin

我目前正在使用Tabbed Page:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:me="clr-namespace:Let_s_go_out.Views.Pages;assembly=Let_s_go_out"
             x:Class="Let_s_go_out.Views.MainPage"
             >
  <TabbedPage.Children >
    <me:PlacesList />

    <me:PlaceSearch />


  </TabbedPage.Children>
</TabbedPage>

在PlaceSearch内容中,我有一个按钮:

<StackLayout Grid.Row="9" Orientation="Horizontal" >
          <Button Text="Rechercher" HorizontalOptions="Center" VerticalOptions="Center" Command="{Binding Search}"></Button>         
        </StackLayout>

所以我的问题是:我怎样才能进入&#34; PlaceList&#34;单击按钮时的标签页?

谢谢:)

3 个答案:

答案 0 :(得分:0)

答案是:

https://forums.xamarin.com/discussion/comment/150973#Comment_150973

这可以帮助其他人:)

答案 1 :(得分:0)

您也可以使用MVVM和MessagingCenter。

查看:

public partial class AwesomeView : TabbedPage
{
    public AwesomeView()
    {
        InitializeComponent();
        BindingContext = new AwesomeViewModel();
    }

    protected override void OnAppearing()
    {
        MessagingCenter.Subscribe<AwesomeViewModel, int>(this, "SetActiveTab", (sender, index) => {
            CurrentPage = Children[index];
        });
    }

    protected override void OnDisappearing()
    {
        MessagingCenter.Unsubscribe<AwesomeViewModel>(this, "SetActiveTab");
    }
}

ViewModel:

public class AwesomeViewModel
{
    public ICommand GoToTabCommand { get; set; }

    private AwesomeViewModel()
    {
        InitCommands();
    }

    private void InitCommands()
    {
        GoToTabCommand = new Command(GoToTab);
    }

    private void GoToTab()
    {
       MessagingCenter.Send<AwesomeViewModel, int>(this, "SetActiveTab", 1); //REPLACE 1 with the index of your tab
    }
}

您只需要将GoToTabCommand命令绑定到按钮或所需的任何控件。

我建议使用常量而不是“ SetActiveTab”。

答案 2 :(得分:0)

根据需要更改索引

private void FAB_Clicked(object sender, EventArgs e)
{
    var tab = this.Parent.Parent as TabbedPage;
    tab.CurrentPage = tab.Children[2];            
}