Caliburn.Micro 找不到 CustomerViewModel 的视图。这里有什么问题?
Caliburn.Micro can't find view for CustomerViewModel. What is wrong here?
我正在使用 Caliburn.Micro 尝试将 ListBox 中的项目绑定到两个视图之一,但只有一个视图模型。我能够在 ListBox 中显示项目,但是当任何项目被 selected 时,我得到 'Cannot find view for CustomerViewModel.'
以下是此应用程序的相关部分:
AppBootstrapper:
public class AppBootstrapper : BootstrapperBase
{
public AppBootstrapper()
: base()
{
Initialize();
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
base.DisplayRootViewFor<CustomerWorkspaceViewModel>();
}
}
在我的 Views/Customers 文件夹中,我有一个 CustomerViewModel:
public class CustomerViewModel : Screen
{
private string name;
public string Name
{
get { return name; }
set
{
name = value;
NotifyOfPropertyChange(() => Name);
}
}
}
和一个 CustomerWorkspaceViewModel:
public class CustomerWorkspaceViewModel : DocumentWorkspace<CustomerViewModel>
{
private CustomerViewModel selectedItem;
public CustomerViewModel SelectedItem
{
get { return selectedItem; }
set
{
selectedItem = value;
NotifyOfPropertyChange(() => SelectedItem);
}
}
public CustomerWorkspaceViewModel()
{
Items.AddRange(
new ObservableCollection<CustomerViewModel>
{
new CustomerViewModel {Name = "Customer 1" },
new CustomerViewModel {Name = "Customer 2" },
new CustomerViewModel {Name = "Customer 3" }
});
}
}
我的 Views/Customers 文件夹中有四个视图:
在 Views/Customers/CustomerWorkspace 中,我有编辑视图和读取视图:
Edit.xaml:
<Grid>
<StackPanel>
<Label Content="Edit View"/>
<TextBlock Foreground="White"
FontSize="20"
Text="{Binding Name}" />
</StackPanel>
</Grid>
和Read.xaml:
<Grid>
<TextBlock Foreground="White"
FontSize="20"
Text="{Binding Name}" />
<Label Content="Read view"/>
</Grid>
最后,我在 Views/Customers 中有一个空的 CustomerView 用户控件,在 Views/Customers 中有一个 CustomerWorkspaceView:
<Grid>
<ListBox x:Name="Items"
Margin="5"
SelectedItem="{Binding SelectedItem}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" Margin="5" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ContentControl cal:View.Context="{Binding State, Mode=TwoWay}" cal:View.Model="{Binding SelectedItem}"/>
</Grid>
最后我在根文件夹中有了 DocumentWorkspace 和 AppBootstrapper:
public abstract class DocumentWorkspace<TDocument> : Conductor<TDocument>.Collection.OneActive
where TDocument : class, INotifyPropertyChanged, IDeactivate, IHaveDisplayName
{
public enum DocumentWorkspaceState
{
Read,
Edit
}
DocumentWorkspaceState state = DocumentWorkspaceState.Read;
public DocumentWorkspaceState State
{
get { return state; }
set
{
if (state == value)
return;
state = value;
NotifyOfPropertyChange(() => State);
}
}
}
我希望(和期待)的是 select 列表框中的一个项目,它由 DocumentWorkspace 对象组成,是导体,从一个视图(编辑)切换到另一个视图(读取) ). select 正在工作,SelectedItem setter 被触发,DocumentWorkspace 中的状态设置正确。但是 Caliburn.Micro 似乎无法找到作为 SelectedItem 的结果 CustomerViewModel 的视图。我真的试图在这个 post 中只包含重现问题所需的内容。
请注意我正在尝试执行的操作的文档遵循
处的 discussion
所以 mvermef 为我指明了正确的方向。我需要修复我的命名空间。我还需要在 "Views" 下创建一个名为 "Customers" 的文件夹,并将编辑和读取视图移至其中。然后我需要再次修复我的命名空间。顺便说一句,Resharper 是一个很好的工具。
那么除了命名空间之外还有什么问题?在 CustomerWorkspaceView
中,我有
<ContentControl cal:View.Context="{Binding State, Mode=TwoWay}" cal:View.Model="{Binding SelectedItem}"/>
这导致 Caliburn.Micro 在 CustomerWorkspaceViewModel
中寻找状态 属性(实际上是在基础 class DocumentWorkspace
中),在这种情况下结果是 "Read"。这里只需要一个字符串。有一个 "Read" 视图,但它位于名为 "CustomerWorkspace" 的文件夹中。它需要位于名为 Customer
的文件夹中,因为 SelectedItem 属性 的类型为 CustomerViewModel
.
原来我什至不需要空的 CustomerView。只要 Caliburn.Micro 可以找到与您的 ViewModel 类型同名的文件夹减去 "ViewModel" 并且名称空间与文件夹结构相匹配,它应该可以找到您的视图。
这是我的完整文件夹结构:
我正在使用 Caliburn.Micro 尝试将 ListBox 中的项目绑定到两个视图之一,但只有一个视图模型。我能够在 ListBox 中显示项目,但是当任何项目被 selected 时,我得到 'Cannot find view for CustomerViewModel.'
以下是此应用程序的相关部分:
AppBootstrapper:
public class AppBootstrapper : BootstrapperBase
{
public AppBootstrapper()
: base()
{
Initialize();
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
base.DisplayRootViewFor<CustomerWorkspaceViewModel>();
}
}
在我的 Views/Customers 文件夹中,我有一个 CustomerViewModel:
public class CustomerViewModel : Screen
{
private string name;
public string Name
{
get { return name; }
set
{
name = value;
NotifyOfPropertyChange(() => Name);
}
}
}
和一个 CustomerWorkspaceViewModel:
public class CustomerWorkspaceViewModel : DocumentWorkspace<CustomerViewModel>
{
private CustomerViewModel selectedItem;
public CustomerViewModel SelectedItem
{
get { return selectedItem; }
set
{
selectedItem = value;
NotifyOfPropertyChange(() => SelectedItem);
}
}
public CustomerWorkspaceViewModel()
{
Items.AddRange(
new ObservableCollection<CustomerViewModel>
{
new CustomerViewModel {Name = "Customer 1" },
new CustomerViewModel {Name = "Customer 2" },
new CustomerViewModel {Name = "Customer 3" }
});
}
}
我的 Views/Customers 文件夹中有四个视图: 在 Views/Customers/CustomerWorkspace 中,我有编辑视图和读取视图:
Edit.xaml:
<Grid>
<StackPanel>
<Label Content="Edit View"/>
<TextBlock Foreground="White"
FontSize="20"
Text="{Binding Name}" />
</StackPanel>
</Grid>
和Read.xaml:
<Grid>
<TextBlock Foreground="White"
FontSize="20"
Text="{Binding Name}" />
<Label Content="Read view"/>
</Grid>
最后,我在 Views/Customers 中有一个空的 CustomerView 用户控件,在 Views/Customers 中有一个 CustomerWorkspaceView:
<Grid>
<ListBox x:Name="Items"
Margin="5"
SelectedItem="{Binding SelectedItem}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" Margin="5" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ContentControl cal:View.Context="{Binding State, Mode=TwoWay}" cal:View.Model="{Binding SelectedItem}"/>
</Grid>
最后我在根文件夹中有了 DocumentWorkspace 和 AppBootstrapper:
public abstract class DocumentWorkspace<TDocument> : Conductor<TDocument>.Collection.OneActive
where TDocument : class, INotifyPropertyChanged, IDeactivate, IHaveDisplayName
{
public enum DocumentWorkspaceState
{
Read,
Edit
}
DocumentWorkspaceState state = DocumentWorkspaceState.Read;
public DocumentWorkspaceState State
{
get { return state; }
set
{
if (state == value)
return;
state = value;
NotifyOfPropertyChange(() => State);
}
}
}
我希望(和期待)的是 select 列表框中的一个项目,它由 DocumentWorkspace 对象组成,是导体,从一个视图(编辑)切换到另一个视图(读取) ). select 正在工作,SelectedItem setter 被触发,DocumentWorkspace 中的状态设置正确。但是 Caliburn.Micro 似乎无法找到作为 SelectedItem 的结果 CustomerViewModel 的视图。我真的试图在这个 post 中只包含重现问题所需的内容。
请注意我正在尝试执行的操作的文档遵循
处的 discussion所以 mvermef 为我指明了正确的方向。我需要修复我的命名空间。我还需要在 "Views" 下创建一个名为 "Customers" 的文件夹,并将编辑和读取视图移至其中。然后我需要再次修复我的命名空间。顺便说一句,Resharper 是一个很好的工具。
那么除了命名空间之外还有什么问题?在 CustomerWorkspaceView
中,我有
<ContentControl cal:View.Context="{Binding State, Mode=TwoWay}" cal:View.Model="{Binding SelectedItem}"/>
这导致 Caliburn.Micro 在 CustomerWorkspaceViewModel
中寻找状态 属性(实际上是在基础 class DocumentWorkspace
中),在这种情况下结果是 "Read"。这里只需要一个字符串。有一个 "Read" 视图,但它位于名为 "CustomerWorkspace" 的文件夹中。它需要位于名为 Customer
的文件夹中,因为 SelectedItem 属性 的类型为 CustomerViewModel
.
原来我什至不需要空的 CustomerView。只要 Caliburn.Micro 可以找到与您的 ViewModel 类型同名的文件夹减去 "ViewModel" 并且名称空间与文件夹结构相匹配,它应该可以找到您的视图。
这是我的完整文件夹结构: