2017-12-03 300 views
0

我有這樣如何從結構寫入Xaml頁面文本框的Int值?

struct struValues 
{ 
public int a; 
} 

我想將它寫在XAML頁面中的textcell的結構。我該怎麼做? 我試過

{Bind struValues.a} 

但它沒有奏效。

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:i18n="clr-namespace:test_Trade.Localization;assembly=test_Trade" 
      xmlns:puser="clr-namespace:test_Trade.Classes" 
      x:Class="test_Trade.Views.Durumpg"> 
    <ContentPage.Content> 
     <TableView Intent="Form"> 
      <TableRoot> 
       <TableSection Title="{i18n:TranslateExtension Text=Stats}"> 
        <ImageCell ImageSource="Euro.png" Detail="{Here Should be money}" x:Name="imgCelleuro"/> 

       </TableSection> 
      </TableRoot> 
     </TableView> 
    </ContentPage.Content> 
</ContentPage> 
+0

它怎麼不工作?嘗試包含重現問題所需的最短代碼。請參閱:[mcve] –

+0

我不知道,文本將爲空。我應該做一些像xlmns:? –

+0

將XAML的簡化版添加到問題中,足以重現問題。 –

回答

0

以下是我將如何將TextCell「Text」屬性綁定到結構類型實例的int屬性的MVVM示例。 我對重要的文章發表了評論。

視覺結果應該是一個表格視圖,其中一個標題爲「Cool Struct Section」的部分和一個Text Cell作爲子標題,顯示文本「123」,即結構體當前值。

XAML頁面內容:

<ContentPage.Content> 
     <TableView Intent="Settings"> 
      <TableRoot> 
       <TableSection Title="{Binding TableSectionTitle}"> 
        <TextCell Text="{Binding StruValues.A}" /> 
       </TableSection> 
      </TableRoot> 
     </TableView> 
</ContentPage.Content> 

C#頁後面的代碼:

using MVVMExample.ViewModel; 
using Xamarin.Forms; 
using Xamarin.Forms.Xaml; 

namespace MVVMExample 
{ 
    [XamlCompilation(XamlCompilationOptions.Compile)] 
    public partial class TableViewPage : ContentPage 
    { 
     public TableViewPage() 
     { 
      InitializeComponent(); 
      BindingContext = new TableViewPageVM(); //Assing the ViewModel to the binding context! 
     } 
    } 
} 

C#視圖模型(也的BindingContext頁面的)

using MVVMExample.Utils; 

namespace MVVMExample.ViewModels 
{ 
    public class TableViewPageVM : BindableBase 
    { 
     //Simple text to bind to the TableSection Title property 
     private string tableSectionTitle; 
     public string TableSectionTitle { get { return tableSectionTitle; } set { SetProperty(ref tableSectionTitle, value); } } 

     //Property that will hold our struValues instance. The TextCell "Text" Property will be bound to the A property of this instance. 
     //The A property exposes the value of the actual "a" property of the facades struct instance 
     private struValuesFacade _struValues; 
     public struValuesFacade StruValues { get { return _struValues; } set { SetProperty(ref _struValues, value); } } 

     public TableViewPageVM() 
     { 
      TableSectionTitle = "Cool Struct Section"; //Set the title text 
      StruValues = new struValuesFacade(123);  //Create an instance of our facade 
     } 

     /// <summary> 
     /// A "facade" of the actual struct, that exposes the "a" property of the struct instance 
     /// Also holds the instances of the struct 
     /// </summary> 
     public class struValuesFacade : BindableBase 
     { 
      struValues origin; 
      public int A 
      { 
       get { return origin.a; } 
       set 
       { 
        SetProperty(ref origin.a, value); 
       } 
      } 

      public struValuesFacade(int value) 
      { 
       origin = new struValues() { a = value }; 
      } 
     } 

     /// <summary> 
     /// Your beloved struct 
     /// </summary> 
     struct struValues 
     { 
      public int a; 
     } 
    } 
} 

C# 「BindableBase」 級,從INotifyPropertyChanged的繼承(學分msdn.microsoft.com) (強制更新視圖時性能在MVVM環境改變)

using System.ComponentModel; 
using System.Runtime.CompilerServices; 

namespace MVVMTest.Utils 
{ 
    public class BindableBase : INotifyPropertyChanged 
    { 
     /// 
     /// Multicast event for property change notifications. 
     /// 
     public event PropertyChangedEventHandler PropertyChanged; 

     /// 
     /// Checks if a property already matches a desired value. Sets the property and 
     /// notifies listeners only when necessary. 
     /// 
     ///Type of the property. 
     ///Reference to a property with both getter and setter. 
     ///Desired value for the property. 
     ///Name of the property used to notify listeners. This 
     /// value is optional and can be provided automatically when invoked from compilers that 
     /// support CallerMemberName. 
     ///True if the value was changed, false if the existing value matched the 
     /// desired value. 
     protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null) 
     { 
      if (object.Equals(storage, value)) return false; 

      storage = value; 
      this.OnPropertyChanged(propertyName); 
      return true; 
     } 

     /// 
     /// Notifies listeners that a property value has changed. 
     /// 
     ///Name of the property used to notify listeners. This 
     /// value is optional and can be provided automatically when invoked from compilers 
     /// that support . 
     protected void OnPropertyChanged([CallerMemberName] string propertyName = null) 
     { 
      this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 
相關問題