当前位置: 首页>>代码示例>>C#>>正文


C# ReactiveCommand.ToProperty方法代码示例

本文整理汇总了C#中ReactiveCommand.ToProperty方法的典型用法代码示例。如果您正苦于以下问题:C# ReactiveCommand.ToProperty方法的具体用法?C# ReactiveCommand.ToProperty怎么用?C# ReactiveCommand.ToProperty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ReactiveCommand的用法示例。


在下文中一共展示了ReactiveCommand.ToProperty方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: changes

		/* Creating our UI declaratively
		 * 
		 * The Properties in this ViewModel are related to each other in different 
		 * ways - with other frameworks, it is difficult to describe each relation
		 * succinctly; the code to implement "The UI spinner spins while the search 
		 * is live" usually ends up spread out over several event handlers.
		 *
		 * However, with RxUI, we can describe how properties are related in a very 
		 * organized clear way. Let's describe the workflow of what the user does in
		 * this application, in the order they do it.
		 */

		/*We're going to take a Property and turn it into an Observable here - this
		Observable will yield a value every time the Search term changes(which in
		the XAML, is connected to the TextBox).

		We're going to use the Throttle operator to ignore changes that 
		happen too quickly, since we don't want to issue a search for each 
		key pressed!We then pull the Value of the change, then filter 
		out changes that are identical, as well as strings that are empty.

		Finally, we use RxUI's InvokeCommand operator, which takes the String 
		and calls the Execute method on the ExecuteSearch Command, after
		making sure the Command can be executed via calling CanExecute.*/
		public AppViewModel()
		{

			ExecuteSearch = ReactiveCommand.CreateAsyncTask(parameter => GetSearchResultsFromFlickr(this.SearchTerm));

			this.WhenAnyValue(x => x.SearchTerm)
				.Throttle(TimeSpan.FromMilliseconds(800), RxApp.MainThreadScheduler)
				.Select(x => x?.Trim())
				.DistinctUntilChanged()
				.Where(x => !String.IsNullOrWhiteSpace(x))
				.InvokeCommand(ExecuteSearch);

			// How would we describe when to show the spinner in English? We 
			// might say something like, "The spinner's visibility is whether
			// the search is running". RxUI lets us write these kinds of 
			// statements in code.
			//
			// ExecuteSearch has an IObservable<bool> called IsExecuting that
			// fires every time the command changes execution state. We Select() that into
			// a Visibility then we will use RxUI's
			// ToProperty operator, which is a helper to create an 
			// ObservableAsPropertyHelper object.

			_spinnerVisibility = ExecuteSearch.IsExecuting
				.Select(x => x ? Visibility.Visible : Visibility.Collapsed)
				.ToProperty(this, x => x.SpinnerVisibility, Visibility.Hidden);

			// We subscribe to the "ThrownExceptions" property of our ReactiveCommand,
			// where ReactiveUI pipes any exceptions that are thrown in 
			// "GetSearchResultsFromFlickr" into. See the "Error Handling" section
			// for more information about this.
			ExecuteSearch.ThrownExceptions.Subscribe(ex =>
			{
				/* Handle errors here */
				Debug.WriteLine("There was an error");
			});

			
			// Here, we're going to actually describe what happens when the Command
			// gets invoked - we're going to run the GetSearchResultsFromFlickr every
			// time the Command is executed. 
			//
			// The important bit here is the return value - an Observable. We're going
			// to end up here with a Stream of FlickrPhoto Lists: every time someone 
			// calls Execute, we eventually end up with a new list which we then 
			// immediately put into the SearchResults property, that will then 
			// automatically fire INotifyPropertyChanged.
			_SearchResults = ExecuteSearch.ToProperty(this, x => x.SearchResults, new List<FlickrPhoto>());
		}
开发者ID:ajonno,项目名称:ReactiveUI,代码行数:73,代码来源:AppViewModel.cs


注:本文中的ReactiveCommand.ToProperty方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。