C# WinRT FlipView.SelectedIndex绑定不响应ViewModel中的更改
我想以编程方式更改FlipView的SelectedIndex。我的ViewModel如下所示:C# WinRT FlipView.SelectedIndex绑定不响应ViewModel中的更改,c#,mvvm,windows-runtime,winrt-xaml,mytoolkit,C#,Mvvm,Windows Runtime,Winrt Xaml,Mytoolkit,我想以编程方式更改FlipView的SelectedIndex。我的ViewModel如下所示: public class MyViewModel : ViewModelBase { private int _flipViewIndex; public int FlipViewIndex { get { return _flipViewIndex; } private set { Set(ref _flipViewIndex, value); }
public class MyViewModel : ViewModelBase {
private int _flipViewIndex;
public int FlipViewIndex
{
get { return _flipViewIndex; }
private set { Set(ref _flipViewIndex, value); }
}
private string _logText;
public string LogText
{
get { return _logText; }
private set { Set(ref _logText, value); }
}
public async void Log(string text)
{
CoreDispatcher dispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher;
if (dispatcher.HasThreadAccess)
{
LogText += text + "\n";
}
else
{
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => Log(text));
}
}
public async void SetIndex(int index)
{
CoreDispatcher dispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher;
if (dispatcher.HasThreadAccess)
{
FlipViewIndex = index;
}
else
{
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => SetIndex(index));
}
}
}
<views:BaseView>
<Grid DataContext="{StaticResource ViewModel}">
<FlipView SelectedIndex="{Binding FlipViewIndex}">
<Control1 />
<Control2 />
<Control3 />
</FlipView>
<TextBlock Text="{Binding LogText}" />
</Grid>
</views.BaseView>
Set()
引发INotifyPropertyChanged.PropertyChanged()
我的XAML如下所示:
public class MyViewModel : ViewModelBase {
private int _flipViewIndex;
public int FlipViewIndex
{
get { return _flipViewIndex; }
private set { Set(ref _flipViewIndex, value); }
}
private string _logText;
public string LogText
{
get { return _logText; }
private set { Set(ref _logText, value); }
}
public async void Log(string text)
{
CoreDispatcher dispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher;
if (dispatcher.HasThreadAccess)
{
LogText += text + "\n";
}
else
{
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => Log(text));
}
}
public async void SetIndex(int index)
{
CoreDispatcher dispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher;
if (dispatcher.HasThreadAccess)
{
FlipViewIndex = index;
}
else
{
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => SetIndex(index));
}
}
}
<views:BaseView>
<Grid DataContext="{StaticResource ViewModel}">
<FlipView SelectedIndex="{Binding FlipViewIndex}">
<Control1 />
<Control2 />
<Control3 />
</FlipView>
<TextBlock Text="{Binding LogText}" />
</Grid>
</views.BaseView>
视图和视图模型似乎已正确绑定。当我从控制器调用ViewModel.Log(“foo”)
时,文本块的文本将更新以反映更改
问题是,当我调用
ViewModel.SetIndex(n)
时,FlipView的SelectedIndex
没有更新为n
,它只是停留在0。你知道为什么会发生这种情况吗?我刚刚证实了这一点
<FlipView FontSize="200"
ItemsSource="{Binding Items}"
SelectedIndex="{Binding Index, Mode=TwoWay}" />
就这么做
<Page.BottomAppBar>
<CommandBar>
<AppBarButton Command="{Binding PrevCommand}"
Icon="Previous"
Label="Previous" />
<AppBarButton Command="{Binding NextCommand}"
Icon="Next"
Label="Next" />
</CommandBar>
</Page.BottomAppBar>
这个视图模型
public class MyViewModel : BindableBase
{
public MyViewModel()
{
foreach (var item in new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 })
this.Items.Add(item);
}
ObservableCollection<int> _Items = new ObservableCollection<int>();
public ObservableCollection<int> Items { get { return _Items; } }
int _Index = default(int);
public int Index { get { return _Index; } set { base.SetProperty(ref _Index, value); } }
public DelegateCommand PrevCommand
{
get { return new DelegateCommand(() => { this.Index--; }); }
}
public DelegateCommand NextCommand
{
get { return new DelegateCommand(() => { this.Index++; }); }
}
}
公共类MyViewModel:BindableBase
{
公共MyViewModel()
{
foreach(新[]{1,2,3,4,5,6,7,8,9}中的变量项)
此.Items.Add(item);
}
ObservableCollection_Items=新的ObservableCollection();
公共可观测集合项{get{return\u Items;}}
int _Index=默认值(int);
公共int索引{get{return}Index;}set{base.SetProperty(ref{u Index,value);}
公共委托人命令
{
获取{returnnewdelegatecommand(()=>{this.Index-->});}
}
公共委派命令NextCommand
{
获取{returnnewdelegatecommand(()=>{this.Index++;});}
}
}
工作。真的
祝你好运 当然看起来不太对。使用SelectedItem是一个选项吗?嗨,Jerry-我还没有尝试过,但我们希望避免使用SelectedItem,这样我们可以更干净地将ViewModel与应用程序的其余部分分离。你比我更了解你的应用程序。但既然我们在讨论它,那么值得指出的是,对所选项目的引用与视图的关系远小于它在排序列表中的顺序位置。对于我的用户,我说选择的项目是Jerry。对于我的用户,我说选择的索引是。。。什么?我要考虑能见度吗?我算什么?我要考虑过滤器吗?SelectedItem是直截了当的。SelectedIndex混合了关注点,因此视图模型与视图密切相关。这不对吗?MVVM是灵活的,没有错。但指数是否更解耦?我不这么认为,我的观点是正确的。在本例中,FlipView的内容是静态的,因此它是一个非常直观的界面。本质上,我们使用FlipView来承载元素,这些元素依次表示结果1、当前情况和结果2。我们从索引1(当前情况)开始,希望以编程方式将视图翻转到依赖于不相关流程结果的索引:索引0(结果1)或索引2(结果2)。继续…视觉元素是严格的视觉;你可以把它们想象成一幅画或什么的。是否可以不绑定
SelectedIndex
?我觉得我正在服用疯狂的药丸(或者只是忽略了一些非常简单的东西)…我将从这里开始,一步一步地调整它,看看断开的地方在哪里。谢谢你的帮助!很好!我在XAML中缺少了Mode=TwoWay
。