如何在c#/ xaml metro app中将时间轴标记放在幻灯片上

时间:2012-07-23 05:07:08

标签: c# xaml windows-8 microsoft-metro windows-runtime

我检查了this样本。它向我展示了TimeLineMarker API。现在,当我按下“添加标记”按钮时,添加了标记,文本块显示标记,在此时添加了标记。现在我想以任何形状或任何形状的任何符号的形式将这些标记显示在Slider控件上,所以当我想直接进入标记时,我可以直接点击滑块。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

最后得到了MSDN的答案,谢谢Matt

XAML

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel VerticalAlignment="Center">
         <Grid>
            <Canvas x:Name="MyCanvas" Width="{Binding ElementName=MySlider, Path=ActualWidth}" Height="{Binding ElementName=MySlider, Path=ActualHeight}" Background="BlueViolet"/>
             <Slider x:Name="MySlider" Orientation="Horizontal" Width="750" Height="50" Loaded="MySlider_Loaded_1"/>
         </Grid>
         <Button Content="Set marker" Click="Button_Click_1"/>
     </StackPanel>
</Grid>

C#

int UniqueId = 0;
List<CustomTick> MyCustomTicks = new List<CustomTick>();
Thumb MySliderThumb;
Point ThumbCurrentPoint;
private void MySlider_Loaded_1(object sender, RoutedEventArgs e)
{
    FrameworkElement templateRoot = VisualTreeHelper.GetChild((Slider)sender, 0) as FrameworkElement;
    MySliderThumb = templateRoot.FindName("HorizontalThumb") as Thumb;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
    MySliderThumb.TransformToVisual(MyCanvas).TryTransform(new Point(0, 0), out ThumbCurrentPoint);
    CustomTick MyCustomTick = new CustomTick();
    MyCustomTick.X = ThumbCurrentPoint.X;
    MyCustomTick.Y = ThumbCurrentPoint.Y;
    MyCustomTick.Value = MySlider.Value;
    MyCustomTick.ID = UniqueId++;
    MyCustomTicks.Add(MyCustomTick);
    Windows.UI.Xaml.Shapes.Rectangle MyRectangle = new Windows.UI.Xaml.Shapes.Rectangle();
    MyRectangle.Name = MyCustomTick.ID.ToString();
    MyRectangle.Fill = new SolidColorBrush(Windows.UI.Colors.Red);
    MyRectangle.Width = MySliderThumb.ActualWidth;
    MyRectangle.Height = MySliderThumb.ActualHeight;
    MyRectangle.PointerPressed += MyRectangle_PointerPressed;
    MyCanvas.Children.Add(MyRectangle);
    Canvas.SetTop(MyRectangle, MyCustomTick.Y - MyRectangle.Height );
    Canvas.SetLeft(MyRectangle, MyCustomTick.X);
}
void MyRectangle_PointerPressed(object sender, PointerRoutedEventArgs e)
{
    Windows.UI.Xaml.Shapes.Rectangle MyRectangle = sender as Windows.UI.Xaml.Shapes.Rectangle;
    CustomTick MyCustomTick = new CustomTick();
    foreach (CustomTick CT in MyCustomTicks)
    {
if (CT.ID.ToString() == MyRectangle.Name)
{
    MyCustomTick = CT;
    break;
}
    }
    MySlider.Value = MyCustomTick.Value;
}

List<CustomTick> MyTicks = new System.Collections.Generic.List<CustomTick>();
private class CustomTick
{
    public double X;
    public double Y;
    public double Value;
    public int ID;
}