【问题标题】:Xamarin : Segmented Control for windows uwpXamarin:Windows uwp 的分段控制
【发布时间】:2016-12-28 21:04:16
【问题描述】:

如何在 Windows UWP 中为分段控制制作自定义渲染器。我按照这个link 在IOS 和android 中自定义渲染分段控件。但我找不到任何适用于 Windows UWP 的东西。任何人都可以在此提供任何资源,或者是否有任何替代方式来执行此操作,类似于 Windows UWP 中的分段控制。

【问题讨论】:

    标签: c# xamarin.forms uwp custom-renderer


    【解决方案1】:

    我自己找到了解决方案。

    我最近发现了一个 nuget 包FreshEssentials。它具有分段按钮组的实现。按照此github link 查找实现。它易于使用。

    【讨论】:

    【解决方案2】:

    现在有一个 Xamarin Forms 跨平台库可以执行此操作。

    Plugin Segmented Control for .NET Standard

    【讨论】:

      【解决方案3】:

      我用自定义样式的 RadioButtons 做过一次。为您的按钮编辑 RadioButton 样式 - 创建您想要的选中和未选中样式,并为它们分配相同的组,以便您一次只能选择一个按钮。 不知道有没有更简单的方法。

      编辑:示例代码,按照你想要的方式编辑它:

      <Page.Resources>
          <Style x:Key="RadioButtonStyle1" TargetType="RadioButton">
              <Setter Property="Background" Value="Transparent"/>
              <Setter Property="Foreground" Value="{ThemeResource RadioButtonContentForegroundThemeBrush}"/>
              <Setter Property="Padding" Value="0,0,0,0"/>
              <Setter Property="HorizontalAlignment" Value="Center"/>
              <Setter Property="VerticalAlignment" Value="Stretch"/>
              <Setter Property="HorizontalContentAlignment" Value="Center"/>
              <Setter Property="VerticalContentAlignment" Value="Center"/>
              <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
              <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
              <Setter Property="Template">
                  <Setter.Value>
                      <ControlTemplate TargetType="RadioButton">
                          <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
                              Background="{TemplateBinding Background}">
                              <VisualStateManager.VisualStateGroups>
                                  <VisualStateGroup x:Name="CommonStates">
                                      <VisualState x:Name="Normal"/>
                                      <VisualState x:Name="PointerOver">
      
                                      </VisualState>
                                      <VisualState x:Name="Pressed">
      
                                      </VisualState>
                                      <VisualState x:Name="Disabled">
                                          <Storyboard>
                                              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BackgroundRectangle">
                                                  <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource RadioButtonDisabledBackgroundThemeBrush}"/>
                                              </ObjectAnimationUsingKeyFrames>
                                              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                  <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource RadioButtonContentDisabledForegroundThemeBrush}"/>
                                              </ObjectAnimationUsingKeyFrames>
                                          </Storyboard>
                                      </VisualState>
                                  </VisualStateGroup>
                                  <VisualStateGroup x:Name="CheckStates">
                                      <VisualState x:Name="Checked">
                                          <Storyboard>
                                              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BackgroundRectangle">
                                                  <DiscreteObjectKeyFrame KeyTime="0" Value="Blue"/>
                                              </ObjectAnimationUsingKeyFrames>
                                              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                  <DiscreteObjectKeyFrame KeyTime="0" Value="White"/>
                                              </ObjectAnimationUsingKeyFrames>
                                          </Storyboard>
                                      </VisualState>
                                      <VisualState x:Name="Unchecked">
      
                                      </VisualState>
                                      <VisualState x:Name="Indeterminate"/>
                                  </VisualStateGroup>
                                  <VisualStateGroup x:Name="FocusStates">
                                      <VisualState x:Name="Focused">
                                          <Storyboard>
                                              <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualWhite"/>
                                              <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualBlack"/>
                                          </Storyboard>
                                      </VisualState>
                                      <VisualState x:Name="Unfocused"/>
                                      <VisualState x:Name="PointerFocused"/>
                                  </VisualStateGroup>
                              </VisualStateManager.VisualStateGroups>
      
      
                              <Grid HorizontalAlignment="Stretch">
                                  <Grid x:Name="BackgroundRectangle" Background="Transparent">
                                      <Border BorderThickness="1" BorderBrush="White" Margin="0" HorizontalAlignment="Stretch">
                                          <Rectangle Margin="0" StrokeThickness="{ThemeResource RadioButtonBorderThemeThickness}" HorizontalAlignment="Stretch" UseLayoutRounding="False"/>
                                      </Border>
                                      <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="Title"></TextBlock>
      
                                  </Grid>
                                  <ContentPresenter x:Name="ContentPresenter" Foreground="White" AutomationProperties.AccessibilityView="Raw" 
                                                ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" 
                                                Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                                Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                              </Grid>
                          </Border>
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
      </Page.Resources>
      
      
      <Grid Height="40" Background="Black" BorderThickness="0">
          <Grid.ColumnDefinitions>
              <ColumnDefinition Width="10"/>
              <ColumnDefinition Width="Auto"/>
              <ColumnDefinition Width="Auto"/>
              <ColumnDefinition Width="Auto"/>
              <ColumnDefinition Width="Auto"/>
              <ColumnDefinition Width="10"/>
          </Grid.ColumnDefinitions>
      
          <RadioButton Height="30" Content="Button1" Grid.Column="1" Style="{StaticResource RadioButtonStyle1}" GroupName="MainGroup" Checked="Button1_OnChecked"></RadioButton>
          <RadioButton Height="30" Content="Button2" Grid.Column="2" Style="{StaticResource RadioButtonStyle1}" GroupName="MainGroup" Checked="Button2_OnChecked"></RadioButton>
          <RadioButton Height="30" Content="Button3" Grid.Column="3" Style="{StaticResource RadioButtonStyle1}" GroupName="MainGroup" Checked="Button3_OnChecked"></RadioButton>
          <RadioButton Height="30" Content="Button4" Grid.Column="4" Style="{StaticResource RadioButtonStyle1}" GroupName="MainGroup" Checked="Button4_OnChecked"></RadioButton>
      </Grid>
      

      【讨论】:

      • 您能否提供一些在uwp上进行自定义的代码。是的,它可以通过单选按钮来完成,但不确定在这种情况下是否在 uwp 中使用布局模板。
      • 在样式开始的那一行下,您可以看到一些 行 - rg。 将填充设置为 0,然后您可以将此值与使用 Padding="{TemplateBinding Padding}" 的样式定义的任何控件一起使用,您可以在此处阅读有关 ThemeResource 的信息:@ 987654321@ 但您可能只需要 ThemeResource 使用默认系统样式的信息 - 因此您可以使用 Value="Red" 或任何其他值更改(并且应该)每个 Value="{ThemeResource ...}" 或您想要的任何其他值
      • 我将 ThemeResource 值设置为 。我在我的 PCl 中实现了这个 Xaml 并尝试运行,但没有获得所需的输出。我是否缺少任何外部链接资源。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多