以下是我将如何将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));
}
}
}
它怎么不工作?尝试包含重现问题所需的最短代码。请参阅:[mcve] –
我不知道,文本将为空。我应该做一些像xlmns:? –
将XAML的简化版添加到问题中,足以重现问题。 –