C# WPF上6.5.0版本的ReactiveUI中的简单反应列表绑定失败

C# WPF上6.5.0版本的ReactiveUI中的简单反应列表绑定失败,c#,wpf,reactiveui,C#,Wpf,Reactiveui,在尝试使用最新版本的反应式UI创建简单示例时,我遇到了一个奇怪的错误 窗口打开,我得到一个系统错误 找不到“嗨,鲍勃!”的视图 注意:“嗨,鲍勃!”是列表中的第一项 我错过了什么 谢谢 版本 反应UI 6.5.0 Splat 1.6.2 .net 4.5 示例代码 xaml 代码 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用System.Windows; 使用S

在尝试使用最新版本的反应式UI创建简单示例时,我遇到了一个奇怪的错误

窗口打开,我得到一个系统错误

找不到“嗨,鲍勃!”的视图

注意:“嗨,鲍勃!”是列表中的第一项

我错过了什么

谢谢

版本 反应UI 6.5.0 Splat 1.6.2 .net 4.5

示例代码

xaml


代码

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用ReactiveUI;
名称空间列表绑定
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口,IViewFor
{
公共主窗口()
{
ViewModel=新的ViewModel();
DataContext=ViewModel;
初始化组件();
这个.OneWayBind(ViewModel,m=>m.Items,v=>v.ListBox1.ItemsSource);
}
公共视图模型视图模型
{
获取{return(ViewModel)GetValue(ViewModelProperty);}
set{SetValue(ViewModelProperty,value);}
}
公共静态只读从属属性ViewModelProperty=
DependencyProperty.Register(“ViewModel”、typeof(ViewModel)、typeof(MainWindow)、new PropertyMetadata(null));
对象IViewFor.ViewModel
{
获取{return ViewModel;}
设置{ViewModel=(ViewModel)值;}
}
}
公共类ViewModel:反应对象
{
public ReactiveList Items=new ReactiveList(new[]{“Hi Bob!”,“Two”,“Three”});
}
}

当您使用
OneWayBind
方法绑定到
ListBox
之类的东西时,
ReactiveUI
的问题是,它将尝试根据使用
Splat.Locator.Resolve>找到的视图自动为数据应用自定义模板。在您的例子中,它试图基于“Hi Bob!”ViewModel(显然不存在)查找并构建一个视图

您应该做的是强制它使用自定义数据模板,这样它就不会尝试应用不存在的模板。使用下面的模板,它不应该尝试为您解析视图,而是将“Hi Bob!”值粘贴到
TextBlock

<ListBox x:Name="ListBox1">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>


ReactiveUI仍然忽略这一点的可能性很小(我现在无法验证),因此如果是这种情况,请用传统的
ItemSource={binding Data}

替换
OneWayBind
绑定,我只是在学习ReactiveUI的基础知识,因此我的观点当然是有偏见的。话虽如此,对于这样一个基本的应用程序来说,这似乎是一个巨大的杀伤力。这不是更好地由反应式UI处理吗?为listView创建一个基本绑定从3个字符到多行。。。不必要的xaml+代码隐藏
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ReactiveUI;

namespace ListBind
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, IViewFor<ViewModel>
    {
        public MainWindow()
        {
            ViewModel = new ViewModel();
            DataContext = ViewModel;
            InitializeComponent();
            this.OneWayBind(ViewModel, m => m.Items, v => v.ListBox1.ItemsSource);
        }
        public ViewModel ViewModel
        {
            get { return (ViewModel)GetValue(ViewModelProperty); }
            set { SetValue(ViewModelProperty, value); }
        }

        public static readonly DependencyProperty ViewModelProperty =
            DependencyProperty.Register("ViewModel", typeof(ViewModel), typeof(MainWindow), new PropertyMetadata(null));

        object IViewFor.ViewModel
        {
            get { return ViewModel; }
            set { ViewModel = (ViewModel)value; }
        }
    }
    public class ViewModel : ReactiveObject
    {
        public ReactiveList<string> Items = new ReactiveList<string>(new[] { "Hi Bob!", "Two", "Three" });
    }
}
<ListBox x:Name="ListBox1">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>