2017-06-15 47 views
1

可以说我有一个非常基础的类,有几个属性。例如:Xaml绑定到Xamarin表格中的自定义类

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" /> 

而且我也试着设置父控件上的BindingContext:

<?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" /> 
    </StackLayout> 

</ContentPage> 

但它只是不起作用。再次

<?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> 

但是,我得到的是空白页:我也尝试在网页的BindingContext中设置anInstance:

public partial class MainPage : ContentPage 
    { 
     public MyClass anInstance; 
     public MainPage() 
     { 
      InitializeComponent(); 
      anInstance = new MyClass(); 
      BindingContext = anInstance; 
     } 
    } 

,只是在XAML端绑定到其属性。 似乎至少有一个这样的工作。 这是什么建议的方式来绑定到像这样的自定义类的属性?

编辑

将船上@jason意见,我也尝试过这样的:

public MyClass anInstance 
     { 
      get 
      { 
       return _anInstance; 
      } 
      set 
      { 
       _anInstance = value; 
      } 
     } 
     private 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 BindingContext="anInstance"> 
     <Label Text="{Binding Something}" 
       VerticalOptions="Center" 
       HorizontalOptions="Center" /> 
     <Label Text="{Binding OrOther}" 
       VerticalOptions="Center" 
       HorizontalOptions="Center" /> 
    </StackLayout> 
</ContentPage> 

但同样一个空白页.. 。

相同结果:

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> 
+0

您的第一个示例不起作用,因为“anInstance”不是属性。你最后的例子应该工作。 – Jason

+0

所显示的代码都不起作用。你的anInstance对象必须是一个属性。这意味着它必须被声明为“public MyClass anInstance {get;组; }'。 –

+0

在你的两个附加例子中,你都在XAML和后面的代码中设置了BindingContext。挑选或其他,而不是两个。它充其量是令人困惑的。我通常会发现将其置于代码背后更清晰。 – Jason

回答

1

你几乎没有。你的财产需要公开(或受保护可能有效),然后你将绑定上下文设置为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> 

如果你不想属性名称不AnInstance前缀,设置直接结合上下文的MyClass对象的实例,就像这样:

public MyClass AnInstance { get; set; } 

public MainPage() 
{ 
    InitializeComponent(); 

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

现在,您可以使用属性属于MyClass可怕ctly,如下所示:<Label Text="{Binding OrOther}" VerticalOptions="Center" HorizontalOptions="Center" />

+0

谢谢。真的很有用。但是,为什么我不能在代码隐藏中将上下文设置为this,然后在stacklayout中将BindingContext设置为AnInstance。那里面只需使用AnInstance上的字段而不需要FQ名称? –

+0

此外,它是否记载任何关于绑定只与公共领域工作?为什么如果他们都属于同一班级,为什么需要公开?只是好奇而已... –