Set BindingContext to ViewModel in XAML on Xamarin.Forms

前端 未结 1 468
心在旅途
心在旅途 2020-11-29 09:29

I want to develop a simple project with Xamarin.Form and MVVM. In my solution (named XamarinPOC) i have (in addition to standard Xamarin.Forms projects) one separate project

相关标签:
1条回答
  • 2020-11-29 09:55

    To bind the view to the viewmodel from Xaml in your case do it like this

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:viewModels="clr-namespace:XamarinPOC.ViewModel; assembly=XamarinPOC.ViewModel"
                 x:Class="XamarinPOC.Summary"
                 Title="Summary List">
      <ContentPage.BindingContext>
        <viewModels:SummaryViewModel/>
      </ContentPage.BindingContext>
      <StackLayout>
        <Label Text="{Binding test}"/>
      </StackLayout>
    </ContentPage>
    

    One side note I noticed is with naming conventions, it is better to put all your ViewModels, even if it is only one viewModel, inside a folder named "ViewModels" So the namespace in your case would be XamarinPOC.ViewModels

    0 讨论(0)
提交回复
热议问题