首页 > 解决方案 > 提供页面间导航的方法出错

问题描述

我正在开发一个我最初用 C#(包括 UI)编写的应用程序,但现在我正在重写它,以便 UI 由 xml 处理。该应用程序的每个部分都在 C# 中运行良好,但是,现在我已将 UI 切换到 xml,我的 ViewModels 中出现错误。

错误:“StartPageViewModel”不包含“Navigation”的定义,并且找不到接受“StartPageViewModel”类型的第一个参数的可访问扩展方法“Navigation”(您是否缺少 using 指令或程序集引用?)

XML

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         x:Class="SampleApp.Views.StartPage">
<ContentPage.Content>
    <StackLayout>
        <Grid VerticalOptions="CenterAndExpand">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Button x:Name="OnePlayerGame" Text="One Player Game" 
                    Command="{Binding StartOnePlayerGameCommand}" 
                    Grid.Row="0" Grid.Column="0" 
                    HorizontalOptions="Center"/>
            <Button x:Name="TwoPlayerGame" Text="Two Player Game" 
                    Command="{Binding StartTwoPlayerGameCommand}"
                    Grid.Row="0" Grid.Column="1" HorizontalOptions="Center" />
        </Grid>
    </StackLayout>
</ContentPage.Content>

背后的代码

namespace SampleApp.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class StartPage : ContentPage
{
    public StartPage()
    {
        InitializeComponent();
        BindingContext = new StartPageViewModel();
    }
}
}

视图模型代码

namespace SampleApp.ViewModels
{
class StartPageViewModel : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        var changed = PropertyChanged;
        if (changed == null)
            return;

        changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public Command StartOnePlayerGameCommand { get; set; }
    public Command StartTwoPlayerGameCommand { get; set; }

    public StartPageViewModel()
    {
        StartOnePlayerGameCommand = new Command(() => StartOnePlayerGame());
        StartTwoPlayerGameCommand = new Command(() => StartTwoPlayerGame());
    }

    private void StartOnePlayerGame()
    {
        //the error is here (Navigation has a red squigly under it)
        this.Navigation.PushAsync(new Views.OnePlayerPage());
    }

    private void StartTwoPlayerGame()
    {
        //the error is here (Navigation has a red squigly under it)
        this.Navigation.PushAsync(new Views.NameEntryPage());
    }
}
}

标签: c#xamarin.forms

解决方案


Navigation是 Page 的一个属性,您可以通过ViewModel构造函数传递导航,然后在ViewModel

class StartPageViewModel : INotifyPropertyChanged
{

    public INavigation myNavigation { get; set; }

    public StartPageViewModel(INavigation navigation)
    {
        myNavigation = navigation;

        StartOnePlayerGameCommand = new Command(() => StartOnePlayerGame());
        StartTwoPlayerGameCommand = new Command(() => StartTwoPlayerGame());
    }

    private void StartOnePlayerGame()
    {
        //the error is here (Navigation has a red squigly under it)
        myNavigation.PushAsync(new Views.OnePlayerPage());
    }

    private void StartTwoPlayerGame()
    {
        //the error is here (Navigation has a red squigly under it)
        myNavigation.PushAsync(new Views.NameEntryPage());
    }
}

从页面传递它:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        this.BindingContext = new StartPageViewModel(Navigation);
    }
}

您可以尝试使用的另一种方法Application.Current.MainPage.Navigation

private async void StartOnePlayerGame()
{
    //the error is here (Navigation has a red squigly under it)

    await Application.Current.MainPage.Navigation.PushAsync(new Views.OnePlayerPage());
}

推荐阅读