[英]Xamarin label binding
So I've recently just re-worked my larger app project structure to MVVM, however, I'm trying to do simple label binding and I cannot get anything to work at all. 因此,最近我刚刚将较大的应用程序项目结构重新构建为MVVM,但是,我尝试进行简单的标签绑定,因此根本无法工作。 I'm probably just being somewhat stupid and missing an obvious thing here but I cannot figure this out.
我可能只是有点愚蠢,在这里错过了明显的事情,但我无法弄清楚。 I'm not currently trying to do this in an ObservableCollection because I can't even get the basics to work currently.
我目前不在ObservableCollection中尝试执行此操作,因为我什至无法使基础知识正常工作。 Just a simple Label.
只是一个简单的标签。
My TaskModel class contains 我的TaskModel类包含
public class TaskModel
{
public string Title { get; set; }
}
My MainViewModel contains 我的MainViewModel包含
public class MainViewModel
{
TaskModel task = new TaskModel
{
Title = "Hello"
};
}
My MainPage Xaml contains 我的MainPage Xaml包含
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Testing"
x:Class="Testing.MainPage"
xmlns:bc="clr-namespace:Testing.ViewModels">
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness"
iOS="0, 20, 0, 0"
Android="0, 40, 0, 0">
</OnPlatform>
</ContentPage.Padding>
<StackLayout BindingContext="{x:Reference Slider}" HorizontalOptions="Center" VerticalOptions="Center">
<BoxView Color="Green" Opacity="{Binding Value}"></BoxView>
<Label BindingContext="{x:Reference Slider}" Text="{Binding
Value,
StringFormat='Value is {0:F2}'}"
Opacity="{Binding Value}">
</Label>
<Slider x:Name="Slider" ></Slider>
<Label Text="{Binding TaskModel.Hello}"></Label>
</StackLayout>
And then my MainPage.Xaml.CS contains 然后我的MainPage.Xaml.CS包含
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainViewModel();
}
}
So as you can see, nothing too special. 如您所见,没什么特别的。 Feeling a bit stupid that I can't work this out and any assistance on this matter would be greatly appreciated!
觉得我无法解决这个问题有点愚蠢,对此事的任何帮助将不胜感激!
EDIT: 编辑:
Ignore the extra XAML, the XAML above the label at the base is just me testing other binding within the same XAML page, which works, just trying to get it to bind outside of the page if you get what I'm saying. 忽略多余的XAML,在底部标签上方的XAML只是我测试同一XAML页面内的其他绑定,这种方法有效,只是在您理解我的意思的情况下,尝试使其绑定到页面之外。
Change the 2nd last line of your xaml: Binding TaskModel.Hello
to Binding TaskModel.Title
. 将xaml的第二行:
Binding TaskModel.Hello
为Binding TaskModel.Title
。
If this won't fix it, implement INPC
in your TaskModel
class and call it in the setter of the Title
property 如果这不能解决问题,
INPC
在TaskModel
类中实现INPC
并在Title
属性的设置器中调用它
<StackLayout BindingContext="{x:Reference Slider}"
public class MainViewModel: INotifyPropertyChanged { MainViewModel() { _task = new TaskModel() { Title = "Hello" }; } TaskModel _task; public TaskModel TaskModel { get=>_task; set { if (value!=_task) { _task = value; NotifyPropertyChanged(nameof(this.TaskModel)); } } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }
<Label Text="{Binding TaskModel.Title}"></Label>
Implement INPC in TaskModel 在TaskModel中实现INPC
public class TaskModel : INotifyPropertyChanged { public string Title { get=>_title; set=> { if (value!=_title) { value=title; OnPropertyChanged(nameof(Title)); } } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.