Xamarin Form - 无法从 Service 类方法获取返回值数据到 MainPage 方法中进行过滤

问题描述

我是 Xamarin 的新手,我在 Xamarin-Form 中有一个应用程序,它从 Web api 获取数据并从 Entry 控件获取用户输入。

web api 服务类工作正常,并在 getCourses 方法中实现反序列化,如下面的代码片段 1 所示。

Entry 控件也能正常工作,直到它检索 MainPage 类上的用户输入,OnOkGetCourseButton 方法,如下所示代码片段 2

我想要实现的是,在 MainPage.xaml.cs 中,我创建了一个方法获取用户输入数据并检查反序列化的 json 数据(Id 特别),

如果它在反序列化的数据列表中找到Id,那么它可以将找到的数据发送到另一个ViewPage并显示它们。

如果找不到数据,则显示一个对话框。

到目前为止,我尝试从 MainPage 类中调用 Task getCourses() 方法,在 CheckCourseComplete 中作为见下文,但它没有给我任何价值/什么,某种空值。

我不想根据 getCourses() 中的 web api json 响应过滤用户输入, 我想以一种单独的方法来做到这一点,以遵循 S-OLID(单一职责原则)。 如果在单独的方法中不可能,那么我只需要让它工作。

请问实现它的最佳方法是什么?

代码片段 1

       public class CourseService : ICourseService
       {
           
           string Base_Url = "https://www.test.com/api/TheCourse";
           
           public async Task<ObservableCollection<Course>> getCourses()
           {
               try
               {
                   string url = Base_Url;
   
                   HttpClient client = new HttpClient();
                   HttpResponseMessage responseMessage = await client.GetAsync(url);
   
                   if (responseMessage.StatusCode == System.Net.HttpStatusCode.OK)
                   {
                       var result = await responseMessage.Content.ReadAsstringAsync();
                       var deserializedClass = JsonConvert.DeserializeObject<ObservableCollection<Course>>(result);
   
                       // I don't want to do that here,as it will violate SRP (SOLID)
   
                       return deserializedClass;
                   }
   
                   return null;
               }
               catch (Exception)
               {
                   throw;
               }
           }
   
       }

代码片段 2

namespace CourseMobile
{
    public partial class MainPage : ContentPage
    {
        private string _getEntryText;

        private readonly Courseviewmodel orderviewmodel;
        public Course FetchCourse { get; set; }

        public MainPage()
        {
            InitializeComponent();
            CheckCourseComplete();
            BindingContext = new Courseviewmodel();
        }

        public string GetEntryText 
        {
            get => _getEntryText;
            set => _getEntryText = value;
        }

        public async void OnOkGetCourseButton(object sender,EventArgs e)
        {
            var inputtedCourseNumber = this.GetEntryText;

            if(inputtedCourseNumber == string.Empty)
            {
                await displayAlert("","Please enter your Course number","OK 3");
            }
            else
            {
                CheckCourseComplete();

                this.GetEntryText = inputtedCourseNumber;

                await displayAlert("New Text",inputtedCourseNumber,"OK 2");
            }
        }

        void Entry_TextChanged(object sender,TextChangedEventArgs e)
        {
            var newText = e.NewTextValue;
            this.GetEntryText = newText;
        }

        public async void CheckCourseComplete()
        {
            CourseService myCourse = new CourseService();

            await myCourse.getCourses();            // It doesn't return the json data (web api data)
            
            
            // I need to check user input + (web api data) here

        }

    }
}

解决方法

getCourses 是异步的,所以调用时需要使用 await

    public async void CheckCourseComplete()
    {
        CourseService myCourse = new CourseService();

        var data = await myCourse.getCourses();

        // now filter data
    }