自定义子视图Xamarin Forms的绑定上下文

时间:2017-10-29 20:09:30

标签: c# data-binding xamarin.forms

我有一个名为 MainPage.xaml 的视图,其中包含多个lessonPickView。

为了简单起见, lessonPickView.xaml 仅包含一个标签绑定到Title属性的标签。

我无法将lessonPickView绑定到MainPage.cs中的 lessonPickViewModel .cs。

MainPage.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"            
             x:Class="KanjiStudy.MainPage"
             xmlns:local="clr-namespace:KanjiStudy.View">
    <ContentPage.Content>
        <StackLayout Orientation="Vertical">
            <StackLayout Orientation="Vertical" VerticalOptions="StartAndExpand">
                <Label Margin="0,20,0,0" Text="Grade 1"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" />
                <StackLayout Margin="15,0,0,0" HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
                    <local:LessonPickView x:Name="g1l1" Margin="10,10,10,10">
                    </local:LessonPickView>
                    <local:LessonPickView x:Name="g1l2" Margin="10,10,10,10">
                    </local:LessonPickView>
                    <local:LessonPickView x:Name="g1l3" Margin="10,10,10,10">
                    </local:LessonPickView>
                    <local:LessonPickView x:Name="g1l4" Margin="10,10,10,10">
                    </local:LessonPickView>
                </StackLayout>
                <Label Margin="0,20,0,0" Text="Grade 2"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" />
                <StackLayout HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
                    <local:LessonPickView Margin="10,10,10,10">
                    </local:LessonPickView>
                    <local:LessonPickView Margin="10,10,10,10">
                    </local:LessonPickView>
                    <local:LessonPickView Margin="10,10,10,10">
                    </local:LessonPickView>
                </StackLayout>
                <StackLayout HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
                    <local:LessonPickView Margin="10,10,10,10">
                    </local:LessonPickView>
                    <local:LessonPickView Margin="10,10,10,10">
                    </local:LessonPickView>
                    <local:LessonPickView Margin="10,10,10,10">
                    </local:LessonPickView>
                </StackLayout>
            </StackLayout>
            <StackLayout Margin="0,0,0,20" Padding="20,0,20,0" VerticalOptions="End" HorizontalOptions="Center" Orientation="Horizontal">
                <Button Margin="20,0,50,20" FontSize="Large" HorizontalOptions="Start" Text="Study" >
                </Button>
                <Button Margin="50,0,20,20" FontSize="Large" Text="Test" Clicked="Button_Clicked">
                </Button>
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

MainPage.cs 如果我将下面的绑定代码移动到LessonPick.cs类,那么所有对象都将其title属性绑定到相同的字符串。但我在这堂课中需要它,所以我可以通过不同的标题。

public partial class MainPage : ContentPage
{

    public MainPage()
    {

        InitializeComponent();
        LessonPickView g1l1V = LessonView_get("g1l1");
        LessonPickView g1l2V = LessonView_get("g1l2");
        LessonPickView g1l3V = LessonView_get("g1l3");
        LessonPickView g1l4V = LessonView_get("g1l4");

        LessonPickViewModel viewModel = new LessonPickViewModel();
        g1l1V.BindingContext = viewModel;
        viewModel.title = "Hello world";

    }


    private LessonPickView LessonView_get(String name_)
    {
        LessonPickView lessonView = this.FindByName<LessonPickView>(name_);

        return lessonView;
    }

    private void Button_Clicked(object sender, EventArgs e)
    {

        DisplayAlert("Title","Hi world","Cancel");
    }
}

LessonPickView

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="KanjiStudy.View.LessonPickView">
  <ContentView.Content>
      <StackLayout>
            <Frame OutlineColor="Black">
                <Label Text="{Binding Title}" />
            </Frame>

      </StackLayout>
  </ContentView.Content>
</ContentView>

LessonPickViewModel

class LessonPickViewModel: INotifyPropertyChanged
{
    public string title;
    public string Title
    {
        get
        {
            return title;
        }
        set
        {
            if (Title != value)
            {
                Title = value;
                OnPropertyChanged("Title");
            }
        }
    }
public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
    var changed = PropertyChanged;
    if (changed != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
}

0 个答案:

没有答案