简体   繁体   English

如何在 Xamarin.Z64502425319129281C3683CAE8A8 中的 XAML 中使用 C# 变量的值?

[英]How to use value of a C# variable in XAML in Xamarin.Forms?

I am a newbie in Xamarin.forms as well as in C#, I am creating a user account page with Xaml named userac.xaml, in which there will be a label saying, "Hello, [name of user]" and their is a string variable in userac.xaml.cs which contains the name of user which will be displayed in userac.xaml page as "Hello, [name of user]", but I can't figure out how to do so. I am a newbie in Xamarin.forms as well as in C#, I am creating a user account page with Xaml named userac.xaml, in which there will be a label saying, "Hello, [name of user]" and their is a string variable在 userac.xaml.cs 中包含用户名,该用户名将在 userac.xaml 页面中显示为“你好,[用户名]”,但我不知道该怎么做。 Please Help.请帮忙。

I have attached the code:-我附上了代码:-

    <StackLayout>
        <Label Text=""
            VerticalOptions="CenterAndExpand" 
            HorizontalOptions="CenterAndExpand" />
    </StackLayout>

userac.xaml.cs:- userac.xaml.cs:-

public partial class userac : ContentPage
{
    public Contracts()
    {
        InitializeComponent();
    }
}

You should use data binding.您应该使用数据绑定。
Also, as you said you are a newbie to the C# language, so if you are not familiar with C# Properties declaration you can see in the example (the get() and set() methods inside the code) please read this .另外,正如您所说,您是 C# 语言的新手,因此,如果您不熟悉C#属性声明,您可以在示例中看到(代码中的get()set()方法),请阅读

Please see comments inside the example based on your code:请根据您的代码查看示例中的注释:

.XAML: .XAML:

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

.CS: 。CS:

public partial class userac : ContentPage
{
    private string _TheUserName = "Some User Name";
    public string TheUserName
    {
        get
        {
            return "Hello " + _TheUserName;
        }
        set
        {
            _TheUserName = value;
            // read here about OnPropertyChanged built-in method https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.bindableobject.onpropertychanged?view=xamarin-forms
            OnPropertyChanged("TheUserName");
        }
    }

    public userac()
    {
        // Note: binding context can also to another view model class of this view, but here we will use the class of this specific view
        // So, you can also do something like that:
        // BindingContext = new useracViewModel() 
        BindingContext = this;
        // you can also change the default value of _TheUserName by getting it from database, xml file etc...
        TheUserName = GetCurrentUserName();

        InitializeComponent();
    }

    private string GetCurrentUserName()
    {
        // ...
        // do things to retrieve the user name...
        // ...
        return "John Doe";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM