2017-12-08 13 views
0

xamarin 양식에서 mvvm을 사용하려고했지만 각 페이지의 탐색과 혼동되지 않습니다. 이미 Xamarin 양식의 모든 탐색을 처리하기 위해 인터페이스 및 NavigationService를 작성합니다. 오류 코드 줄을하지만 buton을 클릭하고 다른 페이지로 이동하려고하면 항상 충돌합니다. 여기에 내 코드 내 인터페이스MVVM Navigation Force 닫기 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()이 실제로 어떤 것을 반환하는지 확인 했습니까? 1. C# naming conventions
자바 다릅니다 : 내가 발견

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

또 다른 문제 :

시도는 GetCurrentPage()로 변경합니다. 메소드 이름은 대문자로 시작해야합니다. 현재 순수한 믹스가 있습니다.
2. XAML에서 BindingContext을 초기 상태로 초기화하면 bug으로 인해 ViewModel이 두 번 생성됩니다. 다음과 같이 변경하십시오.

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

3. 각 명령에 대해 NavigationService를 다시 만들 필요가 없습니다. ViewModel에서 다시 사용하십시오.

<StackLayout BackgroundColor="##CEB053"> 

가 16 진수 코드를 수정 :
4. 당신은 일부 손상된 XAML 있습니다. 일반적으로 XAMLC을 사용하면 코드를 작성하는 동안 XAML 오류에 대한 알림을받을 수 있습니다.
PushAsync을 사용하려면 MainPageNavigationPage으로 싸야합니다. 위의 목록을 통해

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

확인 및 응용 프로그램 출력과 예외주의 :
6. 당신은 비동기 코드 실행을 기다리고해야합니다.

+0

GetCurrentPage 메서드를 사용하여 PushAsync를 PushModalAsync 및 해당 작업으로 변경하면 이미 작동하지만, PushAsync Im을 사용하려고 할 때 Application.Current.MainPage = new MainPage();를 사용하면 작동하지 않습니다. 내 메인 페이지에서 로그인 이후 나는 내 사용자가 다시 로그인 페이지로 돌아갈 수 있기를 원하지 않기 때문에. 푸시 비동기를 사용하는 이유는 무엇입니까? –

+0

내 대답이 업데이트되었습니다. 아마도 비동기 코드 실행을 기다리고 있지 않기 때문일 것입니다. 또한 제안 # 5에주의하십시오. – EvZ

+0

NavigationPage의 메인 페이지를이 Application.Current로 만들기 위해 이미 코드를 변경했습니다.MainPage = 새 NavigationPage (새 MainPage()); 그리고 pushasync를 실행할 수 있지만 다른 페이지로 이동 한 다음 메인 페이지로 돌아가서 다른 페이지로 다시 이동하면 닫을 수 있습니다.이 전체 mvvm 아이디어는 여전히 혼란 스럽습니다. mvvm은 엔터프라이즈 애플리케이션을 작성하기 위해 필수입니까? –