以下代码在问题标题中提供了运行时错误。但是,当我在 MainWindow 构造函数中注释掉 OneWayBind() 调用时,应用程序运行时不会出错。有任何想法吗?
窗户
public partial class MainWindow : Window, IViewFor<MainViewModel>
{
public MainWindow()
{
ViewModel = new MainViewModel();
InitializeComponent();
this.OneWayBind(ViewModel, x => x.ContentItems, x => x.panelDemo.ItemsSource);
}
public MainViewModel ViewModel
{
get { return (MainViewModel)GetValue(ViewModelProperty); }
set { SetValue(ViewModelProperty, value); }
}
public static readonly DependencyProperty ViewModelProperty =
DependencyProperty.Register("ViewModel", typeof(MainViewModel), typeof(MainWindow), new PropertyMetadata(null));
object IViewFor.ViewModel
{
get { return ViewModel; }
set { ViewModel = (MainViewModel)value; }
}
}
班上
public class ContentItem
{
public BitmapImage Image { get; set; }
public string Name { get; set; }
}
视图模型
public class MainViewModel : ReactiveObject
{
string _Directory;
public string Directory
{
get { return _Directory; }
set { this.RaiseAndSetIfChanged(x => x.Directory, value); }
}
bool _IsBusy;
public bool IsBusy
{
get { return _IsBusy; }
set { this.RaiseAndSetIfChanged(x => x.IsBusy, value); }
}
ObservableAsPropertyHelper<IList<ContentItem>> _ContentItems;
public IList<ContentItem> ContentItems
{
get { return _ContentItems.Value; }
}
public MainViewModel()
{
this.WhenAny(x => x.Directory, x => x.Value)
.Do(_ => IsBusy = true)
.Select(GetImages)
.Do(_ => IsBusy = false);
}
IObservable<IList<ContentItem>> GetImages(string directory)
{
return new DirectoryInfo(directory).GetFiles().ToList().ToObservable()
.Select(x => new ContentItem
{
Image = new BitmapImage(new Uri(x.FullName)),
Name = x.Name
}).ToList();
}
}
太感谢了!我唯一希望 ReactiveUI 4.5.0 是它具有当前的 WPF 实现示例!它们都已经过时了六个月或更长时间!