2017-04-22 51 views
1

我在.xmal文件中使用了一個標籤。以.xmal格式在.xmal和.cs之間進行數據訪問

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     x:Class="ListDemo.TableView"> 

<StackLayout> 
    <Label Text="Above TableView"></Label> 
    <TableView> 
     <TableView.Root> 
      <TableSection Title="Test"> 
       <EntryCell Label="EntryCell"></EntryCell> 
       <TextCell Text="Test" Detail="Text Detail"></TextCell> 
       <ViewCell> 
        <ViewCell.View> 
         <StackLayout Orientation="Horizontal" > 
          <BoxView Color="Red"></BoxView> 
          <StackLayout> 
           <Label Text="{Binding Receivename}"></Label> 
           <Label Text="News URL 1"></Label> 
          </StackLayout> 
          <BoxView x:Name="boxView" Color="Blue" ></BoxView> 
         </StackLayout> 
        </ViewCell.View> 
       </ViewCell> 
      </TableSection> 
     </TableView.Root> 
    </TableView> 
</StackLayout> 

我想設置從cs文件的標籤數據。

namespace ListDemo 
{ 
    [XamlCompilation(XamlCompilationOptions.Compile)] 
    public partial class TableView : ContentPage 
    { 
     public TableView() 
     { 
      InitializeComponent(); 

      public string Receivename = "Hello"; 
     } 
    } 
} 

請讓我知道,我怎麼可以設置標籤的動態數據。在.cs文件中寫什麼?

在此先感謝。

回答

1

首先,您只能綁定到屬性。所以你需要:

public string Recievename { get; set; } 

第二,你是在構造函數中設置這個數據,當它應該在實際的類的範圍內。

但是,您可以在構造函數中設置該屬性的值。只是沒有在那裏定義它。每個請求

更新:

[XamlCompilation(XamlCompilationOptions.Compile)] 
public partial class TableView : ContentPage 
{ 
    public string Receivename { get; set; } 

    public TableView() 
    { 
     InitializeComponent(); 
     BindingContext = this; //Need to set data context as well, if not defined in XAML 
     Receivename = "Hello"; 
    } 
} 

我也建議你看起來更加有約束力的,物業通知等上xamarin這個博客應該給你一個提示:https://blog.xamarin.com/introduction-to-data-binding/

+0

我是很新的xamarin。你能爲我編碼嗎?我很困惑,在哪裏寫和寫什麼在這裏設置數據 – Myaaoonn

+0

謝謝..但它沒有顯示我「你好」在設備中。 :(我錯過了什麼.. – Myaaoonn

+0

這個堆棧面板和標籤是否存在於'TableView的XAML中?也許會發布更多的XAMLs內容 – Toskr

相關問題