2012-02-07 39 views
1

我正在嘗試使用滑塊控件。這只是簡單的控制。沒有什麼花哨。但我碰到一個令我困惑的問題。wp7滑塊掛起

如果我把控件放在一個測試頁面上(沒有別的空白),並在應用程序啓動後立即導航到它,我可以完美地滑動它。但是,如果我先導航到另一個頁面,然後再導航到測試頁面。我有一個非常奇怪的行爲。滑塊控件逐步移動。看起來好像它掛起或失去焦點。

我正在使用wp7.1,並且已經在模擬器和手機上進行了測試。兩者都給了我相同的結果。我甚至不知道從哪裏開始解決這個問題,但我肯定需要一個滑塊並使其順利移動。

任何想法?

修訂,包括XAML:

<phone:PhoneApplicationPage 
x:Class="WP7ListBoxSelectedItemStyle.TestPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
FontFamily="{StaticResource PhoneFontFamilyNormal}" 
FontSize="{StaticResource PhoneFontSizeNormal}" 
Foreground="{StaticResource PhoneForegroundBrush}" 
SupportedOrientations="Portrait" Orientation="Portrait" 
xmlns:local="clr-namespace:WP7ListBoxSelectedItemStyle" 
xmlns:my="clr-namespace:colordata_controls;assembly=colordata_controls" 
shell:SystemTray.IsVisible="True" xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"> 

<!--LayoutRoot is the root grid where all page content is placed--> 
<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <!--TitlePanel contains the name of the application and page title--> 
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
     <TextBlock x:Name="ApplicationTitle" Text="IPO" Style="{StaticResource PhoneTextNormalStyle}"/> 
     <TextBlock x:Name="PageTitle" Text="Test" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
    </StackPanel> 

    <!--ContentPanel - place additional content here--> 
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="8,17,16,-17"> 
     <Slider Height="84" HorizontalAlignment="Left" Margin="10,10,0,0" Name="slider1" VerticalAlignment="Top" Width="460" /> 
    </Grid> 
</Grid> 

這裏是在模擬器中它的視頻的鏈接在行動。 https://vimeo.com/36428677

+0

你肯定有什麼在控制?發生這種情況,您在透視控件中有一個滑塊 – MyKuLLSKI 2012-02-07 21:59:59

+1

向我們展示XAML! – 2012-02-08 12:05:35

回答

0

所以,我仍然不知道爲什麼滑塊粘在那邊,但爲了繼續前進,我創建了自己的滑塊。希望這段代碼可以幫助別人。

XAML:

<UserControl 
x:Name="userControl" 
x:Class="controls.pSlider" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
d:DesignWidth="76" d:DesignHeight="400" Background="Gray" Foreground="White"> 

<Grid x:Name="LayoutRoot" Background="Transparent" MouseMove="LayoutRoot_MouseMove" MouseLeftButtonDown="LayoutRoot_MouseLeftButtonDown"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="10"/> 
     <RowDefinition/> 
     <RowDefinition Height="10"/> 
    </Grid.RowDefinitions> 
    <Rectangle x:Name="colorBar" Margin="20,6" Width="10" Fill="{Binding Background, ElementName=userControl}" Grid.Row="1"/> 
    <Grid x:Name="g_container" Grid.Row="1" Margin="0,1,0,13"> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition Height="0"/> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 
     <Grid x:Name="g_thumb" Height="0" VerticalAlignment="Top" Grid.Row="1"> 
      <Canvas Height="0" VerticalAlignment="Top"> 
       <Path Data="M0,0 L1,0 L1.2,0.5 L1,1 L0,1 z" Margin="0" Stretch="Fill" UseLayoutRounding="False" Width="33" RenderTransformOrigin="0.5,0.5" StrokeThickness="0" Fill="{Binding Foreground, ElementName=userControl}" Height="12" d:LayoutOverrides="VerticalAlignment"/> 
       <Path Data="M0.3,0.5 L0.5,0 L1.5,0 L1.5,1 L0.5,1 z" Margin="0" Stretch="Fill" UseLayoutRounding="False" Width="33" RenderTransformOrigin="0.5,0.5" StrokeThickness="0" Fill="{Binding Foreground, ElementName=userControl}" Height="12" Canvas.Left="43" d:LayoutOverrides="VerticalAlignment"/> 
      </Canvas> 
     </Grid> 
     <Rectangle x:Name="r_lifter" Margin="0" Grid.Row="2" Height="188" StrokeThickness="0"/> 
    </Grid> 
</Grid> 

代碼:

namespace controls { 
public partial class pSlider : UserControl { 

    public event RoutedEventHandler ValueChanged; 

    public static readonly DependencyProperty MaxProperty = 
    DependencyProperty.Register("Maximum", typeof(double), typeof(pSlider), new PropertyMetadata(100.0)); 

    public double Maximum { 
     get { return (double)GetValue(MaxProperty); } 
     set { SetValue(MaxProperty, value); } 
    } 

    public static readonly DependencyProperty MinProperty = 
    DependencyProperty.Register("Minimum", typeof(double), typeof(pSlider), new PropertyMetadata(0.0)); 

    public double Minimum { 
     get { return (double)GetValue(MinProperty); } 
     set { SetValue(MinProperty, value); } 
    } 

    public static readonly DependencyProperty ValueProperty = 
    DependencyProperty.Register("Value", typeof(double), typeof(pSlider), new PropertyMetadata(50.0)); 

    public double Value { 
     get { return (double)GetValue(ValueProperty); } 
     set { SetValue(ValueProperty, value); } 
    } 

    public pSlider() { 
     InitializeComponent(); 
     Loaded += new RoutedEventHandler(pSlider_Loaded); 
    } 

    void pSlider_Loaded(object sender, RoutedEventArgs e) { 

     if (Value > Maximum) 
      Value = Maximum; 
     else if (Value < Minimum) 
      Value = Minimum; 

     double min = 0; 
     double max = g_container.ActualHeight; 

     r_lifter.Height = Value/(Maximum - Minimum) * (max - min); 

    } 

    private Point Position; 

    private void LayoutRoot_MouseMove(object sender, MouseEventArgs e) { 
     Point newPosition = e.GetPosition((UIElement)sender); 

     double delta = newPosition.Y - Position.Y; 
     double temp = r_lifter.Height - delta; 

     if (temp > g_container.ActualHeight) 
      r_lifter.Height = g_container.ActualHeight; 
     else if (temp < 0) 
      r_lifter.Height = 0; 
     else 
      r_lifter.Height = temp; 

     double min = 0; 
     double max = g_container.ActualHeight; 

     Value = r_lifter.Height/(max - min) * (Maximum - Minimum); 
     Value = Math.Floor(Value); 
     RoutedEventHandler handler = ValueChanged; 
     if (handler != null) 
      handler(this, e); 


     Position = e.GetPosition((UIElement)sender); 
    } 

    private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { 
     Position = e.GetPosition((UIElement)sender); 
    } 

} 

}