C# FlipView.SelectedIndex鼠标模式与基本触摸模式
我的页面上有一个数据绑定C# FlipView.SelectedIndex鼠标模式与基本触摸模式,c#,.net,xaml,windows-8,C#,.net,Xaml,Windows 8,我的页面上有一个数据绑定FlipView,效果非常好。我的页面上有两个按钮,我想用它们来向前和向后导航FlipView。当我在模拟器中使用鼠标模式或在本地机器模式下实际使用鼠标时,这些按钮起作用 但当我使用“基本触摸模式”或在实际触摸屏上使用我的应用程序并点击按钮时,FlipView会导航到下一个视图,但随后会闪烁/翻转回来 以下是我的FlipView: <FlipView x:Name="Flipper" ItemsSource="{Binding Path=Views}">...
FlipView
,效果非常好。我的页面上有两个按钮,我想用它们来向前和向后导航FlipView
。当我在模拟器中使用鼠标模式
或在本地机器模式下实际使用鼠标时,这些按钮起作用
但当我使用“基本触摸模式”或在实际触摸屏上使用我的应用程序并点击按钮时,FlipView会导航到下一个视图,但随后会闪烁/翻转回来
以下是我的FlipView:
<FlipView x:Name="Flipper" ItemsSource="{Binding Path=Views}">...</FlipView>
更新:
在FlipView中添加
OnSelectionChanged
,我看到它在基本触摸模式下点击了两次,但在鼠标模式下只点击了一次,再次点击鼠标模式就可以了。是的,所以我这样实现它:
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));
}
}
}
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
ViewModel ViewModel{get{返回this.DataContext作为ViewModel;}}
私有无效按钮\u单击\u 1(对象发送者,路由目标)
{
this.ViewModel.MovePrevious();
}
私有无效按钮\u单击\u 2(对象发送方,路由目标)
{
this.ViewModel.MoveNext();
}
}
公共类视图模型:INotifyPropertyChanged
{
public ViewModel(){SelectedThing=ThingList.First();}
List m_ThingList=Enumerable.Range(1,50).ToList();
公共列表ThingList{get{return m_ThingList;}set{SetProperty(ref m_ThingList,value);}
int m_SelectedThing=默认值(int);
public int SelectedThing{get{return m_SelectedThing;}set{SetProperty(ref m_SelectedThing,value);}
内部void MovePrevious()
{
var\u CurrentIndex=ThingList.IndexOf(this.SelectedThing);
请尝试{SelectedThing=ThingList[--\u CurrentIndex];}
catch{SelectedThing=ThingList.First();}
}
内部void MoveNext()
{
var\u CurrentIndex=ThingList.IndexOf(this.SelectedThing);
试试{SelectedThing=ThingList[+\u CurrentIndex];}
catch{SelectedThing=ThingList.Last();}
}
公共事件属性更改事件处理程序属性更改;
void SetProperty(ref T storage,T value,[System.Runtime.CompilerServices.CallerMemberName]字符串propertyName=null)
{
如果(!object.Equals(存储,值))
{
储存=价值;
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
及
谢谢,我学到了很多关于MVVM的知识,我实现了您的代码,但仍然得到了相同的行为。常规鼠标模式的工作方式很奇怪,如果我按一下按钮,它会按预期工作,但在“基本触摸模式”下,它会返回到上一页。我将您的代码复制到一个新的解决方案中,当然它工作得很完美。这就是我在FlipView上做的事情。不知道怎么办,正在取得进展。我的按钮位于FlipView的ItemTemplate中。我将它们放在AppBar中,并按预期工作。所以我假设这与渲染多个按钮有关。还在挖…是的,不是你不能在flipview中看到它们。如果你想那样做,那就行了。但也许阻力最小的路径可以帮助你的应用更快完成
private void ForwardButton_OnTapped(object sender, TappedRoutedEventArgs e)
{
if (Flipper.SelectedIndex >= Flipper.Items.Count - 1)
{
Flipper.SelectedIndex = 0;
}
else
{
Flipper.SelectedIndex++;
}
e.Handled = true;
}
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>