2016-12-01 10 views
0

私はmvvmcrossを使ってXamarin Formsアプリケーションを持っています。そこに私はTabbedPages経由のナビゲーションがあります。各ページにはxaml +のコードとviewmodelがあります。最初のページのmvvmcrossタブモードからviewmodelにナビゲート

関連するコード:

<?xml version="1.0" encoding="utf-8" ?> 
<pages:BaseTabbedPage xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     xmlns:pages="clr-namespace:ABBI_XPlat_3.Pages;assembly=ABBI_XPlat_3" 
     x:Class="ABBI_XPlat_3.Pages.DeviceListPage" 
     Title="Discover devices" 
     x:Name="DevicePage"> 
<pages:BaseTabbedPage.Resources> 
    <ResourceDictionary> 
    <DataTemplate x:Key="DeviceItemTemplate"> ... </DataTemplate> 
    </ResourceDictionary> 
</pages:BaseTabbedPage.Resources> 
<pages:BaseTabbedPage.Children> 
    <pages:BasePage Title="Scan for devices"> 
    <Grid> 
     <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"></RowDefinition> 
     <RowDefinition Height="*"></RowDefinition> 
     <RowDefinition Height="Auto"></RowDefinition> 
     </Grid.RowDefinitions> 
     <StackLayout BackgroundColor="#FF6969" Padding="10" IsVisible="{Binding IsStateOn, Converter={StaticResource InverseBoolean}}"> 
     <Label Text="{Binding StateText}" FontSize="18" HorizontalTextAlignment="Center"></Label> 
     </StackLayout> 

     <ListView Grid.Row="1" ItemsSource="{Binding Devices}" SelectedItem="{Binding SelectedDevice, Mode=TwoWay}" 
      IsPullToRefreshEnabled="True" 
      RefreshCommand="{Binding RefreshCommand}" 
      IsRefreshing="{Binding IsRefreshing, Mode=OneWay}" 
      RowHeight="80" 
      ItemTemplate="{StaticResource DeviceItemTemplate}"> 
     </ListView> 

     <StackLayout Grid.Row="2" Orientation="Horizontal"> 
     <Button Text="Connect" Command="{Binding ConnectToSelectedCommand}" HorizontalOptions="FillAndExpand"/> 
     <Button Text="Stop Scan" Command="{Binding StopScanCommand}" HorizontalOptions="End"/> 
     <ActivityIndicator IsRunning="{Binding IsRefreshing}" 
         HeightRequest="24" 
         WidthRequest="24" 
         VerticalOptions="Center" 
         HorizontalOptions="End"/> 
     </StackLayout> 

    </Grid> 
    </pages:BasePage> 
    <pages:ServiceListPage Title="Services"/> 
    <pages:OtherTabbedPage Title="Services"/> 
</pages:BaseTabbedPage.Children> 
</pages:BaseTabbedPage> 

は、私が使用して私のメインビューモデルのボタンと異なるのviewmodelsを呼び出すことができる午前:私のメインのViewModel内

public MvxCommand ConnectToSelectedCommand => new MvxCommand(ConnectToSelectedDeviceAsync, CanDisplayServices); 

    private async void ConnectToSelectedDeviceAsync() 
    { 
     ShowViewModel<ServiceListViewModel>(new MvxBundle(new Dictionary<string, string> { { DeviceIdKey, SystemDevices.FirstOrDefault().Id.ToString() } })); 
    } 

。しかし、私はViewModel間をナビゲートするためにタブを使用できるようにしたい。現時点では、タブをクリックすると、ページが表示されますが、関連するViewModelはありません。

お願いします!

答えて

0

最終的に問題を解決することができました。私はどこでも答えを見つけることができなかったのでとてもシンプルでした。私はちょうどバインディングコンテキストをページのコードビヘイビアに追加しなければならなかった。

public ServiceListPage() 
    { 
     InitializeComponent(); 
     this.BindingContext = new ViewModels.ServiceListViewModel(Plugin.BLE.CrossBluetoothLE.Current.Adapter, UserDialogs.Instance); 
    } 
関連する問題