我在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()
{
}
访问对象时出现问题 - 很可能是因为匿名对象被生成为内部 - 请参阅Anonymous types and Get accessors on WP7.1?
但是我不使用任何内部类,所以我不知道如何解决这个问题
答案 0 :(得分:1)
这一行:
return new MvxRelayCommand<InstalledMeterListItemViewModel>(
vm => RequestNavigate<InstalledMeterListItemViewModel>(
new {serial = type.Serial.ToString()}));
创建匿名类的实例
该匿名类是由编译器生成的(使用像Reflector这样的工具来查看它)
编译器将其生成为internal
即。编译器创建一个类似
的类 internal class Anonymous_Mangled_Name_1223345tHER
{
public string serial { get; set; }
}
然后将代码重写为:
return new MvxRelayCommand<InstalledMeterListItemViewModel>(
vm => RequestNavigate<InstalledMeterListItemViewModel>(
new Anonymous_Mangled_Name_1223345tHER
{
serial = type.Serial.ToString()
}));
为了反映和使用这个类,Cirrious.MvvmCross中的代码需要访问它...
这就是您需要InternalsVisibleTo
- http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx
有关要添加的行的示例,请参阅Conference-AssemblyInfo.cs的最后一行。
[assembly: InternalsVisibleTo("Cirrious.MvvmCross")]
注意:对于旧的“主”分支,这是:
[assembly: InternalsVisibleTo("Cirrious.MvvmCross.WindowsPhone")]
// should really also add Cirrious.MvvmCross.Touch - but mono runtime doesn't care
// should really also add Cirrious.MvvmCross.Droid - but mono runtime doesn't care