How to add Popup or dialog like radio button group selection in xamarin forms. I am getting all in android and java.
On click is should be open and selection should be done.
Join Stack Overflow to learn, share knowledge, and build your career.
How to add Popup or dialog like radio button group selection in xamarin forms. I am getting all in android and java.
On click is should be open and selection should be done.
I wirte a demo about it. Using Rg.Plugins.Popup
and MessagingCenter
(Transfer data) to achieve it.
Here is running GIF.
If we want to use Rg.Plugins.Popup
and RadioButton
, we should add following code in OnCreate
method.
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Rg.Plugins.Popup.Popup.Init(this, savedInstanceState);
Forms.SetFlags("RadioButton_Experimental");
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
Here is MainPage.xaml
<StackLayout>
<!-- Place new controls here -->
<Label x:Name="myLabel"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
<Button Text="play" Clicked="Button_Clicked"></Button>
</StackLayout>
Here is MainPage.xaml.cs
, I used MessagingCenter.Subscribe
to receive to data from popup page.
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
MessagingCenter.Subscribe<App, string>(App.Current, "OpenPage", (snd, arg) =>
{
myLabel.Text = arg;
});
}
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PushPopupAsync(new Page1());
}
Here is popup page,Page1.xaml
<?xml version="1.0" encoding="utf-8" ?>
<pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
mc:Ignorable="d"
x:Class="MinitorMediaManager.Page1">
<pages:PopupPage.Animation>
<animations:ScaleAnimation
PositionIn="Center"
PositionOut="Center"
ScaleIn="1.2"
ScaleOut="0.8"
DurationIn="400"
DurationOut="300"
EasingIn="SinOut"
EasingOut="SinIn"
HasBackgroundAnimation="True"/>
</pages:PopupPage.Animation>
<!--You can use any elements here which are extended from Xamarin.Forms.View-->
<Frame VerticalOptions="Center"
HorizontalOptions="Center" >
<StackLayout
Padding="20, 20, 20, 20">
<Label Text="What's your favorite animal?" />
<RadioButton Text="Cat" x:Name="IsCat" CheckedChanged="IsCat_CheckedChanged" />
<RadioButton Text="Dog" x:Name="IsDog" CheckedChanged="IsDog_CheckedChanged"/>
<Button Text="Ok" Clicked="Button_Clicked"></Button>
</StackLayout>
</Frame>
</pages:PopupPage>
Here is background code of popup page,Page1.xaml.cs
I used MessagingCenter.Send
to send data from popup page to the mainPage.
public partial class Page1 : Rg.Plugins.Popup.Pages.PopupPage
{
bool IsCatSelect;
bool IsDogSelect;
public Page1()
{
InitializeComponent();
}
private void IsCat_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
IsCatSelect = e.Value;
}
private void IsDog_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
IsDogSelect = e.Value;
}
private async void Button_Clicked(object sender, EventArgs e)
{
MessagingCenter.Send<App, string>(App.Current as App, "OpenPage", "IsCatSelect: " + IsCatSelect.ToString()+ " IsCatSelect: "+IsDogSelect.ToString());
await Navigation.PopPopupAsync();
}
}