RM Sedrez
RM Sedrez

Reputation: 1

.net Maui DI Link label Text

I'm trying to pass a text obtained through binding from an existing base date to a new page to split items from another db3

firs file xaml

<Grid> 
    <Frame>                         
        <Frame.GestureRecognizers>  
                           
            <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type viewModels:ListViewModel}}, Path=TapCommand}" CommandParameter="{Binding .}" />

        </Fr<Label Text="{Binding ListName}" FontSize="Large" />                                               </Frame>                     
</Grid>  

the ViewModel

 [RelayCommand]
        public async Task Tap()
        {
            string texto = ?

                 
                 await Shell.Current.GoToAsync($"{nameof(Produtos)}?Text={texto}");
            
           
        }

the viewModel for the next page

[QueryProperty("Text", "Text")]
    public partial class ProductViewModel : ObservableObject
    {
        public ListViewModel _vm;
        public ProductViewModel(ListViewModel vm)
        {
            _vm = vm;
           
        }
    }

i`m try to paste this information for new page but nothing i do works.

Upvotes: 0

Views: 618

Answers (1)

Alexandar May - MSFT
Alexandar May - MSFT

Reputation: 10156

In .NET Multi-platform App UI (.NET MAUI) Shell, primitive data can be passed as string-based query parameters when performing URI-based programmatic navigation.

You can refer to the sample code below on how to transfer a string: Texto to the next page.

SAMPLE CODE:

MainPage.xaml

  <Button Text="Pass texto to next page" Command="{Binding TapCommand}"/>

MainPageViewModel.cs


    public partial class MainPageViewModel : ObservableObject
    {

        [ObservableProperty]
        string texto;

        [RelayCommand]
        public async Task Tap()
        {

            Texto = "This is a test";

            await Shell.Current.GoToAsync($"{nameof(ProductPage)}?Texto={Texto}");
        }
    }

ProductPage.xaml


   <VerticalStackLayout> 
        <Label 
            Text="{Binding Texto}"
            VerticalOptions="Center" 
            HorizontalOptions="Center" />
    </VerticalStackLayout>

Code-behind:


public partial class ProductPage : ContentPage 
{
      public ProductPage(ProductViewModel vm)
      {
            InitializeComponent();

            BindingContext= vm;
      }

    protected override void OnNavigatedTo(NavigatedToEventArgs args)
    {
        base.OnNavigatedTo(args);
    }
}

ProductViewModel.cs

namespace MauiAppDI.ViewModel 
{
    [QueryProperty(nameof(Texto), nameof(Texto))]
    public partial class ProductViewModel : ObservableObject
    {

        [ObservableProperty]
        string texto;
    }
}

Upvotes: 0

Related Questions