在 Xamarin.uwp 中将此滑块控件与 Thumb Image 一起使用时,没有水平限制。但是,原始拇指效果很好。
您可以创建一个自定义控件来使用original Thumb style 来覆盖Thumb Image style。
更改:
<Thumb x:Name="HorizontalThumb"
Background="{ThemeResource SystemControlForegroundAccentBrush}"
Style="{StaticResource SliderThumbStyle}"
DataContext="{TemplateBinding Value}"
Height="24"
Width="8"
Grid.Row="0"
Grid.RowSpan="3"
Grid.Column="1"
AutomationProperties.AccessibilityView="Raw" />
<Thumb x:Name="HorizontalImageThumb"
Visibility="Collapsed"
Background="{ThemeResource SystemControlForegroundAccentBrush}"
Style="{StaticResource SliderThumbImageStyle}"
DataContext="{TemplateBinding Value}"
Tag="{Binding ThumbImageSource, RelativeSource={RelativeSource TemplatedParent}}"
Height="24"
Width="24"
Grid.Row="0"
Grid.RowSpan="3"
Grid.Column="1"
AutomationProperties.AccessibilityView="Raw" />
收件人:
<Thumb
x:Name="HorizontalThumb"
Grid.Row="0"
Grid.RowSpan="3"
Grid.Column="1"
Width="24"
Height="24"
AutomationProperties.AccessibilityView="Raw"
Style="{StaticResource SliderThumbImageStyle}"
Background="{ThemeResource SystemControlForegroundAccentBrush}"
DataContext="{TemplateBinding Value}"
Tag="{Binding ThumbImageSource, RelativeSource={RelativeSource TemplatedParent}}"
/>
并对用于交换拇指的方法评论。
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
Thumb = GetTemplateChild("HorizontalThumb") as Thumb;
ImageThumb = GetTemplateChild("HorizontalImageThumb") as Thumb;
//SwapThumbs(this);
OnReady();
}
并添加以下代码以使用此属性更改控件使用的默认样式。
public MySlider()
{
this.DefaultStyleKey = typeof(MySlider);
}
然后注释 Xamarin.uwp 的 MainPage.cs 中的代码。
LoadApplication(new App43.App());
最后,您可以在 Xamarin.uwp MainPage.xaml 中使用此控件。
<local:MySlider ThumbImageSource="Assets/pig.jpg"/>
结果:
我已经在 GitHub 上上传了我的示例,您可以下载 App43 文件夹以供参考。
https://github.com/WendyZang/Test.git