Xamarin Forms ItemDisplayBinding 中的选择器未绑定
Picker in Xamarin Forms ItemDisplayBinding not binding
我正在 Xamarin 中编写一个页面,但在绑定到选取器中的 ItemDisplayBinding 时遇到问题。我拥有的代码显示了我试图绑定它的主要组件。我可以设置 ItemsSource="{Binding terms}" 但如果我尝试设置 ItemDisplayBinding="{Binding Name}",Visual Studio 会显示“在数据上下文 'SearchViewModel' 中找不到成员并且选择器是没有被填充。我看过的所有解决方案都没有帮助我解决这个问题。
我的 Term 模型(在模型文件夹中):
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
方法中添加数据的例子,供大家参考。
Xaml:
<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);
//}
}
我正在 Xamarin 中编写一个页面,但在绑定到选取器中的 ItemDisplayBinding 时遇到问题。我拥有的代码显示了我试图绑定它的主要组件。我可以设置 ItemsSource="{Binding terms}" 但如果我尝试设置 ItemDisplayBinding="{Binding Name}",Visual Studio 会显示“在数据上下文 'SearchViewModel' 中找不到成员并且选择器是没有被填充。我看过的所有解决方案都没有帮助我解决这个问题。
我的 Term 模型(在模型文件夹中):
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
方法中添加数据的例子,供大家参考。
Xaml:
<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);
//}
}