2016-12-01 71 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); 
    }