Xamarin listView第二单击不显示项目

时间:2018-12-03 17:20:13

标签: c# xamarin.forms listviewitem

有人可以告诉我我在做什么错吗?我的应用程序有两个listView。首先是本地的,它运作完美。我单击列表的元素,然后打开包含详细信息的页面。

第二个列表显示了我从REST Api中读取的元素。正确读取日期,第一次列表正确显示了元素,当我单击其中的一个元素时,将打开包含详细信息的页面。当我尝试再次单击一个项目时,会出现问题。包含详细信息的页面无法正常工作,我无法从列表中返回。

我一直在寻找解决方案,但无法将其与我的项目相匹配。 谁能告诉我我在做什么错?

我的代码:

型号: UnivElement.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;

namespace PrismBlankXF.Models
{

    //[DataContract]
    public class UnivElement
    {
        //[DataMember(Name = "C0")]
        public string C0 { get; set; }

        //[DataMember(Name = "C1")]
        public string C1 { get; set; }

        //[DataMember(Name = "C2")]
        public string C2 { get; set; }

        //[DataMember(Name = "C3")]
        public string C3 { get; set; }

        //[DataMember(Name = "C4")]
        public string C4 { get; set; }

        //[DataMember(Name = "C5")]
        public string C5 { get; set; }

        //[DataMember(Name = "Color")]
        public string Color { get; set; }
    }
}

观看次数: ZasobyListContentPage.xaml.cs

using System.Collections;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using PrismBlankXF.Date;
using PrismBlankXF.Date.Rest;
using PrismBlankXF.Models;
using PrismBlankXF.ViewModels;
using Xamarin.Forms;

namespace PrismBlankXF.Views
{
    public partial class ZasobyListContentPage : ContentPage
    {
        private readonly MyRestDataService _myrestDataService = new MyRestDataService();

        public ZasobyListContentPage()
        {
            InitializeComponent();

            Title = "Zdalne REST Api ";

        }

        protected async override void OnAppearing()
        {
            base.OnAppearing();

            List<UnivElement> listitems = await _myrestDataService.GetAllWithParam(AppState.CurrentCode, AppState.User);

             lstView.ItemsSource = listitems;

        }


    }
}

ZasobyListContentPage.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"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             xmlns:behaviors="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"
             xmlns:converters="clr-namespace:PrismBlankXF.Converters;assembly=PrismBlankXF"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="PrismBlankXF.Views.ZasobyListContentPage"
             Title="{Binding Title}"
             BackgroundColor="WhiteSmoke">

    <StackLayout>
        <StackLayout>

            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="160"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Label
                    Grid.Row="0"
                    Grid.Column="0"
                    Text="LISTA REST API"
                    TextColor="CadetBlue"
                    FontAttributes="Bold"
                    FontSize="Small"/>
                <Label
                        Grid.Row="1"
                        Grid.Column="0"
                        Text="Zalogowany użytkownik:"
                        TextColor="DarkSlateGray"
                        FontSize="Small"
                        FontAttributes="Bold"/>
                <Label 
                        Grid.Row="1"
                        Grid.Column="1"
                        Text="{Binding UserCode}"
                        FontSize="Small"
                        FontAttributes="None"/>

                <Label
                        Grid.Row="2"
                        Grid.Column="0"
                        Text="Lokalizacja:"
                        TextColor="DarkSlateGray"
                        FontSize="Small"
                        FontAttributes="Bold"/>
                <Label 
                        Grid.Row="2"
                        Grid.Column="1"
                        Text="{Binding LokalizCode}"
                        FontSize="Small"
                        FontAttributes="None"/>
            </Grid>

        </StackLayout>

        <StackLayout>
            <ListView 
                x:Name="lstView"
                HasUnevenRows="True"
                ItemsSource="{Binding MyDatas}">

                <ListView.Behaviors>
                    <behaviors:EventToCommandBehavior 
                        EventName="ItemTapped"
                        Command="{Binding ItemTappedCommand}"
                        EventArgsConverter="{converters:ItemTappedEventArgsConverter}" />

                </ListView.Behaviors>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="75"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>

                                <Image Grid.Column="0" WidthRequest="75" Source ="emotikon1.png" Aspect="AspectFit" />
                                <StackLayout Grid.Column="1">
                                    <Label Text="{Binding C0}" FontSize="Small" FontAttributes="Bold"/>
                                    <Label Text="{Binding C1}" FontSize="Micro"/>
                                    <Label Text="{Binding C2}" FontSize="Micro"/>
                                </StackLayout>
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>


        </StackLayout>
    </StackLayout>

</ContentPage>

ZasobyListDetailsContentPage.xaml.cs

using Xamarin.Forms;

namespace PrismBlankXF.Views
{
    public partial class ZasobDetailsContentPage : ContentPage
    {
        public ZasobDetailsContentPage(Models.UnivElement selectedItem)
        {
            InitializeComponent();

        }
    }
}

