【问题标题】:CS0120 An object reference is required for the non static field , method or property 'Userscache.GetUsersAsync(), [duplicate]CS0120 非静态字段、方法或属性'Userscache.GetUsersAsync() 需要对象引用,[重复]
【发布时间】:2021-09-21 13:15:25
【问题描述】:

Mypageviewmodel.cs

using MonkeyCache.FileStore;
using MvvmHelpers.Commands;
using Newtonsoft.Json;
using Refit;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using UsersApp.Model;
using UsersApp.Services;
using Xamarin.Essentials;

namespace UsersApp.Viewmodel
{
    public class Mypageviewmodel : INotifyPropertyChanged
    {
        public IUsers Usercache = new Userscache();


        public ICommand Getusers { get; set; }
        public ObservableCollection<Users> users { get; set; }

        public Mypageviewmodel()
        {
            Getusers = new Command(async () => await getusers());
            Getusers.Execute(null);
        }

        async Task getusers()
        {

            var result = await Userscache.GetUsersAsync();
            if (result != null)
                users = new ObservableCollection<Users>(result);

        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

Userscache.cs

using MonkeyCache.FileStore;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using UsersApp.Model;
using Xamarin.Essentials;

namespace UsersApp.Services
{
    public class Userscache : IUsers
    {
        const string url = "https://jsonplaceholder.typicode.com";

        public Userscache()
        {
            Barrel.ApplicationId = "UsersApp";
        }
        public async Task<IEnumerable<Users>> GetUsersAsync()
        {
            try
            {
                if (Connectivity.NetworkAccess != NetworkAccess.Internet &&
                    !Barrel.Current.IsExpired(key: url))
                {
                    await Task.Yield();
                    return Barrel.Current.Get<IEnumerable<Users>>(key: url);
                }

                var client = new HttpClient();
                var json = await client.GetStringAsync(url);

                var getusers = JsonConvert.DeserializeObject<IEnumerable<Users>>(json);

                Barrel.Current.Add(key: url, data: getusers, expireIn: TimeSpan.FromDays(1));

                return getusers;
            }
            catch (Exception ex)
            {
                //Debug.WriteLine($"Unable to get information from server {ex}");
                //throw ex;
            }
            return null;
        }
    }
}

IUsers.cs

using Refit;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using UsersApp.Model;

namespace UsersApp.Services
{
    [Headers("Content-Type: application/json")]
    public interface IUsers
    {
        //[Get("/users")]
        Task<IEnumerable<Users>> GetUsersAsync();
    }
}

Mypage.xaml.cs

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UsersApp.Viewmodel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace UsersApp.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Mypage : ContentPage
    {
        public Mypage()
        {
            InitializeComponent();
            BindingContext = new Mypageviewmodel();
        }

    }
}

Mypage.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:viewmodels="clr-namespace:UsersApp.Viewmodel"
             x:Class="UsersApp.Views.Mypage">
    <ContentPage.BindingContext>
        <viewmodels:Mypageviewmodel/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <StackLayout Padding="40">
            <Button Text="Get Users" Command="{Binding Getusers}"
                BackgroundColor="Black" TextColor="White" HorizontalOptions="FillAndExpand"/>
            <ListView x:Name="UsersList">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextCell Text="{Binding name}" />
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

我首先使用 refit 从 typicode.json API 取回数据,但我还必须进行缓存,所以我尝试关注 https://xamgirl.com/improving-the-ux-of-a-xamarin-forms-application-by-caching-data/ 这篇文章。但是我现在被卡住了,我尝试使用静态,但是接下来会出现更多错误。另外,有人可以告诉我如何将这些数据上传到我的本地存储中(使用 SQLite 创建)。我是 xamarin 的新手*

【问题讨论】:

  • 类型名称为Userscache,变量名称相同——不要那样做。
  • var result = await Userscache.GetUsersAsync(); -> var result = await Usercache.GetUsersAsync();?

标签: c# android xamarin xamarin.forms cross-platform


【解决方案1】:

public IUsers Usercache = new Userscache();

这就是问题

public IUsers Userscache = new Userscache();

知道它没有显示任何错误,但是当我在模拟器中构建应用程序后,当我点击获取用户时,它没有显示任何内容

是我的 ICommand 错误还是我的 xaml 代码和绑定有任何问题

【讨论】:

    【解决方案2】:

    您在 UsersCache 获取值之前执行命令

    【讨论】:

    • 如何纠正?在我在按钮中使用 clicked 方法之前,但现在我将其作为 MVVM 进行,所以我必须摆脱那个异步 void 方法并在我的视图模型中使用命令
    猜你喜欢
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多