ControlTemplateで定義されたバインディングを、自分のモデルに対して機能させるのに問題があります。Xamarinフォーム - ControlTemplateへのバインド
以下のControlTemplateで、TemplateBindingを使用してCount(オリーブラベル)というプロパティにバインドしています。私はprescribed by this articleとしてParent.Countを使用していますが、の値はありません。Parent.Countももあります。が動作しています。
次のページはのControlTemplateを使用しています。私のViewModelが動作することを証明するために、Countプロパティにバインドされた灰色のLabelがあります。
結果の画面に注目してください。灰色のラベルにはCountプロパティが表示されています。 ControlTemplateのオリーブラベルに何も表示されていません。私はControlTemplateの中でラベルを作ることができますどのように
のViewModelからCountプロパティを表示しますか?
ビューモデル
namespace SimpleApp
{
public class MainViewModel : INotifyPropertyChanged
{
public MainViewModel()
{
_count = 10;
Uptick = new Command(() => { Count++; });
}
private int _count;
public int Count
{
get { return _count; }
set
{
_count = value;
OnPropertyChanged("Count");
}
}
public ICommand Uptick { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
XAML
<?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:local="clr-namespace:SimpleApp"
x:Class="SimpleApp.MainPage"
ControlTemplate="{StaticResource ParentPage}">
<StackLayout>
<Button Command="{Binding Uptick}" Text="Increment Count" />
<Label Text="{Binding Count}" BackgroundColor="Gray" />
</StackLayout>
</ContentPage>
BindingContextをここでMainViewModelに設定されている
お知らせ背後にあるコード。私は独自のViewModelを使用する必要があります。
namespace SimpleApp
{
public partial class MainPage : ContentPage
{
public MainPage()
{
BindingContext = new MainViewModel();
InitializeComponent();
}
}
}
コントロールテンプレートあなたのControlTemplateで
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SimpleApp.App">
<Application.Resources>
<ResourceDictionary>
<ControlTemplate x:Key="ParentPage">
<StackLayout>
<Label Text="{TemplateBinding Parent.Count}" BackgroundColor="Olive" />
<ContentPresenter />
</StackLayout>
</ControlTemplate>
</ResourceDictionary>
</Application.Resources>
</Application>
にする必要がありますか? – Tony
私はすでにそれを試みましたが、どちらもうまくいきません。 –
また、NuGetsパッケージとXamarin自体のアップデートを確認しますか?私は自分のPCを再インストールしましたが、ここにはXamarinはいません。私はここでテストしたいと思います。 – Tony