ZasobyListDetailsContentPage.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"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="PrismBlankXF.Views.ZasobDetailsContentPage"
             Title="{Binding Title}"
             BackgroundColor="WhiteSmoke">

    <StackLayout>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Label Grid.Row="0" Grid.Column="0"
                   Text="{Binding UnivElemDetails.C0}"/>
            <Label Grid.Row="1" Grid.Column="0"
                   Text="{Binding UnivElemDetails.C1}"/>
            <Label Grid.Row="2" Grid.Column="0"
                   Text="{Binding UnivElemDetails.C2}"/>
            <Label Grid.Row="3" Grid.Column="0"
                   Text="{Binding UnivElemDetails.C3}"/>
            <Label Grid.Row="4" Grid.Column="0"
                   Text="{Binding UnivElemDetails.C4}"/>
            <Label Grid.Row="5" Grid.Column="0"
                   Text="{Binding UnivElemDetails.C5}"/>
        </Grid>
    </StackLayout>

</ContentPage>

和转换器ItemTapedEventArgsConverter.cs

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace PrismBlankXF.Converters
{
    public class ItemTappedEventArgsConverter : IValueConverter, IMarkupExtension
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var itemTappedEventArgs = value as ItemTappedEventArgs;
            if (itemTappedEventArgs == null)
            {
                throw new ArgumentException("Expected value to be of type ItemTappedEventArgs", nameof(value));
            }
            return itemTappedEventArgs.Item;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }
}

ViewModels:

ZasobyListContantPageViewModel.cs

等待_myrestDataService.GetAllWithParam(AppState.CurrentCode,AppState.User)的方法正确返回列表。

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Prism.Navigation;
using PrismBlankXF.Models;
using System.Threading.Tasks;
using PrismBlankXF.Date.Rest;
using Xamarin.Forms;

namespace PrismBlankXF.ViewModels
{
    public class ZasobyListContentPageViewModel : BindableBase, INavigatedAware
    {
        private DelegateCommand<UnivElement> _selItemCommand;    //do obsługi ItemTapped w listView

        private MyRestDataService _myrestDataService=new MyRestDataService();

        private List<UnivElement> _mydatas;
        public List<UnivElement> MyDatas
        {
            get { return _mydatas; }
            set { SetProperty(ref _mydatas, value); }
        }

        private readonly INavigationService _navigationService;

        private string _title;
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private string _UserCode;
        public string UserCode
        {
            get { return _UserCode; }
            set { SetProperty(ref _UserCode, value); }
        }

        private string _LokalizCode;
        public string LokalizCode
        {
            get { return _LokalizCode; }
            set { SetProperty(ref _LokalizCode, value); }
        }

        private async Task GetAllWithParam(string machineCode, string userID)
        {
            MyDatas = await _myrestDataService.GetAllWithParam(AppState.CurrentCode, AppState.User);
        }


        /// <summary>
        /// KONSTRUKTOR
        /// </summary>
        public ZasobyListContentPageViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
            MyDatas = new List<UnivElement>();

            UserCode = AppState.User;
            LokalizCode = AppState.CurrentCode;

            Title = "Lista pozycji - web";

            GetAllWithParam(LokalizCode, UserCode);
        }

        public DelegateCommand<UnivElement> ItemTappedCommand => _selItemCommand ?? (_selItemCommand = new DelegateCommand<UnivElement>(ShowMyDateDetails));

        private async void ShowMyDateDetails(UnivElement paramData)
        {
            var parameters = new NavigationParameters
            {
                {"myItem", paramData },
                {"title", paramData.C1 }
            };

            await _navigationService.NavigateAsync("ZasobDetailsContentPage", parameters);



        }

        public void OnNavigatedFrom(NavigationParameters parameters)
        {
            //throw new NotImplementedException();

        }

        public void OnNavigatedTo(NavigationParameters parameters)
        {

            //throw new NotImplementedException();
            if (parameters.ContainsKey("par1"))
            {
                string par1 = (string)parameters["par1"];
                string par2 = (string)parameters["par2"];
            }
        }

        public void OnNavigatingTo(NavigationParameters parameters)
        {
            //throw new NotImplementedException();
        }
    }
}

ZasobyDetailsContantPageViewModel.cs

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using Prism.Navigation;
using PrismBlankXF.Models;

namespace PrismBlankXF.ViewModels
{
    public class ZasobDetailsContentPageViewModel : BindableBase, INavigatedAware
    {
        public ZasobDetailsContentPageViewModel()
        {

        }

        private string _title;
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private UnivElement _univelemdetails;
        public UnivElement UnivElemDetails
        {
            get { return _univelemdetails; }
            set { SetProperty(ref _univelemdetails, value); }
        }


        public void OnNavigatedFrom(NavigationParameters parameters)
        {
            throw new NotImplementedException();
        }

        public void OnNavigatedTo(NavigationParameters parameters)
        {
            if (parameters.ContainsKey("myItem"))
            {
                UnivElemDetails = (UnivElement)parameters["myItem"];
            }
            if (parameters.ContainsKey("title"))
            {
                Title = (string)parameters["title"];
            }
        }
    }
}

谢谢您的帮助:-)

0 个答案:

没有答案