Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xaml绑定到Xamarin表单中的自定义类_Xaml_Xamarin_Data Binding_Xamarin.forms - Fatal编程技术网

Xaml绑定到Xamarin表单中的自定义类

Xaml绑定到Xamarin表单中的自定义类,xaml,xamarin,data-binding,xamarin.forms,Xaml,Xamarin,Data Binding,Xamarin.forms,假设我有一个非常基本的类,它有一些属性。例如: public class MyClass { public MyClass() { Something = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"; OrOther = "Proin dignissim, nunc non tincidunt imperdiet, magna urna

假设我有一个非常基本的类,它有一些属性。例如:

public class MyClass
    {
        public MyClass()
        {
            Something = "Lorem ipsum dolor sit amet, consectetur adipiscing elit";
            OrOther = "Proin dignissim, nunc non tincidunt imperdiet, magna urna malesuada enim";
        }
        public string Something { get; set; }
        public string OrOther { get; set; }
    }
我想在Xaml中数据绑定到这个,我该怎么做? 我已尝试直接绑定到对象,因此在我的Xaml页面代码隐藏中:

public partial class MainPage : ContentPage
    {
        public MyClass anInstance;
        public MainPage()
        {
            InitializeComponent();
            anInstance = new MyClass();
            BindingContext = this;
        }
    }
然后在我的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:DataBindingTest"
             x:Class="DataBindingTest.MainPage">

    <Label Text="{Binding anInstance.Something}" 
           VerticalOptions="Center" 
           HorizontalOptions="Center" />
只需在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:DataBindingTest"
             x:Class="DataBindingTest.MainPage">

        <Label Text="{Binding Something}" 
               VerticalOptions="Center" 
               HorizontalOptions="Center" />

</ContentPage>
以及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:DataBindingTest"
             x:Class="DataBindingTest.MainPage">
    <StackLayout BindingContext="anInstance">
        <Label Text="{Binding Something}" 
               VerticalOptions="Center" 
               HorizontalOptions="Center" />
        <Label Text="{Binding OrOther}" 
               VerticalOptions="Center" 
               HorizontalOptions="Center" />
    </StackLayout>
</ContentPage>


你就快到了。您的属性需要是公共的(或受保护的)。然后您将绑定上下文设置为
this
。这很好。作为记录,我使用以下代码:

public MyClass AnInstance { get; set; }

public MainPage()
{
    InitializeComponent();

    AnInstance = new MyClass();
    BindingContext = this;
}
现在,在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:DataBindingTest"
             x:Class="DataBindingTest.MainPage">
    <StackLayout>
        <Label Text="{Binding AnInstance.Something}" 
               VerticalOptions="Center" 
               HorizontalOptions="Center" />
        <Label Text="{Binding AnInstance.OrOther}" 
               VerticalOptions="Center" 
               HorizontalOptions="Center" />
    </StackLayout>
</ContentPage>
public MyClass AnInstance { get; set; }

public MainPage()
{
    InitializeComponent();

    AnInstance = new MyClass();
    BindingContext = AnInstance;
}

您现在可以直接使用属于
MyClass
的属性,例如:

您的第一个示例不起作用,因为“anInstance”不是属性。您的最后一个示例应该有效。显示的两个代码都无效。您的anInstance对象必须是属性。这意味着它必须像
public MyClass anInstance{get;set;}一样声明
。在您所附的两个示例中,您都在XAML和代码隐藏中设置BindingContext。选择on或其他,而不是两者。这充其量是令人困惑的。我通常发现在代码隐藏中设置BindingContext更为清晰。另外,刚刚注意到您的anInstance属性是私有的-我认为它需要是public或ProtectedTanks。真的很有用。为什么,我不能在后台代码中设置上下文,然后在StayDead上设置Buffin上下文到AnEx实例。然后,只使用一个实例上的字段而不需要FQ名称?而且,它是否被记录在绑定仅与公共字段一起工作的任何地方?为什么它们都需要公开,如果它们都是PAR的话?同一类的头饰?只是好奇而已。。。
private MyClass anInstance { get; set; }
        public MainPage()
        {
            InitializeComponent();
            anInstance = new MyClass();
            BindingContext = this;
        }
<?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:DataBindingTest"
             x:Class="DataBindingTest.MainPage">
    <StackLayout BindingContext="anInstance">
        <Label Text="{Binding Something}" 
               VerticalOptions="Center" 
               HorizontalOptions="Center" />
        <Label Text="{Binding OrOther}" 
               VerticalOptions="Center" 
               HorizontalOptions="Center" />
    </StackLayout>
</ContentPage>
public MyClass AnInstance { get; set; }

public MainPage()
{
    InitializeComponent();

    AnInstance = new MyClass();
    BindingContext = this;
}
<?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:DataBindingTest"
             x:Class="DataBindingTest.MainPage">
    <StackLayout>
        <Label Text="{Binding AnInstance.Something}" 
               VerticalOptions="Center" 
               HorizontalOptions="Center" />
        <Label Text="{Binding AnInstance.OrOther}" 
               VerticalOptions="Center" 
               HorizontalOptions="Center" />
    </StackLayout>
</ContentPage>
public MyClass AnInstance { get; set; }

public MainPage()
{
    InitializeComponent();

    AnInstance = new MyClass();
    BindingContext = AnInstance;
}