在模板中绑定标签文本不起作用

发布于 2025-01-11 04:40:41 字数 1286 浏览 0 评论 0原文

我创建了一个模板,必须将变量的值分配给标签,我使用了绑定,但它不起作用。 这是cs中的代码:

AppViewModel vm = new AppViewModel();
        BindingContext = vm;
        InitializeComponent();

这是viewmodel中的代码:

public class AppViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string _utente;

    public AppViewModel()
    {
        Utente = App.UTENTE;
    }

    public string Utente
    {
        get
        {
            return _utente;
        }
        set
        {
            _utente = value;
            //OnPropertyChanged("Utente");
            //PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Utente));
            OnPropertyChanged("utente");
            //PropertyChanged(this, new PropertyChangedEventArgs("utente"));
        }
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

这是xaml中的代码:

                    <Label x:Name="utente" Padding="10, 0, 0, 0" Grid.Row="0" Grid.Column="0" Text="{Binding Utente}" FontSize="Large" TextColor="White" LineBreakMode="TailTruncation" VerticalOptions="Center" />
                    

I have created a template and I have to assign the value of a variable to a label, I use the binding, but it doesn't work.
This is the code in the cs:

AppViewModel vm = new AppViewModel();
        BindingContext = vm;
        InitializeComponent();

This is the code in the viewmodel:

public class AppViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string _utente;

    public AppViewModel()
    {
        Utente = App.UTENTE;
    }

    public string Utente
    {
        get
        {
            return _utente;
        }
        set
        {
            _utente = value;
            //OnPropertyChanged("Utente");
            //PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Utente));
            OnPropertyChanged("utente");
            //PropertyChanged(this, new PropertyChangedEventArgs("utente"));
        }
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

This is the code xaml:

                    <Label x:Name="utente" Padding="10, 0, 0, 0" Grid.Row="0" Grid.Column="0" Text="{Binding Utente}" FontSize="Large" TextColor="White" LineBreakMode="TailTruncation" VerticalOptions="Center" />
                    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡Ms空城旧梦 2025-01-18 04:40:41

您没有正确使用MVVM,请参阅我的代码片段供您参考。

1.在xaml和后面的代码:

    <StackLayout>
        <Label Text="{Binding Utente}"
            VerticalOptions="CenterAndExpand" 
            HorizontalOptions="CenterAndExpand" />
    </StackLayout>

在xaml后面:

public partial class AppViewPage : ContentPage
{

    AppViewModel vm = new AppViewModel();
    public AppViewPage()
    {
        InitializeComponent();
        BindingContext = vm;
        vm.Utente = "utente";
    }
}

2.AppViewModel:

    public class AppViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
 
        private string _utente;
        public string Utente
        {
            get { return _utente; }
            set {
                  _utente = value;
                PropertyChanged?.Invoke(this,
                   new PropertyChangedEventArgs(nameof(Utente)));
            }
        }
        public AppViewModel()
        {
        }
    }   

You are not using MVVM properly, please see my code snippets for your reference.

1.in xaml and code behind:

    <StackLayout>
        <Label Text="{Binding Utente}"
            VerticalOptions="CenterAndExpand" 
            HorizontalOptions="CenterAndExpand" />
    </StackLayout>

Behind the xaml:

public partial class AppViewPage : ContentPage
{

    AppViewModel vm = new AppViewModel();
    public AppViewPage()
    {
        InitializeComponent();
        BindingContext = vm;
        vm.Utente = "utente";
    }
}

2.AppViewModel:

    public class AppViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
 
        private string _utente;
        public string Utente
        {
            get { return _utente; }
            set {
                  _utente = value;
                PropertyChanged?.Invoke(this,
                   new PropertyChangedEventArgs(nameof(Utente)));
            }
        }
        public AppViewModel()
        {
        }
    }   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文