以下代码提供问题标题中的运行时错误。但是,当我在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; }
}
ViewModel
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实现样本!他们都是六个月或更久了!