【问题标题】:Do we have to write OnItemTapped method in xml code behind page or ViewModel Page ? and How?我们是否必须在页面或 ViewModel 页面后面的 xml 代码中编写 OnItemTapped 方法?如何?
【发布时间】: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


    【解决方案1】:

    首先,您的代码存在多个问题,例如,您的 ViewModel 继承自 ContentPage,这没有任何意义。

    所以请看看那里的一些 MVVM 示例。

    如果您想使用 MVVM 并且仍然可以“控制”您的 ViewModel 中的这些事件,您可以使用EventToCommandBehavior。有关它的更多信息herehere

    使用EventToCommandBehavior,您可以将事件绑定到 ViewModel 中的某些 Command,这样做是从代码隐藏类迁移更多代码逻辑,这是更加尊重 MVVM 的好方法.

    我还建议您查看文档并了解有关在您的 Xamarin.Forms 应用程序中实现 MVVM 的更多信息,here

    祝您编码顺利!

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    相关资源
    最近更新 更多