【问题标题】:How can I bind to a view model in C# Xamarin Forms?如何绑定到 C# Xamarin Forms 中的视图模型?
【发布时间】:2019-06-01 12:41:40
【问题描述】:

我的 C# 代码现在是这样的:

 stack.Children.Add(new FooterTemplate() { Text = Japanese.Helpers.Deck.SetIntro() });

但我想从我的 viewModel 中获取 Text 的值:

 vm.IntroFooter = Japanese.Helpers.Deck.SetIntro();

如何在 C# 中将文本绑定到 vm.IntroFooter

【问题讨论】:

  • 您是否将 FooterTemplate 添加到 StackLayout?

标签: xamarin xamarin.forms


【解决方案1】:

以下是如何在 C# 中绑定属性:

FooterTemplate ft = new FooterTemplate();
Binding binding = new Binding("IntroFooter");
ft.SetBinding(FooterTemplate.TextProperty, binding);
stack.Children.Add(ft);

我假设您正确使用INotifyPropertyChanged 接口:

...
string _introFooter;
public string IntroFooter 
{
    get
    {
        return this._introFooter;
    }
    set
    {
        if (value != this._introFooter)
        {
            this._introFooter = value;
            NotifyPropertyChanged();
        }
    }
}

【讨论】:

    猜你喜欢
    • 2020-06-08
    • 2020-06-20
    • 2017-04-25
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多