繁体   English   中英

Xamarin Forms ItemDisplayBinding 中的选取器未绑定

[英]Picker in Xamarin Forms ItemDisplayBinding not binding

我正在 Xamarin 中编写一个页面,并且在 Picker 中绑定到 ItemDisplayBinding 时遇到问题。 我拥有的代码显示了我试图绑定它的主要组件。 我可以设置 ItemsSource="{Binding terms}" 但如果我尝试设置 ItemDisplayBinding="{Binding Name}",Visual Studio 会显示“在数据上下文 'SearchViewModel' 中找不到成员并且没有填充选取器。所有我看过的解决方案没有帮助我解决这个问题。

我的术语模型(在模型文件夹中):

namespace ClassSearch.Models
{
    public class Term
    {
        public string Code { get; set; }
        public string Name { get; set; }
        public Term(string code, string name)
        {
            Code = code;
            Name = name;
        }
    }
}

我的浏览页面:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ClassSearch.Views.SearchPage"
             xmlns:viewmodel="clr-namespace:ClassSearch.ViewModels"
             x:DataType="viewmodel:SearchViewModel"
             xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
             ios:Page.UseSafeArea="True">
    <ContentPage.BindingContext>
        <viewmodel:SearchViewModel/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
<Grid>
            <Grid.ColumnDefinitions>
                ...
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
               ...
            </Grid.RowDefinitions>
            
            <Frame Style="{StaticResource FrameStyle}" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="0">
            <Picker Title="Select" 
                    ItemsSource="{Binding Terms}"
                    IsEnabled="{Binding IsNotBusy}"
                    />
        </Frame>
 </Grid>
    </ContentPage.Content>
</ContentPage>

我的视图模型:

public class SearchViewModel : ViewModelBase
{
    public ObservableRangeCollection<Term> Terms { get; set; }
    public SearchViewModel()
    {
    Terms = new ObservableRangeCollection<Term>();
    }
    public async Task GetCurrentTerms()
    {
        IsBusy = true;
        Terms = Terms ?? await CurrentTerm.GetTerms();
        IsBusy = false;
    }
}

SearchPage.xaml.cs 的 OnAppearing() 被覆盖

protected override async void OnAppearing()
    {
        base.OnAppearing();
        if (BindingContext is SearchViewModel vm)
        {
            await vm.GetCurrentTerms();
        }
     }

看来这是导致问题的代码,但我不明白问题是什么。

public static class CurrentTerm
    {
        public static async Task<ObservableRangeCollection<Term>> GetTerms()
        {
            ObservableRangeCollection<Term> terms = new ObservableRangeCollection<Term>();
            //  YYYY/MM/DD
            DateTime currentDay = DateTime.Now.Date;
            DateTime SummerCheck = new DateTime(currentDay.Year, 11, 01);
            DateTime SpringCheck = new DateTime(currentDay.Year, 10, 01);
            DateTime FallCheck = new DateTime(currentDay.Year, 03, 01);
            if ((currentDay - SummerCheck).Days > 0)
            {
                var s = $"2{currentDay.Year % 100 + 1}4";
                var sp = $"2{currentDay.Year % 100 + 1}1";
                terms.Add(new Term(s, $"Summer {currentDay.Year + 1}"));
                terms.Add(new Term(sp, $"Spring {currentDay.Year + 1}"));
            }
            else if ((currentDay - SpringCheck).Days > 0)
            {
                var sp = $"2{currentDay.Year % 100 + 1}1";
                var f = $"2{currentDay.Year % 100}7";
                terms.Add(new Term(sp, $"Spring {currentDay.Year + 1}"));
                terms.Add(new Term(f, $"Fall {currentDay.Year}"));
            }
            else if ((currentDay - FallCheck).Days > 0)
            {
                var f = $"2{currentDay.Year % 100}7";
                var s = $"2{currentDay.Year % 100}4";
                terms.Add(new Term(f, $"Fall {currentDay.Year}"));
                terms.Add(new Term(s, $"Summer {currentDay.Year}"));
            }
            return terms;
        }

    }

由于我没有CurrentTerm.GetTerms() ,我制作了一个示例,直接在GetCurrentTerms方法中添加数据供您参考。

xml:

  <ContentPage.BindingContext>
    <viewmodel:SearchViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
    <StackLayout>
        <Picker Title="Select" 
                ItemsSource="{Binding Terms}"
                ItemDisplayBinding="{Binding Name}"                    
                /> <!--IsEnabled="{Binding IsNotBusy}"-->
    </StackLayout>
</ContentPage.Content>

视图模型:

public class SearchViewModel : ViewModelBase
{
    public ObservableRangeCollection<Term> Terms { get; set; }
    public SearchViewModel()
    {
        Terms = new ObservableRangeCollection<Term>();
    }
    public async Task GetCurrentTerms()
    {
        //IsBusy = true;
        //Terms = Terms ?? await CurrentTerm.GetTerms();
        //IsBusy = false;

        Term term1 = new Term("1", "a");
        Term term2 = new Term("2", "b");
        Term term3 = new Term("3", "c");
        Terms.Add(term1);
        Terms.Add(term2);
        Terms.Add(term3);
    }
}

在此处输入图像描述

更新:

删除OnAppearing中的代码。 在 ViewModel 中设置数据,如下所示。

public static class CurrentTerm
{
    public static ObservableRangeCollection<Term> terms { get; set; }
    static CurrentTerm()
    {
        terms = new ObservableRangeCollection<Term>();
        //  YYYY/MM/DD
        DateTime currentDay = DateTime.Now.Date;
        DateTime SummerCheck = new DateTime(currentDay.Year, 11, 01);
        DateTime SpringCheck = new DateTime(currentDay.Year, 10, 01);
        DateTime FallCheck = new DateTime(currentDay.Year, 03, 01);
        if ((currentDay - SummerCheck).Days > 0)
        {
            var s = $"2{currentDay.Year % 100 + 1}4";
            var sp = $"2{currentDay.Year % 100 + 1}1";
            terms.Add(new Term(s, $"Summer {currentDay.Year + 1}"));
            terms.Add(new Term(sp, $"Spring {currentDay.Year + 1}"));
        }
        else if ((currentDay - SpringCheck).Days > 0)
        {
            var sp = $"2{currentDay.Year % 100 + 1}1";
            var f = $"2{currentDay.Year % 100}7";
            terms.Add(new Term(sp, $"Spring {currentDay.Year + 1}"));
            terms.Add(new Term(f, $"Fall {currentDay.Year}"));
        }
        else if ((currentDay - FallCheck).Days > 0)
        {
            var f = $"2{currentDay.Year % 100}7";
            var s = $"2{currentDay.Year % 100}4";
            terms.Add(new Term(f, $"Fall {currentDay.Year}"));
            terms.Add(new Term(s, $"Summer {currentDay.Year}"));
        }
       
    }
}

视图模型:

 public class SearchViewModel : ViewModelBase
{

    public ObservableRangeCollection<Term> Terms { get { return CurrentTerm.terms; } }
    public SearchViewModel()
    {

    }
    //public async Task GetCurrentTerms()
    //{
    //    //IsBusy = true;
    //    //Terms = Terms ?? await CurrentTerm.GetTerms();        
    //    //IsBusy = false;

    //    //Term term1 = new Term("1", "a");
    //    //Term term2 = new Term("2", "b");
    //    //Term term3 = new Term("3", "c");
    //    //Terms.Add(term1);
    //    //Terms.Add(term2);
    //    //Terms.Add(term3);
    //}
}

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM