1

詳細ページに移動するために使用する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 の匿名型と Get アクセサーを参照してください。

しかし、内部クラスを使用していないため、これを修正する方法がわかりません

4

1 に答える 1

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()
              }));

このクラスを反映して使用するには、Cirrios.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 
于 2012-11-22T13:24:06.113 に答える