【问题标题】:Xamarin forms binding label get and setXamarin 表单绑定标签获取和设置
【发布时间】:2019-01-09 06:05:39
【问题描述】:

我将我的项目从 MVVM Cross 迁移到 Xamarin Forms,并且我试图让标签的绑定属性出现在我的 Web 服务中,但它目前无法正常工作,就像 MVVM 一样。 当我运行该应用程序时,它在日志上没有显示任何错误,但它也没有显示我的任何属性,当涉及到我绑定的属性时,它只是空白,比如“标题”。

这是我的 XAML(表单)。

<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="Test.Core.Pages.DeliveryPage">
    <ContentPage.Content Title="General" Icon="">
        <ScrollView Orientation="Vertical">
            <StackLayout HorizontalOptions="FillAndExpand">



                <Label FontSize="Large" Text="GENERAL"/>
                <Frame>
                    <Label x:Name="labelTitle" Text="{Binding Title}"/>
                </Frame>

这是我的 CS(表单)

TestResponse test;



       private string _title;
        public string Title
        {
            get { return _title; }
            set { _title = value; OnPropertyChanged(_title); }
        }

然后我这样称呼它:

Title = "Title: " + "\n" + test.Title;

这是我使用 MVVM 的时候:

 private string _title;
    public string Title 
    {
        get => _title; 
        set => SetProperty(ref _title, value);
    }

【问题讨论】:

  • 有什么问题?
  • @Arvindraja 那里,我已经更新了,我的错。
  • Title = "Title: " + "\n" + delivery.Title;这里是设置页面标题还是标签文字?
  • 标签文本,它将抓取我的网络服务上的文本
  • 你的 ViewModel 是否实现了INotifyPropertyChangedset { _title = value; OnPropertyChanged(_title); } 应该是set { _title = value; OnPropertyChanged(nameof(Title)); }

标签: c# xamarin.forms mvvmcross


【解决方案1】:

问题在于您将页面标题(不是标签文本)设置如下

Title = "Title: " + "\n" + test.Title;

相反,您应该设置标签文本。你的标签是x:Name="labelTitle" 所以尝试如下设置标签文本

labelTitle.Text= "Title: " + "\n" + delivery.Title;

【讨论】:

    【解决方案2】:

    在视图模型中,您需要在 OnPropertyChanged() 方法中设置公共变量,如下所示

    private string _title;
    public string Title
    {
         get { return _title; }
         set { _title = value; **OnPropertyChanged(Title)**; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-03
      • 2018-11-15
      • 2019-03-19
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      相关资源
      最近更新 更多