Xamarin Forms 本机视图中的 RadioGroup [英] RadioGroup in Xamarin Forms Native Views
问题描述
我有以下几行 XAML,它们在 Xamarin 表单中使用本机视图:
I have the following lines of XAML that utilises Native Views in Xamarin Forms:
<androidWidget:RadioGroup x:Arguments="{x:Static formsAndroid:Forms.Context}">
<androidWidget:RadioButton x:Arguments="{x:Static formsAndroid:Forms.Context}" Click="RadioButton1Clicked"/>
<androidWidget:RadioButton x:Arguments="{x:Static formsAndroid:Forms.Context}" Click="RadioButton2Clicked"/>
<androidWidget:RadioButton x:Arguments="{x:Static formsAndroid:Forms.Context}" Click="RadioButton3Clicked"/>
<androidWidget:RadioButton x:Arguments="{x:Static formsAndroid:Forms.Context}" Click="RadioButton4Clicked"/>
</androidWidget:RadioGroup>
但是,抛出以下异常:无法设置androidWidget:RadioGroup的内容,因为它没有ContentPropertyAttribute"
However, the following exception is thrown: "Can not set the content of androidWidget:RadioGroup as it doesn't have a ContentPropertyAttribute"
只是想知道是否有人可以帮助我找到与 RadioButton 相关的 RadioGroup 的正确 XAML 布局?
Just wondering if anyone could help me find the correct XAML layout for the RadioGroup in relation to the RadioButtons?
干杯
推荐答案
但是,抛出以下异常:无法设置androidWidget:RadioGroup的内容,因为它没有ContentPropertyAttribute"
However, the following exception is thrown: "Can not set the content of androidWidget:RadioGroup as it doesn't have a ContentPropertyAttribute"
并非所有原生视图都可以直接在 xaml 中使用.RadioGroup
就是其中之一.
Not all native views can be used in xaml directly. RadioGroup
is one of them.
要在您的 Xaml 中使用它,您需要按照以下步骤操作:
To use it in your Xaml, you need to follow the below steps:
在
YourProject.Droid
中创建一个自定义的RadioGroup
控件:
In
YourProject.Droid
create a customRadioGroup
control:
public class MyRadioGroup:RadioGroup
{
//Every native control in xaml will be wrapped in NativeViewWrapper, so we want to pass a NativeViewWrapper list here
IList<NativeViewWrapper> items;
public IList<NativeViewWrapper> ItemsSource
{
get {
items.Clear();
for (int i = 0; i < this.ChildCount; i++)
{
items.Add(new NativeViewWrapper(this.GetChildAt(i)));
}
return items;
}
set {
//xaml compiler will call this setter
if (items != value)
{
items = value;
this.RemoveAllViews();
foreach (NativeViewWrapper wrapper in items)
{
this.AddView(wrapper.NativeView);
}
}
}
}
public MyRadioGroup(Context context) : base(context)
{
items = new List<NativeViewWrapper>();
}
}
在您的 pcl 库中添加必要的命名空间:
In your pcl library add necessary namespaces:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
...
xmlns:androidWrapper="clr-namespace:Xamarin.Forms.Platform.Android;assembly=Xamarin.Forms.Platform.Android;targetPlatform=Android"
xmlns:androidWidget="clr-namespace:Android.Widget;assembly=Mono.Android;targetPlatform=Android"
xmlns:androidLocal="clr-namespace:NativeSwitch.Droid;assembly=NativeSwitch.Droid;targetPlatform=Android"
xmlns:formsAndroid="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Platform.Android;targetPlatform=Android"
xmlns:local="clr-namespace:NativeSwitch"
...">
在您的 xaml 页面中引用自定义 MyRadioGroup
:
<StackLayout Margin="20">
<androidLocal:MyRadioGroup x:Arguments="{x:Static formsAndroid:Forms.Context}" >
<androidLocal:MyRadioGroup.ItemsSource>
<x:Array Type="{x:Type androidWrapper:NativeViewWrapper}">
<androidWidget:RadioButton x:Arguments="{x:Static formsAndroid:Forms.Context}" Text="Scale1" />
<androidWidget:RadioButton x:Arguments="{x:Static formsAndroid:Forms.Context}" Text="Scale2" />
<androidWidget:RadioButton x:Arguments="{x:Static formsAndroid:Forms.Context}" Text="Scale3" />
<androidWidget:RadioButton x:Arguments="{x:Static formsAndroid:Forms.Context}" Text="Scale4" />
</x:Array>
</androidLocal:MyRadioGroup.ItemsSource>
</androidLocal:MyRadioGroup>
</StackLayout>
您可以在此处找到完整的演示.
You can find a complete demo here.
类似的官方教程请参考子类化原生视图
有关在 Xaml 中传递参数,请参阅传递参数Xml
For passing arguments inside Xaml, please refer to Passing Arguments in Xaml
这篇关于Xamarin Forms 本机视图中的 RadioGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!