代码之家  ›  专栏  ›  技术社区  ›  Daniel A. Thompson

WinRT FlipView.SelectedIndex绑定未响应ViewModel中的更改

  •  1
  • Daniel A. Thompson  · 技术社区  · 9 年前

    我想以编程方式更改FlipView的SelectedIndex。我的ViewModel如下所示:

    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));
          }
       }
    }
    

    Set() 提高 INotifyPropertyChanged.PropertyChanged() .

    我的XAML如下所示:

    <views:BaseView>
     <Grid DataContext="{StaticResource ViewModel}">
      <FlipView SelectedIndex="{Binding FlipViewIndex}">
        <Control1 />
        <Control2 />
        <Control3 />
       </FlipView>
       <TextBlock Text="{Binding LogText}" />
      </Grid>
    </views.BaseView>
    

    视图和ViewModel似乎已正确绑定。当我打电话时 ViewModel.Log("foo") 从我的控制器中,TextBlock的文本被更新以反映更改。

    问题是当我打电话 ViewModel.SetIndex(n) ,FlipView的 SelectedIndex 未更新到 n ,它只是停留在0。你知道为什么会这样吗?

    1 回复  |  直到 9 年前
        1
  •  3
  •   Jerry Nixon    9 年前

    我刚刚确认这是可行的。

    <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++; }); }
        }
    }
    

    作品确实如此。

    祝你好运!