2017-12-08 12 views
0

私はxamarinフォームでmvvmを使用しようとしていますが、まだ各ページのナビゲーションと混乱しています。私はXamarinフォーム内のすべてのナビゲーションを処理するために既にインターフェイスとNavigationServiceを作成します。エラーコード行は、butonをクリックし、他のページに移動しようとすると、常にクラッシュします。ここで私はすでに変更をしようと私のコード マイインタフェースMVVMナビゲーション強制閉じるXamarinフォーム

namespace KGVC.Interfaces 
{ 
    public interface INavigationService 
    { 
     void NavigateToDashboard(); 
     void NavigateToLogout(); 
     void NavigateBack(); 
     void gotoNews(); 
     void goEvent(); 
     void gotoKGCash(); 
     void gotoCardCommnunity(); 
     void gotoSetting(); 
     void gotoNearbyLocation(); 
    } 
} 

私のNavigationService

namespace KGVC.Services 
{ 
    public class NavigationService : INavigationService 
    { 
     public void goEvent() 
     { 
      var currentPage = GetCurrentPage(); 

      currentPage.Navigation.PushAsync(new EventPage()); 
     } 

     public void gotoCardCommnunity() 
     { 
      var currentPage = GetCurrentPage(); 

      currentPage.Navigation.PushAsync(new CardCommunityPage()); 
     } 

     public void gotoKGCash() 
     { 
      var currentPage = GetCurrentPage(); 

      currentPage.Navigation.PushAsync(new KGCashPage()); 
     } 

     public void gotoNearbyLocation() 
     { 
      var currentPage = GetCurrentPage(); 

      currentPage.Navigation.PushAsync(new StoreMaps()); 
     } 

     public void gotoNews() 
     { 
      var currentPage = GetCurrentPage(); 

      currentPage.Navigation.PushAsync(new RssFeedView()); 
     } 

     public void gotoSetting() 
     { 
      var currentPage = GetCurrentPage(); 

      currentPage.Navigation.PushAsync(new SettingPages()); 
     } 

     public void NavigateBack() 
     { 
      throw new NotImplementedException(); 
     } 

     public void NavigateToDashboard() 
     { 

      var currentPage = GetCurrentPage(); 

      Application.Current.MainPage = new MainPage(); 
     } 

     public void NavigateToLogout() 
     { 
      var currentPage = GetCurrentPage(); 
      Application.Current.MainPage = new NewPageLogin(); 
     } 

     private Page GetCurrentPage() 
     { 
      var currentPage = Application.Current.MainPage.Navigation.NavigationStack.LastOrDefault(); 

      return currentPage; 
     } 
    } 
} 

私の見解モデル

public class GridMenuViewModel 
    { 


     public ICommand gotoNews { get; private set; } 
     public ICommand goEvent { get; private set; } 
     public ICommand gotoKGCash { get; private set; } 
     public ICommand gotoSetting { get; private set; } 
     public ICommand gotoNearbyLocation { get; private set; } 
     public GridMenuViewModel() 
     { 
      gotoNews = new Command(() => 
      { 
       var navigationService = new NavigationService(); 

       navigationService.gotoNews(); 
      }); 
      goEvent = new Command(() => 
      { 
       var navigationService = new NavigationService(); 

       navigationService.goEvent(); 
      }); 

      gotoKGCash = new Command(() => 
      { 
       var navigationService = new NavigationService(); 

       navigationService.gotoKGCash(); 
      }); 
      gotoSetting = new Command(() => 
      { 
       var navigationService = new NavigationService(); 

       navigationService.gotoSetting(); 
      }); 
      gotoNearbyLocation = new Command(() => 
      { 
       var navigationService = new NavigationService(); 

       navigationService.gotoNearbyLocation(); 
      }); 
     } 
    } 

