2013-03-04 78 views
2

我的網頁上有一個數據綁定FlipView,效果很好。我在我的頁面上有2個按鈕,用於前後導航FlipView。當我在模擬器中使用Mouse Mode或在本地機器模式下實際使用我的鼠標時,這些按鈕有效。FlipView.SelectedIndex鼠標模式與基本觸摸模式

但是,當我使用'基本觸摸模式'或在實際觸摸屏上使用我的應用程序並點擊按鈕時,FlipView會導航到下一個視圖,但會閃爍/向後翻轉。

這裏是我的FlipView:

我的按鈕:

<Button x:Name="ForwardButton" IsEnabled="{Binding CanGoForward}" Style="{StaticResource ForwardButtonStyle}" Tapped="ForwardButton_OnTapped" /> 

我的事件:

private void ForwardButton_OnTapped(object sender, TappedRoutedEventArgs e) 
{ 
      if (Flipper.SelectedIndex >= Flipper.Items.Count - 1) 
      { 
       Flipper.SelectedIndex = 0; 
      } 
      else 
      { 
       Flipper.SelectedIndex++; 
      } 

      e.Handled = true; 
} 

更新:

向FlipView添加OnSelectionChanged,我看到它在Basic touch mode中擊中了兩次,但在Mouse mode中只有一次,再次Mouse mode按預期工作。

回答

1

呀,所以我實現它是這樣的:

public sealed partial class MainPage : Page 
{ 
    public MainPage() 
    { 
     this.InitializeComponent(); 
    } 

    ViewModel ViewModel { get { return this.DataContext as ViewModel; } } 

    private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     this.ViewModel.MovePrevious(); 
    } 

    private void Button_Click_2(object sender, RoutedEventArgs e) 
    { 
     this.ViewModel.MoveNext(); 
    } 
} 

public class ViewModel : INotifyPropertyChanged 
{ 
    public ViewModel() { SelectedThing = ThingList.First(); } 

    List<int> m_ThingList = Enumerable.Range(1, 50).ToList(); 
    public List<int> ThingList { get { return m_ThingList; } set { SetProperty(ref m_ThingList, value); } } 

    int m_SelectedThing = default(int); 
    public int SelectedThing { get { return m_SelectedThing; } set { SetProperty(ref m_SelectedThing, value); } } 

    internal void MovePrevious() 
    { 
     var _CurrentIndex = ThingList.IndexOf(this.SelectedThing); 
     try { SelectedThing = ThingList[--_CurrentIndex]; } 
     catch { SelectedThing = ThingList.First(); } 
    } 

    internal void MoveNext() 
    { 
     var _CurrentIndex = ThingList.IndexOf(this.SelectedThing); 
     try { SelectedThing = ThingList[++_CurrentIndex]; } 
     catch { SelectedThing = ThingList.Last(); } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    void SetProperty<T>(ref T storage, T value, [System.Runtime.CompilerServices.CallerMemberName] String propertyName = null) 
    { 
     if (!object.Equals(storage, value)) 
     { 
      storage = value; 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

<Page.DataContext> 
    <local:ViewModel /> 
</Page.DataContext> 

<Page.BottomAppBar> 
    <AppBar> 
     <Grid> 
      <Button HorizontalAlignment="Left" Click="Button_Click_1">Previous</Button> 
      <Button HorizontalAlignment="Right" Click="Button_Click_2">Next</Button> 
     </Grid> 
    </AppBar> 
</Page.BottomAppBar> 

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> 
    <FlipView x:Name="MyFlip" 
       ItemsSource="{Binding ThingList}" 
       SelectedItem="{Binding SelectedThing, 
             Mode=TwoWay}" /> 
</Grid> 

那麼,它工作正常。你可以仔細檢查?或提供更多信息?否則,也許使用我的指南更新您的實施可以爲您解決這個問題。但願如此。

在所有的現實中,我會使用DelegateCommand來處理按鈕。但我並不想過多地使用鍋爐板代碼。如果你想看看我會做什麼,你可以看看這裏:http://blog.jerrynixon.com/2012/08/most-people-are-doing-mvvm-all-wrong.html

+0

謝謝,瞭解了很多關於MVVM,我實現你的代碼,並仍然得到相同的行爲。常規鼠標模式的工作原理很奇怪,如果我點擊按鈕就可以按預期工作,但是在「基本觸摸模式」下,它會翻轉回上一頁。 – mxmissile 2013-03-04 22:44:02

+0

我將你的代碼複製到一個新的解決方案,當然它的工作完美無瑕。所以這是我用FlipView做的事情。不知道是什麼。 – mxmissile 2013-03-04 23:21:31

+0

正在取得進展。我的按鈕位於FlipView的ItemTemplate中。我將它們放在AppBar中並按預期工作。所以我認爲它必須處理多個按鈕。仍然在挖... – mxmissile 2013-03-05 00:00:02