本文整理汇总了C#中ReactiveCommand.CanExecute方法的典型用法代码示例。如果您正苦于以下问题:C# ReactiveCommand.CanExecute方法的具体用法?C# ReactiveCommand.CanExecute怎么用?C# ReactiveCommand.CanExecute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ReactiveCommand
的用法示例。
在下文中一共展示了ReactiveCommand.CanExecute方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReactiveCommandAllFlow
public void ReactiveCommandAllFlow()
{
var testScheduler = new TestScheduler();
var @null = (object)null;
var recorder1 = testScheduler.CreateObserver<object>();
var recorder2 = testScheduler.CreateObserver<object>();
var cmd = new ReactiveCommand();
cmd.Subscribe(recorder1);
cmd.Subscribe(recorder2);
cmd.CanExecute().Is(true);
cmd.Execute(); testScheduler.AdvanceBy(10);
cmd.Execute(); testScheduler.AdvanceBy(10);
cmd.Execute(); testScheduler.AdvanceBy(10);
cmd.Dispose();
cmd.CanExecute().Is(false);
cmd.Dispose(); // dispose again
recorder1.Messages.Is(
OnNext(0, @null),
OnNext(10, @null),
OnNext(20, @null),
OnCompleted<object>(30));
recorder2.Messages.Is(
OnNext(0, @null),
OnNext(10, @null),
OnNext(20, @null),
OnCompleted<object>(30));
}
示例2: AllowConcurrentExecutionTest
public void AllowConcurrentExecutionTest()
{
(new TestScheduler()).With(sched => {
var fixture = new ReactiveCommand(null, true, sched);
Assert.True(fixture.CanExecute(null));
var result = fixture.RegisterAsync(_ => Observable.Return(4).Delay(TimeSpan.FromSeconds(5), sched))
.CreateCollection();
Assert.Equal(0, result.Count);
sched.AdvanceToMs(25);
Assert.Equal(0, result.Count);
fixture.Execute(null);
Assert.True(fixture.CanExecute(null));
Assert.Equal(0, result.Count);
sched.AdvanceToMs(2500);
Assert.True(fixture.CanExecute(null));
Assert.Equal(0, result.Count);
sched.AdvanceToMs(5500);
Assert.True(fixture.CanExecute(null));
Assert.Equal(1, result.Count);
});
}
示例3: CanExecuteShouldChangeOnInflightOp
public void CanExecuteShouldChangeOnInflightOp()
{
(new TestScheduler()).With(sched => {
var canExecute = sched.CreateHotObservable(
sched.OnNextAt(0, true),
sched.OnNextAt(250, false),
sched.OnNextAt(500, true),
sched.OnNextAt(750, false),
sched.OnNextAt(1000, true),
sched.OnNextAt(1100, false)
);
var fixture = new ReactiveCommand(canExecute);
int calculatedResult = -1;
bool latestCanExecute = false;
fixture.RegisterAsync(x =>
Observable.Return((int)x*5).Delay(TimeSpan.FromMilliseconds(900), RxApp.MainThreadScheduler))
.Subscribe(x => calculatedResult = x);
fixture.CanExecuteObservable.Subscribe(x => latestCanExecute = x);
// CanExecute should be true, both input observable is true
// and we don't have anything inflight
sched.AdvanceToMs(10);
Assert.True(fixture.CanExecute(1));
Assert.True(latestCanExecute);
// Invoke a command 10ms in
fixture.Execute(1);
// At 300ms, input is false
sched.AdvanceToMs(300);
Assert.False(fixture.CanExecute(1));
Assert.False(latestCanExecute);
// At 600ms, input is true, but the command is still running
sched.AdvanceToMs(600);
Assert.False(fixture.CanExecute(1));
Assert.False(latestCanExecute);
// After we've completed, we should still be false, since from
// 750ms-1000ms the input observable is false
sched.AdvanceToMs(900);
Assert.False(fixture.CanExecute(1));
Assert.False(latestCanExecute);
Assert.Equal(-1, calculatedResult);
sched.AdvanceToMs(1010);
Assert.True(fixture.CanExecute(1));
Assert.True(latestCanExecute);
Assert.Equal(calculatedResult, 5);
sched.AdvanceToMs(1200);
Assert.False(fixture.CanExecute(1));
Assert.False(latestCanExecute);
});
}
示例4: CommandCanExecute_Test
public void CommandCanExecute_Test()
{
//Given
string result = null;
string expected = Any.Create<string>();
ICommand command = new ReactiveCommand<string>(t => result = t, _ => false);
//When
bool canExecute = command.CanExecute(null);
command.Execute(expected);
//Then
Assert.That(canExecute, Is.False);
Assert.That(result, Is.Null);
}
示例5: Command_Test
public void Command_Test()
{
//Given
string result = null;
string expected = Any.Create<string>();
ICommand command = new ReactiveCommand<string>(t => result = t, _ => true);
//When
bool canExecute = command.CanExecute(null);
command.Execute(expected);
//Then
Assert.That(expected, Is.EqualTo(result));
Assert.That(canExecute, Is.True);
}
示例6: CompletelyDefaultReactiveCommandShouldFire
public void CompletelyDefaultReactiveCommandShouldFire()
{
var sched = new TestScheduler();
var fixture = new ReactiveCommand(null, sched);
Assert.IsTrue(fixture.CanExecute(null));
string result = null;
fixture.Subscribe(x => result = x as string);
fixture.Execute("Test");
sched.Start();
Assert.AreEqual("Test", result);
fixture.Execute("Test2");
sched.Start();
Assert.AreEqual("Test2", result);
}
示例7: FullCommand_Test
public void FullCommand_Test()
{
//Given
const string message = "Reactive";
string result = null;
bool completed = false;
Exception exception = null;
string expected = Any.Create<string>();
var command = new ReactiveCommand<string>(t => result = t, _ => true,ex => exception = ex,() => completed = true );
//When
bool canExecute = command.CanExecute(null);
command.Execute(expected);
command.OnError(new Exception(message));
command.OnCompleted();
//Then
Assert.True(canExecute);
Assert.AreEqual(result, expected);
Assert.That(exception.Message, Is.EqualTo(message));
Assert.True(completed);
}
示例8: MainWindowViewModel
public MainWindowViewModel()
{
ImageHistory = new ReactiveList<string>();
VisiblityCommand = new ReactiveCommand();
DropCommand = new ReactiveCommand();
SettingsCommand = new ReactiveCommand();
UploadCommand = new ReactiveCommand();
ScreenCommand = new ReactiveCommand();
SelectionCommand = new ReactiveCommand(this.WhenAnyValue(x => x.IsCaptureWindowOpen).Select(x => !x));
SelectionCommand.Subscribe(_ => IsCaptureWindowOpen = true);
DropCommand.Subscribe(async ev => {
var e = ev as DragEventArgs;
if (e == null)
return;
if (!e.Data.GetDataPresent(DataFormats.FileDrop))
return;
var data = e.Data.GetData(DataFormats.FileDrop) as IEnumerable<string>;
if (data == null)
return;
foreach (var file in data)
await App.Uploader.Upload(file);
});
OpenCommand = new ReactiveCommand();
OpenCommand.Subscribe(async files => {
foreach (var file in (IEnumerable<string>)files)
await App.Uploader.Upload(file);
});
MessageBus.Current.Listen<object>("CaptureWindow").Subscribe(_ => IsCaptureWindowOpen = false);
Observable.FromEventPattern<KeyPressedEventArgs>(handler => App.HotKeyManager.KeyPressed += handler,
handler => App.HotKeyManager.KeyPressed -= handler).Select(x => x.EventArgs).Subscribe(e => {
var hk = e.HotKey;
if (hk.Equals(App.Settings.ScreenKey))
ScreenCommand.Execute(null);
else if (hk.Equals(App.Settings.SelectionKey)) {
if (SelectionCommand.CanExecute(null))
SelectionCommand.Execute(null);
}
});
Observable.FromEventPattern<UploaderEventArgs>(handler => App.Uploader.ImageUploadSuccess += handler,
handler => App.Uploader.ImageUploadSuccess -= handler).Select(x => x.EventArgs).Subscribe(e => {
if (App.Settings.CopyLinks)
Clipboard.SetDataObject(e.ImageUrl);
ImageHistory.Add(e.ImageUrl);
if (!App.Settings.Notifications)
return;
var msg = string.Format("Image Uploaded: {0}", e.ImageUrl);
MessageBus.Current.SendMessage(new NotificationMessage(Title, msg, BalloonIcon.Info));
});
Observable.FromEventPattern<UploaderEventArgs>(handler => App.Uploader.ImageUploadFailed += handler,
handler => App.Uploader.ImageUploadFailed -= handler).Select(x => x.EventArgs).Subscribe(e => {
if (!App.Settings.Notifications)
return;
var msg = string.Format("Image Failed: {0}", e.Exception.Message);
MessageBus.Current.SendMessage(new NotificationMessage(Title, msg, BalloonIcon.Error));
});
App.Settings.ObservableForProperty(x => x.AlwaysOnTop).Subscribe(_ => this.RaisePropertyChanged("IsTopmost"));
}
示例9: RegisterAsyncFunctionSmokeTest
public void RegisterAsyncFunctionSmokeTest()
{
(new TestScheduler()).With(sched => {
var fixture = new ReactiveCommand();
IReactiveDerivedList<int> results;
results = fixture.RegisterAsync(_ =>
Observable.Return(5).Delay(TimeSpan.FromSeconds(5), sched)).CreateCollection();
var inflightResults = fixture.IsExecuting.CreateCollection();
sched.AdvanceToMs(10);
Assert.True(fixture.CanExecute(null));
fixture.Execute(null);
sched.AdvanceToMs(1005);
Assert.False(fixture.CanExecute(null));
sched.AdvanceToMs(5100);
Assert.True(fixture.CanExecute(null));
new[] {false, true, false}.AssertAreEqual(inflightResults);
new[] {5}.AssertAreEqual(results);
});
}
示例10: MultipleSubscribersShouldntDecrementRefcountBelowZero
public void MultipleSubscribersShouldntDecrementRefcountBelowZero()
{
(new TestScheduler()).With(sched => {
var fixture = new ReactiveCommand();
var results = new List<int>();
bool[] subscribers = new[] {false, false, false, false, false};
var output = fixture.RegisterAsync(_ =>
Observable.Return(5).Delay(TimeSpan.FromMilliseconds(5000), sched));
output.Subscribe(x => results.Add(x));
Enumerable.Range(0, 5).Run(x => output.Subscribe(_ => subscribers[x] = true));
Assert.True(fixture.CanExecute(null));
fixture.Execute(null);
sched.AdvanceToMs(2000);
Assert.False(fixture.CanExecute(null));
sched.AdvanceToMs(6000);
Assert.True(fixture.CanExecute(null));
Assert.True(results.Count == 1);
Assert.True(results[0] == 5);
Assert.True(subscribers.All(x => x));
});
}
示例11: RaisesCanExecuteChangedWithNextCanExecuteImplementation
public void RaisesCanExecuteChangedWithNextCanExecuteImplementation(
Subject<Func<object, bool>> source, object parameter)
{
var sut = new ReactiveCommand<Unit>(
source, _ => Task.FromResult(Unit.Default));
sut.MonitorEvents();
source.OnNext(p => p == parameter);
sut.ShouldRaise(nameof(sut.CanExecuteChanged))
.WithSender(sut).WithArgs<EventArgs>(a => a == EventArgs.Empty);
sut.CanExecute(parameter).Should().BeTrue();
sut.CanExecute(new object()).Should().BeFalse();
}
示例12: TaxonManagementVM
public TaxonManagementVM(
IConnectivityService Connectivity,
ITaxonService Taxa,
IDiversityServiceClient Service,
INotificationService Notification
) {
this.Connectivity = Connectivity;
this.Service = Service;
this.Taxa = Taxa;
this.Notification = Notification;
_IsOnlineAvailable = this.ObservableToProperty(Connectivity.WifiAvailable(), x => x.IsOnlineAvailable);
var localLists =
this.FirstActivation()
.SelectMany(_ =>
Taxa.getTaxonSelections()
.ToObservable(ThreadPoolScheduler.Instance)
.Select(list => new TaxonListVM(list)))
.Publish();
LocalLists =
localLists
.ObserveOnDispatcher()
.CreateCollection();
var onlineLists =
localLists
.IgnoreElements() //only download lists once the local ones are loaded
.Concat(Observable.Return(null as TaxonListVM))
.CombineLatest(this.OnActivation(), (_, _2) => _2)
.CheckConnectivity(Connectivity, Notification)
.SelectMany(_ => {
return Service.GetTaxonLists()
.DisplayProgress(Notification, DiversityResources.TaxonManagement_State_DownloadingLists)
.TakeUntil(this.OnDeactivation());
})
.ObserveOnDispatcher()
.SelectMany(lists =>
lists.Where(list => !LocalLists.Any(loc => loc.Model == list)) // Filter lists already present locally
.Select(list => new TaxonListVM(list))
)
.Publish();
PersonalLists =
onlineLists.Where(vm => !vm.Model.IsPublicList)
.CreateCollection();
PublicLists =
onlineLists.Where(vm => vm.Model.IsPublicList)
.CreateCollection();
onlineLists.Connect();
localLists.Connect();
Select = new ReactiveCommand<TaxonListVM>(vm => !vm.IsSelected && !vm.IsDownloading);
Select.Subscribe(taxonlist => {
foreach (var list in LocalLists) {
if (list.Model.TaxonomicGroup == taxonlist.Model.TaxonomicGroup)
list.Model.IsSelected = false;
}
Taxa.selectTaxonList(taxonlist.Model);
});
Download = new ReactiveCommand<TaxonListVM>(vm => !vm.IsDownloading);
Download
.CheckConnectivity(Connectivity, Notification)
.Subscribe(taxonlist => {
if (Taxa.getTaxonTableFreeCount() > 0) {
CurrentPivot = Pivot.Local;
taxonlist.IsDownloading = true;
makeListLocal(taxonlist);
DownloadTaxonList(taxonlist)
.DisplayProgress(Notification, DiversityResources.TaxonManagement_State_DownloadingList)
.ObserveOnDispatcher()
.ShowServiceErrorNotifications(Notification)
.Subscribe(_ => {
//Download Succeeded
taxonlist.IsDownloading = false;
if (Select.CanExecute(taxonlist))
Select.Execute(taxonlist);
},
_ => //Download Failed
{
taxonlist.IsDownloading = false;
removeLocalList(taxonlist);
},
() =>
{
});
}
});
Delete = new ReactiveCommand<TaxonListVM>(vm => !vm.IsDownloading);
//.........这里部分代码省略.........
示例13: CommandInitialConditionShouldBeAdjustable
public void CommandInitialConditionShouldBeAdjustable()
{
var sub = new Subject<bool>();
var cmd = new ReactiveCommand(sub);
Assert.Equal(true, cmd.CanExecute(null));
var cmd2 = new ReactiveCommand(sub, false);
Assert.Equal(false, cmd2.CanExecute(null));
}