Forum Xamarin Xamarin.Forms

[Shell] Pass parameter to local shellcontent's constructor from XAML

z07713z07713 USMember ✭✭

Is this even possible?

Thank you!

Best Answer

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    You could use x:Arguments to inject parameters in the constructor:

    <local:Gallery>
        <x:Arguments>
            <x:String>Name</x:String>
        </x:Arguments>
    </local:Gallery>
    

    I also recommend you to use BindableProperty to achieve that. Create a bindable property in the Gallery:

    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Gallery : ContentPage
    {
        public Gallery ()
        {
            InitializeComponent ();
        }
    
        protected override void OnAppearing()
        {
            base.OnAppearing();
    
            // Get the value here
        }
    
        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);
    
    }
    

    Then you could set the value in Xaml:

    <local:Gallery Arg="Name">

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    You could use x:Arguments to inject parameters in the constructor:

    <local:Gallery>
        <x:Arguments>
            <x:String>Name</x:String>
        </x:Arguments>
    </local:Gallery>
    

    I also recommend you to use BindableProperty to achieve that. Create a bindable property in the Gallery:

    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Gallery : ContentPage
    {
        public Gallery ()
        {
            InitializeComponent ();
        }
    
        protected override void OnAppearing()
        {
            base.OnAppearing();
    
            // Get the value here
        }
    
        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);
    
    }
    

    Then you could set the value in Xaml:

    <local:Gallery Arg="Name">

  • z07713z07713 USMember ✭✭

    @LandLu said:
    You could use x:Arguments to inject parameters in the constructor:

    <local:Gallery>
        <x:Arguments>
            <x:String>Name</x:String>
        </x:Arguments>
    </local:Gallery>
    

    this is just awesome, thank you!

  • Nick_ehsanjahangardNick_ehsanjahangard Member ✭✭
    edited April 11

    How to Create ShellContent in CodeBehind and set ContentPage ?

  • Roberto.6713Roberto.6713 USUniversity ✭✭

    @Nick_ehsanjahangard said:
    How to Create ShellContent in CodeBehind and set ContentPage ?

    did you resolve it?

Sign In or Register to comment.