I'm really new to Xamarin.forms so pls be patient with me.
I try to give the user the chance to click on a button to create a new Button and initially the Button Name at the same time.
The user can create as many buttons as he wants, showed in a ScrollView. After he presses the button I want a little popup where he can enter the Name of the button.
void Mainpage_button_addNewButton_Clicked(object sender, EventArgs e)
{
var entry = new Namenseingabe();
Button button = new Button
{
//Text = entry.Name, Here i try to get the Value from the Entry
AutomationId = id.ToString(),
};
MainPageButtonStackLayout.Children.Add(button);
mainPageButtons.Add(button);
}
Here is the xaml from the Entry:
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<StackLayout Orientation="Vertical" HeightRequest="200" WidthRequest="300" BackgroundColor="White">
<Entry x:Name="EntryValue" Margin="20,20,20,10" Placeholder="Name" Completed="EntryValue_Completed"></Entry>
<Button Margin="20,0,20,0" Text="Save"></Button>
</StackLayout>
</StackLayout>
</ContentView.Content>
I only get the value in the Method EntryValue_Completed()
like here:
private void EntryValue_Completed(object sender, EventArgs e)
{
var a = ((Entry)sender).Text;
}
But how I can get the value in the method where I need it? Maybe you can help me with how I can do it better or faster.
According to your description, you want to have one popup window to enter Button name and save this Button, I suggest you can use Rg.Plugins.Popup to do this.
Firstly, install Rg.Plugins.Popup by Nuget packages, then create popup page like the following code:
<?xml version="1.0" encoding="utf-8" ?>
<popup:PopupPage
x:Class="demo2.simplecontrol.Page8"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:popup="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup">
<ScrollView VerticalOptions="Center">
<Frame Margin="15" BackgroundColor="White">
<StackLayout
Padding="10,5"
IsClippedToBounds="True"
Spacing="3">
<Entry x:Name="entry1" />
<Button
x:Name="btn1"
Clicked="Btn1_Clicked"
Text="save" />
</StackLayout>
</Frame>
</ScrollView>
</popup:PopupPage>
public partial class Page8:PopupPage
{
public Page8 ()
{
InitializeComponent ();
}
private async void Btn1_Clicked(object sender, EventArgs e)
{
MessagingCenter.Send<Page7, string>(new Page7(), "add", entry1.Text);
await PopupNavigation.Instance.PopAllAsync();
}
}
Then using MessagingCenter to send the entry value.
<ContentPage
x:Class="demo2.simplecontrol.Page7"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<ContentPage.Content>
<StackLayout x:Name="stacklayout1">
<Label
HorizontalOptions="CenterAndExpand"
Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand" />
<Button
x:Name="btn1"
Clicked="Btn1_Clicked"
Text="create button" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
public partial class Page7 : ContentPage
{
public Page7 ()
{
InitializeComponent ();
MessagingCenter.Subscribe<Page7, string>(this, "add", (sender, arg) =>
{
Button btn = new Button()
{
Text = (string)arg
};
stacklayout1.Children.Add(btn);
});
}
private async void Btn1_Clicked(object sender, EventArgs e)
{
await PopupNavigation.Instance.PushAsync(new Page8());
}
}
About using Rg.Plugins.Popup, you can take a look:https://github.com/rotorgames/Rg.Plugins.Popup/blob/master/src/Demo/Demo/Pages/LoginPopupPage.xaml.cs
about using MessagingCenter, you can refer to:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center
If my reply solved your issue, please remember to mark my reply as answer, thanks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With