passing values without heavy libraries?

JuggernOtt81 21 Reputation points
2021-07-28T16:54:59.883+00:00
public partial class ScannerModalPage : ContentPage
{
    public ScannerModalPage()
    {
        InitializeComponent();
    }
    public void ZXingScannerView_OnScanResult(ZXing.Result result)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            scanResultText.Text = result.Text;
        });
    }
    private void AcceptButton_Clicked(object sender, EventArgs e)
    {
        Navigation.PopModalAsync();
    }
}

without involving MVVM, is there a simple way (mainly because this use case only needs to implement it once) to pass a value BACK to the previous page?
for some reason, i imagined this would work, but alas, it does NOT.

Navigation.PopModalAsync(result.Text());
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,355 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,936 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 31,641 Reputation points Microsoft Vendor
    2021-07-29T05:57:25.417+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!
    You could use Action to pass value. There are two pages in my demo, when I click the button and pop the second page to the previous page(first page), I pass the label text of the second page. You can have a try with the following code:
    PageOne.xaml

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
                 x:Class="PassValueDemo.PageOne"  
                 Title="PageOne">  
        <ContentPage.Content>  
            <StackLayout>  
                <Label x:Name="showResultLabel" Text="none" FontSize="Large" BackgroundColor="Pink"></Label>  
                <Button Text="Push button"  
                    VerticalOptions="CenterAndExpand"   
                    HorizontalOptions="CenterAndExpand"  
                        Clicked="Button_Clicked"/>  
            </StackLayout>  
        </ContentPage.Content>  
    </ContentPage>  
    

    PageOne.xaml.cs

     private void Button_Clicked(object sender, EventArgs e)  
            {  
                PageTwo second = new PageTwo();  
                second.PassValueAction = (passvalue) => {  
                    Console.WriteLine("receive: {0}", passvalue);  
                    showResultLabel.Text = passvalue;  
                };  
                Navigation.PushAsync(second);  
            }  
    

    PageTwo.xaml

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
             x:Class="PassValueDemo.PageTwo"  
             Title="PageTwo">  
    <ContentPage.Content>  
        <StackLayout BackgroundColor="LightBlue">  
            <Label x:Name="resultLabel" Text="result"></Label>  
            <Button Text="Pop button"  
                VerticalOptions="CenterAndExpand"   
                HorizontalOptions="CenterAndExpand"  
                    Clicked="Button_Clicked"/>  
        </StackLayout>  
    </ContentPage.Content>  
    </ContentPage>  
    

    PageTwo.xaml.cs

     public partial class PageTwo : ContentPage  
    {  
       public Action<String> PassValueAction;//use action to pass value  
        static int tag;  
        public PageTwo()  
        {  
            InitializeComponent();  
        }  
    
        private void Button_Clicked(object sender, EventArgs e)  
        {  
            tag++;//test  
            PassValueAction(resultLabel.Text + tag.ToString());  
            Navigation.PopAsync();  
        }  
    }  
    

    Best Regards,
    Wenyan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.