问题描述
使用Xamarin表单有点新手,我想知道是否有一种方法可以在下图中以Xamarin表单创建切换按钮,并且该方法在两种平台(Android和iOS)上都可以使用。
唯一的方法是,要实现这一点,我需要导入材料设计库。
解决方法
您可以从nuget安装插件Plugin.SegmentedControl。
iOS 将初始化程序添加到AppDelegate
public override bool FinishedLaunching(UIApplication app,NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
Plugin.Segmented.Control.iOS.SegmentedControlRenderer.Initialize();
...
}
用法
<control:SegmentedControl
x:Name="SegmentedControl"
SelectedSegment="{Binding SelectedSegment,Mode=TwoWay}"
TintColor="BlueViolet"
SelectedTextColor="White"
DisabledColor="Gray"
BorderColor="Black"
BorderWidth="2.0"
FontSize="Small"
FontFamily="{StaticResource PlatformFontName}"
Margin="8,8,8"
SegmentSelectedCommand="{Binding SegmentChangedCommand}"
OnElementChildrenChanging="OnElementChildrenChanging"
ItemsSource="{Binding SegmentStringSource}">
<!--<control:SegmentedControl.Children>
<control:SegmentedControlOption Text="{Binding ChangeText}"/>
<control:SegmentedControlOption Text="Item 2"/>
<control:SegmentedControlOption Text="Item 3"/>
<control:SegmentedControlOption Text="Item 4"/>
</control:SegmentedControl.Children>-->
</control:SegmentedControl>