壳牌导航xamarinForms .net毛伊岛

iqxoj9l9  于 2023-03-16  发布在  .NET
关注(0)|答案(1)|浏览(119)

我尝试使用一个按钮从一个页面到另一个页面在我的maui应用程序中进行简单的导航,但导航不工作。我在我的appShell.cs中注册路线:
Routing.RegisterRoute(nameof(LoadingQuiz),typeof(LoadingQuiz));
在我的视图模型中,我实现了导航方法,如下所示:

[QueryProperty(nameof(QuizJson), nameof(QuizesVM))]
    public partial class QuizDetailsViewModel : BaseViewModel
    {
        //Navigation
        //   [RelayCommand]
        //  public Task Navigate() => Shell.Current.GoToAsync(nameof(LoadingQuiz));

         [RelayCommand]
         public async Task NavigateToAsync() {
             await Shell.Current.GoToAsync(nameof(LoadingQuiz));
         }
        
        private string quizJson;
        public string QuizJson
        {

            get { return quizJson; }
            set
            {
                quizJson = value;
                QuizesVM = Helpers.Helpers.DeserializeObject<QuizVM>(quizJson);
            }
        }
        private QuizVM quizvm;
        public QuizVM QuizesVM
        {
            get { return quizvm; }
            set
            {
                quizvm = value;
                OnPropertyChanged(nameof(QuizesVM));
            }

        }
        public QuizDetailsViewModel()
        {
          
        }

    }

我把它绑在我的视野里

<Button Text=" Start Quiz" TextColor="White" BackgroundColor="#A8A223" WidthRequest="200" HeightRequest="50" CornerRadius="25" HorizontalOptions="Center" Command="{Binding NavigateToCommand}"/>

这是我的QuizHomeDetails.cs,我在其中创建视图和视图模型之间的绑定:

public partial class QuizHomeDetails : ContentPage
{
    public QuizHomeDetails(QuizDetailsViewModel vm)
    {
        InitializeComponent();
        BindingContext = vm;
        //Run the method in seperat thread 
        Task.Run(Rotateimage);
    }

    private async void Rotateimage()
    {
        while (true)
        {
            await QuizImg.RelRotateTo(360, 5000, Easing.BounceOut);

        }
    }

}

此外,我注册的网页和视图模型在我的程序文件和导航仍然不工作,当我点击按钮

dced5bon

dced5bon1#

您可以参考下面的示例代码,了解如何使用按钮从MainPage导航到LoadingQuiz页面。

主页.xaml

<Button Text=" Start Quiz" TextColor="White" BackgroundColor="#A8A223" WidthRequest="200" HeightRequest="50" CornerRadius="25" HorizontalOptions="Center" Command="{Binding NavigateToCommand}"/>

主页.xaml.cs

public MainPage(MainPageViewModel vm) 
{
     InitializeComponent();

     BindingContext= vm;
}

主页面视图模式.cs

public partial class MainPageViewModel 
{

     [RelayCommand]
     public async Task NavigateToAsync()
     {

          await Shell.Current.GoToAsync(nameof(LoadingQuiz));
     }
}

相关问题