2016-12-01 1 views
0

J'ai une application Xamarin Forms en utilisant mvvmcross. Là, j'ai la navigation via TabbedPages. Chaque page a un code xaml + derrière et viewmodel.mvvmcross accéder à viewmodel à partir de l'onglet

Code pertinent pour la première page:

<?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> 

Je suis en mesure d'appeler différents viewmodels de boutons dans mon modèle principal de vue à l'aide:

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

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

dans mon principal ViewModel. Mais je veux être en mesure d'utiliser les onglets pour naviguer entre ViewModels. En ce moment si je clique sur un onglet, alors il fait apparaître la page, mais sans le ViewModel associé.

Aidez s'il vous plaît!

Répondre

0

Enfin réussi à résoudre le problème. C'était si simple que je ne pouvais trouver une réponse nulle part. Je devais juste ajouter un bindingcontext au codebehind de la page.

public ServiceListPage() 
    { 
     InitializeComponent(); 
     this.BindingContext = new ViewModels.ServiceListViewModel(Plugin.BLE.CrossBluetoothLE.Current.Adapter, UserDialogs.Instance); 
    }