簡體   English   中英

Xamarin形成綁定標簽get和set

[英]Xamarin forms binding label get and set

我將項目從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);
    }

問題出在這里,您正在按以下方式設置頁面標題(而非標簽文本)

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

相反,您應該設置標簽文本。 您的標簽為x:Name="labelTitle"因此請嘗試如下設置標簽文本

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

在視圖模型中,您需要在OnPropertyChanged()方法中設置公共變量,如下所示

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM