Возился с reactiveui и пытался получить простой пример, работающий в универсальном приложении и имеющий некоторые различия.
У меня есть модель просмотра:
public class Rooms : ReactiveObject
{
private RoomsService roomService;
private IReactiveCommand<IList<Room>> fetchRooms;
private ObservableAsPropertyHelper<IList<Room>> myRooms;
public IList<Room> MyRooms
{
get { return myRooms.Value; }
}
public Rooms(RoomsService roomsService)
{
this.RoomService = roomsService;
fetchRooms = ReactiveCommand.CreateAsyncTask<IList<Room>>(Observable.Return<bool>(true), async x => await RoomService.Rooms);
fetchRooms.Subscribe(_ => LogMessage("Cool, it was invoked!"));
fetchRooms.ToProperty(this, x => x.MyRooms, out myRooms);
}
public RoomsService RoomService
{
get { return roomService; }
set { roomService = value; }
}
}
а служба, которая получает информацию, это:
public class RoomsService
{
private FakeRoomService service = new FakeRoomService();
public IObservable<IList<Room>> Rooms
{
get
{
LogMessage("RoomsService - Rooms property called.");
return Observable
.Interval(TimeSpan.FromSeconds(1))
.Select(_ => service.GetRoomsForUser());
}
}
}
с базовым кодом позади:
public sealed partial class HubPage : Page
{
private NavigationHelper navigationHelper;
private Rooms rooms = new Rooms(new RoomsService());
/// <summary>
/// Gets the NavigationHelper used to aid in navigation and process lifetime management.
/// </summary>
public NavigationHelper NavigationHelper
{
get { return this.navigationHelper; }
}
/// <summary>
/// Gets the rooms view model.
/// </summary>
public Rooms Rooms
{
get { return this.rooms; }
}
public HubPage()
{
this.InitializeComponent();
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
}
и последний xaml:
<Page
x:Name="pageRoot"
x:Class="HubPage"
DataContext="{Binding Rooms, RelativeSource={RelativeSource Self}}"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Hub"
xmlns:data="using:Hub.Data"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Hub SectionHeaderClick="Hub_SectionHeaderClick">
<HubSection Width="500" x:Uid="Section1Header" Header="Section 1">
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="300" />
</Grid.RowDefinitions>
<ListView Name="RoomListView" Grid.Row="1" ItemsSource="{Binding MyRooms}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</DataTemplate>
</HubSection>
</Hub>
</Grid>
I'm clearly missing something obvious in the viewmodel, but I'm not sure what it is. The task seems to tick over but the the property never gets updated... or if it does, its not propagating through to the view?
Любой совет или есть где-нибудь действительно простой рабочий пример для v6?