我在 ViewModel 上有一个命令,用于导航到详细信息页面
public class InstalledMetersListViewModel : MvxViewModel
, IMvxServiceConsumer<IInstallMeterRepository>
{
public List<InstalledMeterListItemViewModel> List { get; set; }
public InstalledMetersListViewModel()
{
List = new List<InstalledMeterListItemViewModel>();
foreach (var meter in this.GetService<IInstallMeterRepository>().GetInstalledMeters())
{
List.Add(new InstalledMeterListItemViewModel { Serial = meter.Serial, Description = meter.Description });
}
}
public IMvxCommand ShowDetailsCommand
{
get
{
return new MvxRelayCommand<InstalledMeterListItemViewModel>(type => RequestNavigate<InstalledMeterListItemViewModel>(new {serial = type.Serial.ToString()}));
}
}
}
有了这个视图
<ListBox ItemsSource="{Binding List}" x:Name="TheListBox">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="5,5,5,5" BorderBrush="White">
<Grid Width="auto" HorizontalAlignment="Stretch" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="S/N"/>
<TextBlock Text=" : " Grid.Column="1"/>
<TextBlock Text="{Binding Serial}" Grid.Column="2"/>
<TextBlock Text="Description" Grid.Row="1"/>
<TextBlock Text=" : " Grid.Column="1" Grid.Row="1"/>
<TextBlock Text="{Binding Description}" Grid.Column="2" Grid.Row="1"/>
<Button Content="Details" Grid.Column="3" Grid.RowSpan="2" Command="{Binding Path=DataContext.ShowDetailsCommand, ElementName=TheListBox}" CommandParameter="{Binding}"/>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我导航到的 ViewModel 是
public class InstalledMeterListItemViewModel : MvxViewModel,
IMvxServiceConsumer<IInstallMeterRepository>
{
public InstalledMeterListItemViewModel(string serial)
{
IInstalledMeter meter = this.GetService<IInstallMeterRepository>().GetMeter(Convert.ToDouble(serial));
Description = meter.Description;
Serial = meter.Serial;
}
public InstalledMeterListItemViewModel()
{
}
访问对象时出现问题 - 这很可能是由作为内部生成的匿名对象引起的 - 请参阅匿名类型和获取 WP7.1 上的访问器?
但我不使用任何内部类,所以我看不出如何解决这个问题