2013-03-04 46 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