在model. Xamarin.forms上未找到IsRefreshing

oxf4rvwz  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(51)

我需要添加使用RefreshView更新用户数据的功能,但我收到一条错误消息。
这是我的页面,UserData应该更新:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:models="clr-namespace:EcoTracker.Models"
         x:Class="EcoTracker.Views.MainPage"
         x:DataType="models:UserData">

<ContentPage.Title>
    Новостная лента
</ContentPage.Title>

<ContentPage.Content>
    <RefreshView x:Name="refreshView" RefreshColor="#7bb040" IsRefreshing="{Binding IsRefreshing}" Command="{Binding RefreshCommand}">
        <ScrollView>
            <StackLayout>
                <StackLayout>
                    <Label FontSize="30" TextColor="White" HorizontalOptions="Center" Margin="0,10,0,0">
                        <Label.FormattedText>
                            <FormattedString>
                                <Span Text="Eco" TextColor="#009444" />
                                <Span Text="Tracker" TextColor="#7bb040" />
                            </FormattedString>
                        </Label.FormattedText>
                    </Label>
                    <Label Text="" FontSize="24" TextColor="#009444" HorizontalOptions="Center">
                        <Label.FormattedText>
                            <FormattedString>
                                <Span Text="Привет," FontSize="24" TextColor="#81b745" />
                                <Span Text="" FontSize="24" TextColor="#009444" />
                                <Span Text="{Binding name}" FontSize="24" TextColor="#009444" />
                            </FormattedString>
                        </Label.FormattedText>
                    </Label>
                </StackLayout>

字符串
这就是我的刷新方法的样子:

public ObservableCollection<NewsItem> NewsItems { get; set; } = new ObservableCollection<NewsItem>();
private UserData _user;
public MainPage()
{
    InitializeComponent();
    LoadNewsAsync();
    LoadUserData();
    BindingContext = this;
}

private bool _isRefreshing;
public bool IsRefreshing
{
    get { return _isRefreshing; }
    set
    {
        if (_isRefreshing != value)
        {
            _isRefreshing = value;
            OnPropertyChanged(nameof(IsRefreshing));
        }
    }
}

private void LoadUserData()
{
    _user = UserDataHolder.GetUserData();
    if (_user != null)
    {
        BindingContext = null;
        BindingContext = _user;
    }
}

public ICommand RefreshCommand => new Command(async () => await RefreshDataAsync());

private async Task RefreshDataAsync()
{
    IsRefreshing = true;

    await LoadNewsAsync();
    LoadUserData();

    IsRefreshing = false;
}


我得到一个错误:XFC0045绑定:
在“EcoTracker.Models.UserData”上未找到属性“IsRefreshing”。

ltskdhd1

ltskdhd11#

在“EcoTracker.Models.UserData”上未找到属性“IsRefreshing”。
正如Jason提到的,在MainPage.xaml中,您为页面设置了x:DataType="models:UserData",但也为它设置了BindingContext = this;。这可能会导致问题。

public MainPage()
{
    InitializeComponent();
    LoadNewsAsync();
    LoadUserData();
    BindingContext = this;
}

字符串
因此,您可以尝试从MainPage.xaml中删除以下代码:

x:DataType="models:UserData"


此外,您的代码还有一些其他问题,如果您在加载数据后为页面的BindingContext设置_user,则无法再次触发RefreshCommand命令。

if (_user != null)
    {
        BindingContext = null;
        BindingContext = _user;
    }


总而言之,为了使代码看起来更有逻辑性和更清晰,我建议您使用MVVM来实现这一点。
你可以参考以下代码:
1.创建一个视图模型TestViewModel.cs并添加必要的代码。

public class TestViewModel: INotifyPropertyChanged
{
    public ObservableCollection<NewsItem> NewsItems { get; set; } = new ObservableCollection<NewsItem>();

    //private UserData _user;
    public UserData _user { get; set; }

    private bool _isRefreshing;
    public bool IsRefreshing
    {
        get { return _isRefreshing; }
        set
        {
            if (_isRefreshing != value)
            {
                _isRefreshing = value;
                OnPropertyChanged(nameof(IsRefreshing));
            }
        }
    }

    public TestViewModel() {
        _user = new UserData();

        LoadUserData("initial name");
    }

    private void LoadUserData(string name)
    {
        // _user = UserDataHolder.GetUserData();

        _user.Name = name;  // add fake date here

    }

    public ICommand RefreshCommand => new Command(async () => await RefreshDataAsync());

    private async Task RefreshDataAsync()
    {
        IsRefreshing = true;

        //await LoadNewsAsync();

        LoadUserData("upated name ");

        IsRefreshing = false;
    }

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


2.为UserData.cs实现接口INotifyPropertyChanged。如果我们更改属性Name的值,UI将自行刷新。

public class UserData: INotifyPropertyChanged
    {
        private string _name;
        public string Name
        {
            set { SetProperty(ref _name, value); }
            get { return _name; }
        }

        bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (Object.Equals(storage, value))
                return false;
            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

    }


3.使用示例:

<?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:refreshviewdemo="clr-namespace:RefreshViewDemo"
             x:Class="RefreshViewDemo.MyTestPage">

    <ContentPage.BindingContext>
        <refreshviewdemo:TestViewModel></refreshviewdemo:TestViewModel>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <RefreshView x:Name="refreshView" RefreshColor="#7bb040" IsRefreshing="{Binding IsRefreshing}" Command="{Binding RefreshCommand}">
            <ScrollView>
                <StackLayout>
                    <StackLayout>
                        <Label FontSize="30" TextColor="White" HorizontalOptions="Center" Margin="0,10,0,0">
                            <Label.FormattedText>
                                <FormattedString>
                                    <Span Text="Eco" TextColor="#009444" />
                                    <Span Text="Tracker" TextColor="#7bb040" />
                                </FormattedString>
                            </Label.FormattedText>
                        </Label>
                        <Label Text="" FontSize="24" TextColor="#009444" HorizontalOptions="Center">
                            <Label.FormattedText>
                                <FormattedString>
                                    <Span Text="Привет," FontSize="24" TextColor="#81b745" />
                                    <Span Text="" FontSize="24" TextColor="#009444" />
                                    <Span Text="{Binding _user.Name}" FontSize="24" TextColor="#009444" />
                                </FormattedString>
                            </Label.FormattedText>
                        </Label>
                    </StackLayout>

                </StackLayout>

            </ScrollView>

        </RefreshView>
    </ContentPage.Content>

</ContentPage>

注:

我们应该在视图模型中定义_user如下:

public UserData _user { get; set; }


然后像这样绑定数据:

<Span Text="{Binding _user.Name}" FontSize="24" TextColor="#009444" />

相关问题