2016-11-15 86 views
0

我完全不熟悉ReactiveUI,需要您的專家幫助才能找出如何實現以下內容。我有一個在C#中的UWP應用程序,其中包含一個顯示一堆項目的ListView(不重要的項目類型,所以讓我們假設它顯示了一堆整數)的頁面。ReactiveUI和響應列表中的更改

當Page.Loaded被調用時,我向服務器發送查詢,獲取結果(整數數組),並用結果填充ListView。

我想要做的是顯示一個ProgressRing,直到從服務器收到的數據完成填充(即新項目全部添加)。一旦完成這兩個動作,我想隱藏ProgressRing。

我想使用ReactiveUI來完成上述操作,但一直未能弄清楚如何做到這一點。

任何幫助,將不勝感激

回答

0

我從來沒有與UWP的工作,但我可以提供與WPF,我希望將是有益的例子。 在這個例子中我使用進度環從MahApps.Metro

視圖模型

public class MainViewModel : ReactiveObject 
{ 
    private readonly ObservableAsPropertyHelper<bool> _isLoading; 

    public MainViewModel() 
    { 
     Load = ReactiveCommand.CreateFromTask(async() => 
     { 
      // Simulating a long running task, like DB query 
      await Task.Delay(TimeSpan.FromSeconds(10)); 
      // Populating list 
      Items.Clear(); 
      Items.AddRange(Enumerable.Range(0, 30).Where(x => x%2 == 0)); 
     }); 
     Load.IsExecuting.ToProperty(this, x => x.IsLoading, out _isLoading); 
    } 

    public bool IsLoading => _isLoading.Value; 
    public ReactiveList<int> Items { get; } = new ReactiveList<int>(); 
    public ReactiveCommand<Unit, Unit> Load { get; } 
} 

查看

public partial class MainWindow : IViewFor<MainViewModel> 
{ 
    public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(nameof(ViewModel), typeof(MainViewModel), typeof(MainWindow)); 
    public MainWindow() 
    { 
     InitializeComponent(); 
     ViewModel = new MainViewModel(); 
     this.OneWayBind(ViewModel, x => x.Items, x => x.ListView.ItemsSource); 
     this.OneWayBind(ViewModel, x => x.IsLoading, x => x.ProgressRing.IsActive); 
     this.Events().Loaded.Subscribe(_ => this.ViewModel.Load.Execute().Subscribe()); 
    } 
    public MainViewModel ViewModel 
    { 
     get { return (MainViewModel)GetValue(ViewModelProperty); } 
     set { SetValue(ViewModelProperty, value); } 
    } 

    object IViewFor.ViewModel 
    { 
     get { return ViewModel; } 
     set { ViewModel = (MainViewModel)value; } 
    } 
} 
<Window x:Class="ProgressRing.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls" 
        Title="MainWindow"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition /> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 
    <ScrollViewer Grid.Row="0"> 
     <ListView x:Name="ListView" ItemStringFormat="Item {0}" /> 
    </ScrollViewer> 
    <controls:ProgressRing x:Name="ProgressRing" Grid.Row="1" /> 
</Grid>