【发布时间】:2019-01-19 12:18:44
【问题描述】:
我正在尝试在我的 ViewModel 类中添加以下方法,但显示错误,例如在页面后面的代码中找不到方法,所以问题是我是否必须在页面后面的 xml 代码或 viewmodel 文件中编写以下方法,我想遵循 MVVM 模式
ViewModel 页面:
public class StationPickerViewModel : ContentPage
{
// private List<StationDetails> _stationDetails;
public List<StationDetails> Stations
{
get
{
return new List<StationDetails>() {
new StationDetails{ Id = 1, StationCode=01, StationFullName="vapi", StationShortName="vp", TrainId=1},
new StationDetails{ Id = 2, StationCode=02, StationFullName="KaramBele", StationShortName="kmb", TrainId=2},
new StationDetails{ Id = 3, StationCode=03, StationFullName="Bhilad", StationShortName="bh", TrainId=3},
new StationDetails{ Id = 4, StationCode=04, StationFullName="Sanjan", StationShortName="sn", TrainId=4},
new StationDetails{ Id = 5, StationCode=05, StationFullName="Umargam", StationShortName="um", TrainId=5}
};
}
}
public void OnItemTapped(object o, ItemTappedEventArgs e)
{
var station = e.Item as StationDetails;
DisplayAlert("Selection Mode", "You Tapped On" + station.StationFullName, "OK");
}
}
xml页面代码:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="RailwayIndicator.View.StationPickerPage">
<ContentPage.Content>
<StackLayout Padding="20">
<Label FontSize="Large">Choose the Starting Station</Label>
<ListView x:Name="StationList"
ItemsSource="Binding Stations"
ItemTapped="OnItemTapped">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="Binding StationFullName" Font="12" BackgroundColor="Brown"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
xml页面后面代码:
namespace RailwayIndicator.View
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class StationPickerPage : ContentPage
{
public StationPickerPage ()
{
InitializeComponent ();
}
}
}
【问题讨论】:
标签: c# xamarin.forms