そして、私のビュー

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      NavigationPage.HasNavigationBar="True" 
      NavigationPage.BackButtonTitle="False" 
       BindingContext="{Binding GridMenuViewModel, Source={StaticResource Locator}}" 
      xmlns:control="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView" 
      xmlns:local="clr-namespace:KGVC.Views.MainPages" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="KGVC.Views.MainPages.GridMenu" 
      Title="HOME" 
      > 


    <ContentPage.Content> 
     <StackLayout > 
      <StackLayout BackgroundColor="##CEB053"> 
       <StackLayout HeightRequest="35" BackgroundColor="##CEB053" Orientation="Horizontal"> 
        <Label FontAttributes="Italic" TextColor="Black" Margin="9" Text="Hello," FontSize="15"/> 
        <Label TextColor="Black" Margin="9" Text="John Doe" FontSize="15" FontAttributes="Bold"/> 

       </StackLayout> 

       <StackLayout Padding="0" HeightRequest="30" BackgroundColor="#E3E6E3" Orientation="Horizontal"> 
        <Image Margin="5" Source="ic_logo.png"/> 
        <Label Text="Points" Margin="5" FontSize="13" TextColor="Black"/> 
       </StackLayout> 

      </StackLayout> 


      <StackLayout HeightRequest="220" VerticalOptions="StartAndExpand"> 

       <control:CarouselView HeightRequest="185" ItemsSource="{Binding MyDataSource}" Position="{Binding Position, Mode=TwoWay}"> 
        <control:CarouselView.ItemTemplate> 
         <DataTemplate> 
          <Image Source="{Binding Image}"/> 

         </DataTemplate> 
        </control:CarouselView.ItemTemplate> 
       </control:CarouselView> 
       <local:CarouselIndicators IndicatorHeight="9" IndicatorWidth="9" UnselectedIndicator="unselected_circle.png" SelectedIndicator="selected_circle.png" Position="{Binding Position}" ItemsSource="{Binding MyDataSource}" /> 
      </StackLayout> 

      <ScrollView IsClippedToBounds="True" VerticalOptions="StartAndExpand" Orientation="Vertical" > 

        <Grid x:Name="controlGrid" VerticalOptions="StartAndExpand" HeightRequest="370" Margin="15"> 

         <Grid.RowDefinitions> 
          <RowDefinition Height="0" /> 
          <RowDefinition Height="100" /> 
          <RowDefinition Height="100" /> 
          <RowDefinition Height="100" /> 

         </Grid.RowDefinitions> 
         <Grid.ColumnDefinitions> 

          <ColumnDefinition Width="110" /> 
          <ColumnDefinition Width="110" /> 
          <ColumnDefinition Width="110" /> 

         </Grid.ColumnDefinitions> 

         <StackLayout Orientation="Vertical" Grid.Row="1" Grid.Column="0" > 
          <Button Image="ic_account.png" Margin="5" 
     Style="{StaticResource plainButton}" /> 
          <Label FontSize="12" Text="MY ACCOUNT" HorizontalOptions="Center"/> 
         </StackLayout> 

         <StackLayout Orientation="Vertical" Grid.Row="1" Grid.Column="1"> 
         <Button Margin="5" Command="{Binding gotoCardCommunity}" Image="ic_card.png" 
     Style="{StaticResource plainButton}" /> 
          <Label FontSize="12" Text="CARD" HorizontalOptions="Center"/> 
         </StackLayout> 

         <StackLayout Orientation="Vertical" Grid.Row="1" Grid.Column="2"> 
         <Button Margin="5" Command="{Binding goEvent}" Image="ic_event" 
     Style="{StaticResource plainButton}" /> 
          <Label FontSize="12" Text="PROMO" HorizontalOptions="Center"/> 
         </StackLayout> 

         <StackLayout Grid.Row="2" Grid.Column="0" Orientation="Vertical"> 
         <Button Margin="5" Command="{Binding gotoNearbyLocation}" Image="ic_store" 
     Style="{StaticResource plainButton}" /> 
          <Label FontSize="12" Text="STORE LOCATIONS" HorizontalOptions="Center"/> 
         </StackLayout> 

         <StackLayout Orientation="Vertical" Grid.Row="2" Grid.Column="1"> 
         <Button Margin="5" Command="{Binding gotoNews}" Image="ic_news" 
     Style="{StaticResource plainButton}" /> 
          <Label FontSize="12" Text="NEWS" HorizontalOptions="Center"/> 

         </StackLayout> 


         <StackLayout Grid.Row="2" Grid.Column="2" Orientation="Vertical"> 
         <Button Margin="5" Command="{Binding gotoKGCash}" Image="ic_kgcash" 
     Style="{StaticResource plainButton}" /> 
          <Label FontSize="12" Text="E-WALLET" HorizontalOptions="Center"/> 
         </StackLayout> 

         <StackLayout Orientation="Vertical" Grid.Row="3" Grid.Column="0"> 
          <Button Margin="5" Image="ic_ecommerce" 
     Style="{StaticResource plainButton}" /> 
          <Label FontSize="12" Text="E-COMMERCE" HorizontalOptions="Center"/> 
         </StackLayout> 

         <StackLayout Orientation="Vertical" Grid.Row="3" Grid.Column="1"> 
         <Button Margin="5" Command="{Binding gotoSetting}" Image="ic_pointsummary.png" 
     Style="{StaticResource plainButton}" /> 
          <Label FontSize="12" Text="POINT SUMMARY" HorizontalOptions="Center"/> 
         </StackLayout> 


         <StackLayout Orientation="Vertical" Grid.Row="3" Grid.Column="2"> 
         <Button Margin="5" Command="{Binding gotoSetting}" Image="ic_setting" 
     Style="{StaticResource plainButton}" /> 
          <Label FontSize="12" Text="SETTINGS" HorizontalOptions="Center"/> 
         </StackLayout> 



        </Grid> 






      </ScrollView> 



     </StackLayout> 


    </ContentPage.Content> 
