简体   繁体   English

Xamarin标签绑定

[英]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.HelloBinding TaskModel.Title

If this won't fix it, implement INPC in your TaskModel class and call it in the setter of the Title property 如果这不能解决问题, INPCTaskModel类中实现INPC并在Title属性的设置器中调用它

  1. Remove BindingContext from the following xaml, since context spread to control's children. 从上下文xaml中删除BindingContext,因为上下文会传播到控件的子级。 Hence you change Label's context to slider control. 因此,将Label的上下文更改为滑块控件。 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. 绑定不适用于私有属性,并且ViewModel中有私有字段。 Implement INPC: 实施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 在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)); } } 

For more, please visit docs . 有关更多信息,请访问docs

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

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