我真的是Xamarin.forms的新手,所以请耐心等待我。
我尝试让用户有机会单击一个按钮来创建一个新的Button,并同时创建一个最初的Button Name。
用户可以根据需要创建任意数量的按钮,显示在ScrollView中。 他按下按钮后,我想弹出一个小窗口,在那里他可以输入按钮的名称。
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);
}
这是条目中的xaml:
<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>
我只能在方法EntryValue_Completed()
中获得值,如下所示:
private void EntryValue_Completed(object sender, EventArgs e)
{
var a = ((Entry)sender).Text;
}
但是如何在需要的方法中获取值?也许您可以帮助我更好或更快地做到这一点。
答案 0 :(得分:1)
您需要实现Event Aggregator模式。 MVVMLight库Messenger
对新手来说很简单这个概念是一个类,它将通知另一个条目名称并传递输入的字符串
我还注意到您正在使用Xamarin.Forms,因此一个快速的Google会显示它们具有自己的Messenger服务来完成您想要的操作:look here
您将使用消息传递中心并发布条目名称,主页已经订阅了任何发布内容
答案 1 :(得分:1)
根据您的描述,您希望有一个弹出窗口输入Button名称并保存此Button,我建议您可以使用Rg.Plugins.Popup来做到这一点。
首先,通过Nuget软件包安装 Rg.Plugins.Popup ,然后按照以下代码创建弹出页面:
<?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();
}
}
然后使用 MessagingCenter 发送条目值。
<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());
}
}
关于使用Rg.Plugins.Popup,您可以看一下:https://github.com/rotorgames/Rg.Plugins.Popup/blob/master/src/Demo/Demo/Pages/LoginPopupPage.xaml.cs
有关使用MessagingCenter的信息,您可以参考:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center
如果我的答复解决了您的问题,请记住将我的答复标记为答复,谢谢。