有没有办法在C#中设置Xamarin radiobutton检查法方法?
处理基本概念证明,试图使用数据库中的信息来构建整个视图。根据我到目前为止的内容:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="FieldDataTemplateSelectorSample.DynamicView">
<ContentPage.Content>
<StackLayout>
<StackLayout Spacing="2" x:Name="EntryFieldsStack">
<!-- empty in xaml because we add details in code behind -->
</StackLayout>
<Label Text="nothing selected" x:Name="SelectedLabelName" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
and the code behind so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using FieldDataTemplateSelectorSample.Validations;
namespace FieldDataTemplateSelectorSample
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DynamicView : ContentPage
{
public DynamicView()
{
InitializeComponent();
LoadDynamicFields();
}
public void OnRadioChanged(object sender, CheckedChangedEventArgs e)
{
SelectedLabelName.Text = String.Format("Selected value is {0}", e.Value);
}
public void LoadDynamicFields()
{
EntryFieldsStack.Children.Clear();
EntryFieldsStack.Children.Add(new Label { Text = "Dynamic Radio"});
for (int i = 0; i < 10; i++)
{
EntryFieldsStack.Children.Add(new RadioButton
{
Value = i.ToString(),
Content = string.Format("Radio text {0}", i.ToString()),
GroupName = "RadioGroup1"
});
}
}
}
}
因此,现在很难添加带有10个选择的广播按钮。我希望能够为c#中添加到OnradioChanged方法中的每个RadioButton设置CheckedChanged方法名称,但该属性未显示在列表中。还是我需要对InotifyPropertychanged做点事?在此示例中,我只想更新具有所选值的标签,但最终我想提出属性汇总的事件,因此,如果还有其他字段需要更新,他们可以收听它。
我还假设一旦我弄清楚如何设置该方法,则包括组名,因此,如果我添加了这样的多个无线电组,我可以区分它们?如果没有,我该如何添加?我尝试添加一个嵌套的堆叠式堆栈,但我无法设置GroupName属性。
谢谢
Working on a basic proof of concept trying to build an entire view using information from a database. Based on what I have so far:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="FieldDataTemplateSelectorSample.DynamicView">
<ContentPage.Content>
<StackLayout>
<StackLayout Spacing="2" x:Name="EntryFieldsStack">
<!-- empty in xaml because we add details in code behind -->
</StackLayout>
<Label Text="nothing selected" x:Name="SelectedLabelName" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
and the code behind so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using FieldDataTemplateSelectorSample.Validations;
namespace FieldDataTemplateSelectorSample
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DynamicView : ContentPage
{
public DynamicView()
{
InitializeComponent();
LoadDynamicFields();
}
public void OnRadioChanged(object sender, CheckedChangedEventArgs e)
{
SelectedLabelName.Text = String.Format("Selected value is {0}", e.Value);
}
public void LoadDynamicFields()
{
EntryFieldsStack.Children.Clear();
EntryFieldsStack.Children.Add(new Label { Text = "Dynamic Radio"});
for (int i = 0; i < 10; i++)
{
EntryFieldsStack.Children.Add(new RadioButton
{
Value = i.ToString(),
Content = string.Format("Radio text {0}", i.ToString()),
GroupName = "RadioGroup1"
});
}
}
}
}
So for now it is hard-coded to add a radio button with 10 choices. I'd like to be able to set the CheckedChanged method name for each RadioButton added in c# to the OnRadioChanged method but that property isn't showing in the list. Or would I need to do something with the INotifyPropertyChanged? For this example I just want to update a label with the selected value but eventually I'll want to raise the PropertyChanged event so if there are other fields that need to update, they can listen for it.
I'm also assuming that once I figure out how to set the method, that the GroupName is included so if I have multiple radio groups added like this I can differentiate between them? If not, how can I add it? I tried adding a nested StackLayout but the GroupName property wasn't available for me to set.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法使用对属性的内联语法分配事件处理
程序
you can't assign event handlers using the inline syntax you do for properties
try this