C# xamarin格式的.xmal和.cs之间的数据访问

C# xamarin格式的.xmal和.cs之间的数据访问,c#,xamarin,binding,xamarin.forms,C#,Xamarin,Binding,Xamarin.forms,我在.xmal文件中获取了一个标签 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ListDemo.TableView"> <StackLayout> <Label Text="Above TableView"><

我在.xmal文件中获取了一个标签

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="ListDemo.TableView">

<StackLayout>
    <Label Text="Above TableView"></Label>
    <TableView>
        <TableView.Root>
            <TableSection Title="Test">
                <EntryCell Label="EntryCell"></EntryCell>
                <TextCell Text="Test" Detail="Text Detail"></TextCell>
                <ViewCell>
                    <ViewCell.View>
                        <StackLayout Orientation="Horizontal" >
                            <BoxView Color="Red"></BoxView>
                            <StackLayout>
                                <Label Text="{Binding Receivename}"></Label>
                                <Label Text="News URL 1"></Label>
                            </StackLayout>
                            <BoxView x:Name="boxView" Color="Blue" ></BoxView>
                        </StackLayout>
                    </ViewCell.View>
                </ViewCell>
            </TableSection>
        </TableView.Root>
    </TableView>
</StackLayout>
请让我知道,如何设置标签的动态数据。在.cs文件中写入的内容


提前谢谢

首先,您只能绑定到属性。因此,您需要:

public string Recievename { get; set; }
第二,在构造函数中设置此数据,此时它应该在实际类的范围内

但是,您可以在构造函数中设置属性的值。只是不要在那里定义它

按请求更新:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TableView : ContentPage
{
    public string Receivename { get; set; }

    public TableView()
    {
        InitializeComponent();
        BindingContext = this; //Need to set data context as well, if not defined in XAML
        Receivename = "Hello";
    }
}

我还建议你多看看绑定、属性通知等。这个关于xamarin的博客应该给你一个提示:

我对xamarin很陌生。你能帮我把这个编码吗?我很困惑,在哪里写,在这里写什么来设置数据?堆栈面板和标签是否存在于“TableView”的XAML中?也许可以发布更多你的XAMLs内容。是的,我试过了。这说明我说得不对。“datacontext在当前上下文中不存在”对不起,我来自WPF背景,所以看起来有些东西是不同的。Xamarin似乎使用“BindingContext”而不是“DataContext”来形成表单
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TableView : ContentPage
{
    public string Receivename { get; set; }

    public TableView()
    {
        InitializeComponent();
        BindingContext = this; //Need to set data context as well, if not defined in XAML
        Receivename = "Hello";
    }
}