使用Visual Studio 2015 proff,

我的LoginViewModel类(便携式类库)

public class LoginViewModel : INotifyPropertyChanged, INotifyCollectionChanged
{
    LoginPage page;
    private ObservableCollection<Employees> _employeeList;
    private string _loginName;

    public ObservableCollection<Employees> EmployeeList
    {
        get { return _employeeList; }
        set
        {
            _employeeList = value;
            OnPropertyChanged();
            OnCollectionChanged(NotifyCollectionChangedAction.Reset);
        }
    }

    public string LoginName
    {
        get { return _loginName; }
        set
        {
            _loginName = value;
            if (_loginName != null)
            {
                OnPropertyChanged();
            }
        }
    }

    public LoginViewModel(LoginPage parent)
    {
        page = parent;
    }

    public async void GetEmployees()
    {
        var loginService = new LoginService();
        EmployeeList = await loginService.GetEmployeesAsync();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public event NotifyCollectionChangedEventHandler CollectionChanged;
    protected virtual void OnCollectionChanged( NotifyCollectionChangedAction action)
    {
        CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(action));
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    }
}


我的LoginPage.xaml(便携式类库)

<?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="ScannerApp.Views.LoginPage"
         xmlns:ViewModels="clr-namespace:ScannerApp.ViewModels;assembly=ScannerApp">
<StackLayout Orientation="Vertical">

<Label Text="Please Login"
        VerticalOptions="Start"
        HorizontalTextAlignment="Center"
        IsVisible="true"
        FontSize="Large"
        FontAttributes="Bold" />

<ListView x:Name="mylist" ItemsSource="{Binding EmployeeList}"
        HasUnevenRows="True" SelectedItem="{Binding LoginName}">
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
        <StackLayout Orientation="Vertical" Padding="12,6">
          <Label Text="{Binding Name}"/>
        </StackLayout>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>






我的LoginPage.xaml.cs类(便携式类库)

public partial class LoginPage : ContentPage
{
    public LoginPage()
    {
        InitializeComponent();
        BindingContext = new LoginViewModel(this);
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        LoginViewModel model = new LoginViewModel(this);
        model.GetEmployees();
        BindingContext = model;
    }

    public ListView MyList
    {
        get
        {
            return mylist;
        }
    }
}




我得到了一个员工列表,前端的列表呈现了这一点。然后,用户从列表中选择一个名称。这时,我想检测到该名称,然后导航到我的其他页面。
目前,我的媒体资源没有被点击,我想知道这与我对“ OnAppearing”背后的代码的绑定有什么关系吗?但我不确定。

最佳答案

虽然您可以使用,但我还是建议您进行一些调整。

无需在构造函数和BindingContext中设置OnAppearing()。只需在代码隐藏区中将LoginViewModel设为类级别的私有属性,然后将其分配给构造函数中的BindingContext即可。然后在GetEmployees()中调用OnAppearing()

另外,您应该使GetEmployees()返回一个Task,以便尽可能长时间地等待链上。

ViewModel:

....

public async Task GetEmployees()
    {
        var loginService = new LoginService();
        EmployeeList = await loginService.GetEmployeesAsync();
    }

....


后台代码:

public partial class LoginPage : ContentPage
{
    private LoginViewModel _model;

    public LoginPage()
    {
        InitializeComponent();
        BindingContext = _model = new LoginViewModel(this);
    }

    protected override async void OnAppearing() //Notice I changed this to async and can now await the GetEmployees() call
    {
        base.OnAppearing();
        await _model.GetEmployees();
    }

    public ListView MyList
    {
        get
        {
            return mylist;
        }
    }

    private async void OnItemSelected(object sender, SelectedItemChangedEventArgs e) {
        if (e.SelectedItem == null) return;

        await Navigation.PushAsync(new MenuPage());
    }
}


XAML:

<!-- Adding OnItemSelected in XAML below -->
<ListView x:Name="mylist"
          ItemsSource="{Binding EmployeeList}"
          HasUnevenRows="True"
          SelectedItem="{Binding LoginName}"
          ItemSelected="OnItemSelected">
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
        <StackLayout Orientation="Vertical" Padding="12,6">
          <Label Text="{Binding Name}"/>
        </StackLayout>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

10-05 23:49