简体   繁体   English

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

[英]Xamarin - Change tabbed page from button

I was currently working with Tabbed Page :我目前正在使用 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>

And in the PlaceSearch content I have a button :在 PlaceSearch 内容中,我有一个按钮:

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

So my question is : How I can go on the "PlaceList" Tabbed page when the Button is clicked ?所以我的问题是:当单击 Button 时,我如何进入“PlaceList”选项卡式页面?

Thanks :)谢谢 :)

You can use MVVM and MessagingCenter for this as well.您也可以为此使用 MVVM 和 MessagingCenter。

View:看法:

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
    }
}

You just have to bind GoToTabCommand command to a button or any control you want.您只需要将 GoToTabCommand 命令绑定到按钮或任何您想要的控件。

I recommend having a constant instead of "SetActiveTab".我建议使用常量而不是“SetActiveTab”。

Change the index according to your need根据需要更改索引

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

I was currently working with 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>

And in the PlaceSearch content I have a button :在PlaceSearch内容中,我有一个按钮:

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

So my question is : How I can go on the "PlaceList" Tabbed page when the Button is clicked ?所以我的问题是:单击按钮后如何进入“ PlaceList”选项卡式页面?

Thanks :)谢谢 :)

I don't know if it will help other people, but about the first answer, it worked for me after deleting one "Parent" when defining var tab, see below.我不知道它是否会帮助其他人,但关于第一个答案,它在定义 var 选项卡时删除一个“父级”后对我有用,见下文。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM