我正在关注Brian Lagunas 的教程并有一个问题。
我们有一个使用 PRISM 6 和 Unity 引导程序的小型应用程序设置。我想使用ViewModelLocator
将 a 绑定ViewModel
到我的视图。这适用于“基本模块”(带有 Shell 和引导程序的模块),但在其他模块中使用它时似乎存在一些问题。
您可以在下面找到View
(XAML 和代码隐藏)和ViewModel
. 如果我DataContext
在后面的代码中手动设置绑定,则绑定有效。在调试代码时,我发现ViewModel
该类从未实例化,所以我猜VML
由于配置错误而无法找到 VM。但据我所知,命名约定已得到满足。
你能找到问题或知道我忘记了什么配置吗?
查看(后面的代码):
namespace CommunicationModule.Views
{
public partial class CommunicationView : UserControl, IView
{
public CommunicationView()
{
InitializeComponent();
}
}
}
查看 XAML:
<UserControl
x:Class="CommunicationModule.Views.CommunicationView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:View="clr-namespace:CommunicationModule.Views"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True">
<Grid DataContext="{Binding ModelList, UpdateSourceTrigger=PropertyChanged}" Width="320" Height="300">
<Grid.Resources>
<DataTemplate x:Key="DataTemplate">
<Border Name="border" BorderBrush="DarkSlateBlue" BorderThickness="2"
CornerRadius="2" Padding="5" Margin="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition MaxWidth="30"/>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Grid.Column="0" Source="{Binding Path=IconUri}" HorizontalAlignment="Left" />
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Title}" FontSize="12" FontWeight="Bold" />
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=ContentShort}"/>
</Grid>
</Border>
</DataTemplate>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!-- UI -->
<DockPanel Grid.ColumnSpan="2" Margin="0,0,10,0">
<!-- Title -->
<Label DockPanel.Dock="Top" FontSize="18" Margin="5" Content="Wichtige Meldungen"/>
<!-- Data template is specified by the ItemTemplate attribute -->
<ScrollViewer>
<ListBox Name="listBox"
SelectionMode="Single"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource DataTemplate}"
HorizontalContentAlignment="Stretch"
IsSynchronizedWithCurrentItem="True"
Margin="5,0,5,5" Width="280"/>
</ScrollViewer>
</DockPanel>
</Grid>
</UserControl>
视图模型:
namespace CommunicationModule.ViewModels
{
public class CommunicationViewViewModel : BindableBase
{
private List<CommunicationModel> _modelList = (new CommunicationModelBO()).getCommunicationItems(); //= new List<CommunicationModel>();
private readonly IRegionManager _regionManager;
public List<CommunicationModel> ModelList
{
get { return _modelList; }
set { SetProperty<List<CommunicationModel>>(ref _modelList, value); }
}
public CommunicationViewViewModel(IRegionManager regionManager)
{
_regionManager = regionManager;
}
}
}