Skip to content

Instantly share code, notes, and snippets.

@codebeaulieu
Last active February 25, 2018 15:45
Show Gist options
  • Save codebeaulieu/a522d4cdcc600021ae80081dfb26d91d to your computer and use it in GitHub Desktop.
Save codebeaulieu/a522d4cdcc600021ae80081dfb26d91d to your computer and use it in GitHub Desktop.
public class Dashboard : ViewModelBase<Dashboard>
{
[DataMember]
public string Title
{
get { return "My Dashboard"; }
}
// 1
string _statusMessage;
[DataMember]
public string StatusMessage
{
get { return _statusMessage; }
set { this.RaiseAndSetIfChanged(ref _statusMessage, value); }
}
string _currentImage;
[DataMember]
public string CurrentImage
{
get { return _currentImage; }
set { this.RaiseAndSetIfChanged(ref _currentImage, value); }
}
[DataMember]
public List<string> ImageList { get; set; }
protected override void Initialize()
{
base.Initialize();
ImageList = new List<string>();
}
// 2
ReactiveCommand<Unit, Unit> _initializeCommand;
[DataMember]
public ReactiveCommand<Unit, Unit> InitializeCommand
{
get { return _initializeCommand; }
private set
{
this.RaiseAndSetIfChanged(ref _initializeCommand, value);
}
}
// 3
protected override void RegisterObservables()
{
// 4
InitializeCommand = ReactiveCommand.CreateFromTask(async _ =>
{
// initialization logic goes here
StatusMessage = "Initializing...";
// maybe we're getting images from a server
await Task.Delay(1500); // dont use Task.Delay in real life plz
StatusMessage = "Downloading...";
// simulate a lengthy server response
await Task.Delay(2500); // dont use Task.Delay in real life plz
StatusMessage = "Go-Go Random Logos!";
ImageList.Add("xamagon.png");
ImageList.Add("eightbot.png");
ImageList.Add("reactivelogo.png");
ImageList.Add("888.png");
ImageList.Add("Rx_Logo_512.png");
})
.DisposeWith(ViewModelBindings);
// 5
Observable
.Interval(TimeSpan.FromMilliseconds(500))
.SubscribeOn(RxApp.TaskpoolScheduler)
.Select(_ =>
{
if (ImageList.Count == 0)
return "reactivelogo.png";
Random random = new Random();
int number = random.Next(0, ImageList.Count);
return ImageList[number];
})
.ObserveOn(RxApp.MainThreadScheduler)
.BindTo(this, x => x.CurrentImage)
.DisposeWith(ViewModelBindings);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment