는 여기에 내가 구조체 형식의 인스턴스를 int 속성에 TextCell "텍스트"속성을 결합 할 방법의 MVVM의 예입니다. 중요한 줄에 주석을 달았습니다.
시각적 결과는 하나의 섹션이 "Cool Struct Section"으로 표시되고 하나의 텍스트 셀이 자식으로 표시되어 구조체 현재 값인 "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#의 ViewModel (페이지도 BindingContext를) :
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의 간단한 버전을 질문에 추가하면 문제를 재현 할 수 있습니다. –