// This code is the main page of the application.
await Navigation.PushAsync(new TodoItemPage
{
BindingContext = e.SelectedItem as Models.TodoItem,
});
public TodoItemPage()
{
InitializeComponent();
Title = "Name"; // here need show NAME - I can't display my name here
var nameEntry = new Entry();
nameEntry.SetBinding(Entry.TextProperty, "Name");
Content = new StackLayout
{
Margin = new Thickness(20),
VerticalOptions = LayoutOptions.StartAndExpand,
Children =
{
new Label { Text = "Name" },
nameEntry, // here NAME is shown
}
};
如何从BindingContext到TodoItemPage页面上的变量获取属性的值?
VS,C#,Xamarin形式
我尝试过
var data = (Models.TodoItem)BindingContext;
data.ID;
data.Name;
...
答案 0 :(得分:0)
对象类的Cast BindingContext。
示例:
public class TodoItem
{
public int Prop1 { get; set; }
public string Prop2 { get; set; }
}
然后使用
var data = BindingContext as TodoItem;
var val1 = data.Prop1;
var val2 = data.Prop2;
答案 1 :(得分:0)
您是否要在TodoItemPage的构造函数中访问BindingContext?它不应该可用。构造函数代码完成后,将调用BindingContext的setter。 如果要访问它,则可以在构造函数中向此添加一个事件。 并访问其值
[更新]
您无法在构造函数中访问BindingContext,因为BindingContext = e.SelectedItem as Models.TodoItem
中的所有代码都已被执行后,public TodoItemPage()
{...}
的设置才完成。
您可以根据绑定上下文执行以下操作以更新这些值
Entry m_nameentry; //to access it later outside of the constructor
public TodoItemPage()
{
InitializeComponent();
BindingContextChanged += TodoItemPage_BindingContextChanged; //put the code that should use the BindingContext Value inside
//Title = "Name"; // here need show NAME - I can't display my name here
//var nameEntry = new Entry();
m_nameentry = new Entry();
nameEntry.SetBinding(Entry.TextProperty, "Name");
Content = new StackLayout
{
Margin = new Thickness(20),
VerticalOptions = LayoutOptions.StartAndExpand,
Children =
{
new Label { Text = "Name" },
//nameEntry, // here NAME is shown
m_nameentry
}
};
}
private void TodoItemPage_BindingContextChanged(object sender, EventArgs e)
{
Title = (BindingContext as Models.TodoItem).Name;
m_nameentry.Text = (BindingContext as Models.TodoItem).Name;
}
每当BindingContext更改时,这将更新您的Entry和Title。 如果Models.TodoItem实现了INotifyPropertyChanged接口,并且您还希望在绑定完成后更改“名称”的值,则必须在Models.TodoItem的PropertyChangedEvent上注册并在此进行所有操作。
答案 2 :(得分:0)
我使用两个页面,一个是Page20,另一个是Page21。 Page20中有ListView,ListView selectedItem是Page21的BindingContext。这是代码示例:
第20页:
<ListView ItemSelected="ListView_ItemSelectedAsync" ItemsSource="{Binding items}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding Id}" />
<Label Text="{Binding Name}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page20 : ContentPage
{
public ObservableCollection<TodoItem1> items { get; set; }
public Page20 ()
{
InitializeComponent ();
items = new ObservableCollection<TodoItem1>()
{
new TodoItem1(){Id=1,Name="aaa"},
new TodoItem1(){Id=2,Name="bbb"},
new TodoItem1(){Id=3,Name="ccc"},
new TodoItem1(){Id=4,Name="ddd"},
new TodoItem1(){Id=5,Name="eee"},
new TodoItem1(){Id=6,Name="fff"},
new TodoItem1(){Id=7,Name="ggg"},
new TodoItem1(){Id=8,Name="hhh"}
};
this.BindingContext = this;
}
private async void ListView_ItemSelectedAsync(object sender, SelectedItemChangedEventArgs e)
{
await Navigation.PushAsync(new Page21 {BindingContext=e.SelectedItem as TodoItem1 });
}
}
public class TodoItem1
{
public int Id { get; set; }
public string Name { get; set; }
}
Page21
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page21 : ContentPage
{
public Page21 ()
{
InitializeComponent ();
Title = "Name"; // here need show NAME - I can't display my name here
var nameEntry = new Entry();
nameEntry.SetBinding(Entry.TextProperty, "Name");
Content = new StackLayout
{
Margin = new Thickness(20),
VerticalOptions = LayoutOptions.StartAndExpand,
Children =
{
new Label { Text = "Name" },
nameEntry, // here NAME is shown
}
};
}
private void btn1_Clicked(object sender, EventArgs e)
{
var data = this.BindingContext as TodoItem1;
Console.WriteLine(data.Id);
Console.WriteLine(data.Name);
}
}