PRISM(Unity)中的ViewModelLocator在模块中不起作用

时间:2016-04-21 07:09:13

标签: c# wpf mvvm viewmodellocator

我正在关注Brian Lagunas's tutorial并提出问题。

我们有一个小型应用程序设置与PRISM 6和Unity bootstrapper。我想使用ViewModelLocatorViewModel绑定到我的视图中。这适用于“基本模块”(Shell和bootstrapper),但在其他模块中使用它时似乎存在一些问题。

您可以在下面找到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;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您不能让用户控件的名称以View结尾,否则,AutoWireViewModel将不起作用。因此,请将您的usercontrols名称更改为Communication,并将viewmodels名称更改为CommunicationViewModel