简体   繁体   中英

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. 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. Just a simple Label.

My TaskModel class contains

public class TaskModel
{
    public string Title { get; set; }
}

My MainViewModel contains

public class MainViewModel
{
    TaskModel task = new TaskModel
    {
        Title = "Hello"
    };
}

My MainPage Xaml contains

<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

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.

Change the 2nd last line of your xaml: Binding TaskModel.Hello to 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

  1. Remove BindingContext from the following xaml, since context spread to control's children. Hence you change Label's context to slider control. Binding will not work if it points to incorrect context.

<StackLayout BindingContext="{x:Reference Slider}"

  1. Bindings does not work with private properties and you have private field in your ViewModel. Implement INPC:
 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)); } } 
  1. <Label Text="{Binding TaskModel.Title}"></Label>

  2. Implement INPC in TaskModel

 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)); } } 

For more, please visit docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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