2016-12-01 194 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在我的主视图模型使用:

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

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

我的主视图模型内。但我希望能够使用这些标签在ViewModels之间导航。目前,如果我点击一个标签,那么它会调出页面,但没有关联的ViewModel。

请帮忙!

回答

0

终于设法解决了这个问题。这很简单,我无法在任何地方找到答案。我只需要添加一个bindingcontext到页面的代码隐藏。

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