Home > other >  How to get dynamic value from Rg.Plugins.Popup with Event Handler fr [No MessagingCenter]
How to get dynamic value from Rg.Plugins.Popup with Event Handler fr [No MessagingCenter]

Time:01-12

I am learning Xamarin Forms.

I would like to dynamically get the selected value in my Popup Listview when my Popup is closed.

I am using Event Handler. MessagingCenter is not working well as my code need to use the selected item in the listview right after the popup is closed.

Using this link I have manage to code this :

**My Popup Xalm code looks like this **

<Grid RowDefinitions="*,50" >


<CollectionView Grid.Row="0" x:Name="QuestionLangListView" SelectionMode="Single" SelectionChanged="OnselectLanguageList" >

<CollectionView.ItemTemplate>
<DataTemplate>
<Label Grid.Column="0" TextColor="{StaticResource MySecondColor }"   HorizontalTextAlignment="Start" Text="{Binding LanguageKey2}" Style="{StaticResource NormalSizeLabel }"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
       

<StackLayout Grid.Row="1" >
 <!--Button to close the Popup once it is opened-->
<StackLayout.GestureRecognizers>
    <TapGestureRecognizer  Command="{Binding PrimaryCommand , Source={Reference ThisPopUpPage}}"/>
</StackLayout.GestureRecognizers>


</StackLayout>



</Grid>

In My PopUp C# code :

When user click on my list of string item I update the global variable ListLangFilter that would like to send when the PopUp closed like this :

string ListLangFilter;
void OnselectLanguageList(System.Object sender, Xamarin.Forms.SelectionChangedEventArgs e)
{
var word = (string)e.CurrentSelection.FirstOrDefault();

/*
get value of item selected
*/

// I am updating the global variable here
ListLangFilter = word; // value of an item in list
}

Here is how I use Event Handler in my PopUp to get the global :

public event EventHandler<object> ClickEvent;
public void OnStartGame()
{
     // in this function I am getting the value of the selected item thanks to global variable*emphasized text*
     ClickEvent?.Invoke(this, ListLangFilter);

}

In a random classe : Here is how I use my PopUp

   await PopupNavigation.Instance.PushAsync(new Mypopup.MyGamePopup(LangQuestionList)
{
PrimaryCommand = new Command(async () =>
{


// List lang close and message center is sent
await PopupNavigation.Instance.PopAsync(true);

Mypopup.MyGamePopup gamepopop = new Mypopup.MyGamePopup(LangQuestionList);


// this is not working well because I cannot get the selected item in the Popup as  gamepopop is not the opened Popup
//await PopupNavigation.Instance.PushAsync(new Mypopup.MyGamePopup(LangQuestionList) is the opened Popup I would like to get the Event handler from this Popup so that I can get the Item selected value 
gamepopop.ClickEvent  = (s,args) =>
{
  
  Console.WriteLine("your arg value is  "  args);
};

gamepopop.OnStartGame();


})

}); ;

The Problem :

 Mypopup.MyGamePopup gamepopop = new Mypopup.MyGamePopup(LangQuestionList);

This way I cannot get the updated value on my global variable ListLangFilter

I do not know how I can get the selected Item in my Popup ListView

Thanks

CodePudding user response:

You are very close

First, create the popup

Mypopup.MyGamePopup gamepopop = new Mypopup.MyGamePopup(LangQuestionList);

Then assign the event handler

gamepopop.ClickEvent  = (s,args) =>
{
  Console.WriteLine("your arg value is  "  args);
};

Then navigate to the instance of the popup that has the handler already assigned

await PopupNavigation.Instance.PushAsync(gamepopup)
  • Related