</ContentPage> 

の一部でありますナビゲーションサービスは、pushmodalasyncを使用して、非同期ナビゲーションを作るが、まだクラッシュ、誰でも私を助けることができますか?私はプリズム、mvvmlightと他のサードパーティのmvvm nugetを使用していない

答えて

1

これはあなたのMainPageによって異なります。 GetCurrentPage()が実際に何かを返すかどうか確認しましたか?

GetCurrentPage()に変更してください:私は発見

Page GetCurrentPage() => 
    Application.Current.MainPage.Navigation?.NavigationStack?.LastOrDefault() ?? 
    Application.Current.MainPage; 

もう一つの問題:
1. C# naming conventionsは、Javaとは異なります。メソッド名は大文字で始まる必要があります。現在、あなたは純粋なミックスを持っています。
2. BindingContextをXAMLで初期化すると、bugのためにViewModelが2回作成されます。それを次のように変更してください。

<ContentPage 
    xmlns:vm="your-namespace.vm" 
    ...> 
    <ContentPage.BindingContext> 
    <vm:GridMenuViewModel /> 
    </ContentPage.BindingContext> 
</ContentPage> 

3.各コマンドのNavigationServiceを再作成する必要はありません。 ViewModelで再利用してください。

<StackLayout BackgroundColor="##CEB053"> 

が進コードを修正:
4.あなたは、いくつかの壊れたXAMLを持っています。一般的に言えば、XAMLCを有効にして、コードを作成する際にXAMLエラーについて通知を受けることができます。
5. PushAsyncを使用するには、MainPageNavigationPageで囲んでください。
6.あなたは、非同期コードの実行を待つ必要があります。上記のリストの上に

public async Task GoToCardCommnunity() 
{ 
    var currentPage = GetCurrentPage(); 
    await currentPage.Navigation.PushAsync(new CardCommunityPage()); 
} 

チェックをし、アプリケーションの出力と例外に注意を払います。

+0

GetCurrentPageメソッドを使用して、PushAsyncをPushModalAsyncとその仕事に変更しても、すでに動作していますが、私がPushAsyncを使用しようとすると動作しません。私のメインページでは、ログイン後に私のユーザが再びログインページに戻ることができないためです。それはプッシュ非同期を使用して私はなぜカントですか? –

+0

私の答えを更新しました。おそらく、非同期コードの実行を待っているわけではないからです。また、提案#5にも注意してください。 – EvZ

+0

私は既にこのApplication.CurrentでNavigationPageのメインページを作るために私のコードを変更します。MainPage =新しいNavigationPage(新しいMainPage());それはpushasyncを実行することができますが、他のページに移動した後、メインページに戻って、もう一度別のページに移動すると、この完全なMVVのアイデアは私を混乱させるでしょう、mvvmはエンタープライズアプリケーションを作成するために必須ですか? –

関連する問題