c# - 提供页面间导航的方法出错
问题描述
我正在开发一个我最初用 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());
}
}
}
解决方案
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());
}
推荐阅读
- java - 是否可以将 EL 用作独立的模板语言?
- sql - 生成最小和最大日期之间的日期范围 Athena presto SQL 序列错误
- yaml - xacro 通过提供变量从 YAML 中获取值
- postgresql - 使用 PostgreSQL 根据 UTC 列值查找昨天和今天之间的记录时遇到问题
- vulkan - Vulkan 的描述符集布局和 `VkWriteDescriptorInfo` 有什么区别?
- java - Lucee 升级问题 - 版本 5.2.9 到 5.3.8
- reactjs - 处理提交上的 React-Hook-Form 和 preventDefault
- pine-script - 多个时间框架上的几个 EMA 的 Pine 脚本
- react-native - 如何将模态附加到根元素?
- laravel-5 - 创建pdf文件ContentOperationNotPermittedError时laravel-snappy错误