从AppShell.xaml页面Xamarin向页面传递命令

从AppShell.xaml页面Xamarin向页面传递命令,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我在AppShell.xaml中使用了带有以下代码的选项卡式xamarin表单: <TabBar Route="tabbar"> <ShellContent Title="Top Rated" Icon="icon_top.png" Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}"

我在AppShell.xaml中使用了带有以下代码的选项卡式xamarin表单:

 <TabBar Route="tabbar">
        <ShellContent Title="Top Rated" Icon="icon_top.png" Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />
        <ShellContent Title="Recent" Icon="icon_recent.png" ContentTemplate="{DataTemplate local:RecentPage}" />
        <ShellContent Title="Oldest" Icon="icon_old.png" ContentTemplate="{DataTemplate local:OldestPage}" />
</TabBar>
这将基于该参数更改视图。有没有办法为tabbar做这件事

所以理想情况下我想要这样的东西:

await Shell.Current.GoToAsync($"{nameof(RecentPage)}?{nameof(RecentPage.Type)}={1}")
 <TabBar Route="tabbar">
        <ShellContent Title="Top Rated" Icon="icon_top.png" Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />
        <ShellContent Title="Recent" Icon="icon_recent.png" ContentTemplate="{DataTemplate local:PostsPage>Recent}" />
        <ShellContent Title="Oldest" Icon="icon_old.png" ContentTemplate="{DataTemplate local:PostsPage>Oldest}" />
</TabBar>

这将基于该参数更改视图。有没有办法为tabbar做这件事

我猜您希望通过xaml将参数传递给本地shellcontent的构造函数

我建议你可以用它来做。在ContentPage中创建可绑定属性

 public partial class Gallery : ContentPage
{
    public string Arg
    {
        set { SetValue(ArgProperty, value); }
        get { return (string)GetValue(ArgProperty); }
    }
    public static readonly BindableProperty ArgProperty = BindableProperty.Create(nameof(Arg), typeof(string), typeof(Gallery), string.Empty,defaultBindingMode: BindingMode.OneWay,
                                                     propertyChanged: ArgPropertyChanged);

    private static void ArgPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = (Gallery)bindable;
        control.label1.Text = newValue.ToString();
    }
    public Gallery()
    {
        InitializeComponent();                 
    }
}
我使用标签来获取参数

<ContentPage.Content>
    <StackLayout>
        <Label
            x:Name="label1"
            HorizontalOptions="CenterAndExpand"
            VerticalOptions="CenterAndExpand" />
    </StackLayout>
</ContentPage.Content>

  <ShellContent
        Title="About"
        ContentTemplate="{DataTemplate local:AboutPage}"
        Icon="tab_about.png"
        Route="AboutPage" />
    <ShellContent
        Title="Browse"
        ContentTemplate="{DataTemplate local:ItemsPage}"
        Icon="tab_feed.png" />
    <ShellContent Title="Browse" Icon="tab_feed.png">
        <local:Gallery Arg="name" />
    </ShellContent>

更新:

我建议您可以使用将数据传递给viewmodel

<ContentPage.Content>
    <StackLayout>
        <Label
            x:Name="label1"
            HorizontalOptions="CenterAndExpand"
            VerticalOptions="CenterAndExpand" />

        <Label Text="{Binding str}" />
    </StackLayout>
</ContentPage.Content>

 public partial class Gallery : ContentPage
{
    public string str;
    public string Arg
    {
        set { SetValue(ArgProperty, value); }
        get { return (string)GetValue(ArgProperty); }
    }
    public static readonly BindableProperty ArgProperty = BindableProperty.Create(nameof(Arg), typeof(string), typeof(Gallery), string.Empty,defaultBindingMode: BindingMode.OneWay,
                                                     propertyChanged: ArgPropertyChanged);

    private static void ArgPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = (Gallery)bindable;
        control.label1.Text = newValue.ToString();
        MessagingCenter.Send<string, string>("test", "Hi", newValue.ToString());

    }

  
    public Gallery()
    {
        InitializeComponent();
        this.BindingContext = new viewmodel();         
    }
}
public class viewmodel: INotifyPropertyChanged
{
    private string _str;
    public string str
    {
        get { return _str; }
        set
        {
            _str = value;
            RaisePropertyChanged("str");
        }
    }
    public viewmodel()
    {
        MessagingCenter.Subscribe<string, string>("test", "Hi", (sender, arg) =>
        {
            str = arg;
        });
    }
   
    public event PropertyChangedEventHandler PropertyChanged;       
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

公共部分类库:ContentPage
{
公共字符串str;
公共字符串Arg
{
set{SetValue(ArgProperty,value);}
获取{return(string)GetValue(ArgProperty);}
}
public static readonly BindableProperty ArgProperty=BindableProperty.Create(nameof(Arg)、typeof(string)、typeof(Gallery)、string.Empty、defaultBindingMode:BindingMode.OneWay、,
propertyChanged:ArgPropertyChanged);
私有静态void ArgPropertyChanged(BindableObject bindable、object oldValue、object newValue)
{
变量控制=(图库)可绑定;
control.label1.Text=newValue.ToString();
Send(“test”,“Hi”,newValue.ToString());
}
公众席()
{
初始化组件();
this.BindingContext=新的viewmodel();
}
}
公共类视图模型:INotifyPropertyChanged
{
私有字符串_str;
公共字符串str
{
获取{return\u str;}
设置
{
_str=值;
RaisePropertyChanged(“str”);
}
}
公共视图模型()
{
MessagingCenter.Subscribe(“测试”,“嗨”,“发送方,参数)=>
{
str=arg;
});
}
公共事件属性更改事件处理程序属性更改;
public void RaisePropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}

“现在我想使用相同的视图”您提供给ShellContent.ContentTemplate的是一个页面而不是一个视图,您的意思是相同的页面吗?嘿,这看起来像是我需要的,我的问题是所有内容都在一个ViewModel上,所以我的页面在InitializeComponent代码所在的地方,我有以下内容:BindingContext=_ViewModel=new PostsViewModel();如何将值传递给ViewModel?@LearningEveryDay2.0请查看我的更新。