微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决从 AppShell.xaml 页面向页面传递命令 Xamarin

我在 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>

现在我想对RecentPage 和OldestPage 使用相同的视图,但向视图传递一个参数以了解要显示内容,在代码隐藏中,您可以执行以下操作:

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>

解决方法

这将根据该参数更改视图。有没有办法为标签栏做到这一点?

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

我建议你可以使用 Bindable Properties 来做。在 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>

更新:

我建议您可以使用 Xamarin.Forms MessagingCenter 将数据传递给视图模型。

<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,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,(sender,arg) =>
        {
            str = arg;
        });
    }
   
    public event PropertyChangedEventHandler PropertyChanged;       
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this,new PropertyChangedEventArgs(propertyName));
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。