【问题标题】:Xamarin Forms Binding Page Title from Model模型中的 Xamarin 表单绑定页面标题
【发布时间】:2021-12-13 11:56:22
【问题描述】:

有人知道如何从模型中绑定页面标题吗?我想将 Name 属性设置为 Title Page 下面是我的代码

Xaml

<ContentPage BackgroundColor="White"                           
         xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="app.MainMenuPage"
         NavigationPage.HasBackButton="False">
<NavigationPage.TitleView>
    <StackLayout>
        <Label Text="{Binding Name}"/>
    </StackLayout>
</NavigationPage.TitleView>

我的模特

public class EmployeeDetails
{        
    public string PersonnelNumber { get; set; }        
    public string PrimaryContactEmail { get; set; }
    public string Name { get; set; }
}

【问题讨论】:

  • 应该可以。 BindingContext 分配了吗?
  • 当我悬停绑定名称时,我收到警告/错误 No DataContext found for Binding Name
  • 你没有回答问题
  • 可能这就是我不知道如何分配绑定上下文的原因我对 xamarin 不熟悉如何做到这一点?
  • this.BindingContext = new MyViewModel();

标签: c# xamarin.forms mobile


【解决方案1】:

如果我们想绑定一个模型并将模型的数据显示到我们的页面,我们需要将模型设置为我们页面的BindingContext。通常,我们通常会为我们的页面创建一个 ViewModel。

从文档ViewModel 中,我们知道:

视图模型实现视图的属性和命令 数据可以绑定到,并通过任何方式通知视图任何状态变化 更改通知事件。视图的属性和命令 模型提供定义 UI 提供的功能。

我创建了一个简单的demo,可以参考以下代码:

1.创建 ViewModel (TestViewModel.cs):

在这个 ViewModel 中,我们可以初始化我们的数据(employee)。

 public  class TestViewModel
    {

        public EmployeeDetails employee { get; set; }

        public TestViewModel() {
            employee = new EmployeeDetails { PersonnelNumber = "01", Name = "Jack", PrimaryContactEmail = "test123@gmail.com" };
        }
    }

2.在OurPage.xaml.cs中

我们可以设置BindingContext

public partial class MainPage : ContentPage
{
    TestViewModel viewModel;
    public MainPage()
    {
        InitializeComponent();

        viewModel = new TestViewModel();

        this.BindingContext = viewModel;
    }
}

3.在OurPage.xaml

我们可以这样显示我们的数据(&lt;Label Text="{Binding employee.Name}"/&gt;):

<?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="FormApp1214.MainPage">

    <NavigationPage.TitleView>
        <StackLayout>
            <Label Text="{Binding employee.Name}"/>
        </StackLayout>
    </NavigationPage.TitleView>

    <StackLayout>

    </StackLayout>

</ContentPage>

【讨论】:

    【解决方案2】:

    您可以通过您的视图模型来完成,使用 bindingContext 将您的视图模型分配给您的视图,将其放入视图的构造函数中 BindingContext = new TEstViewModel();

    TEstViewModel 应该是您的 viewModel 的名称。

    在您的视图模型中,您必须将模型作为公共属性:

    public EmployeeDetails Detail { get; set; }
    

    然后在你的 XAML 视图中你可以输入Detail.Name

    <ContentPage BackgroundColor="White"                           
             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="app.MainMenuPage"
              Title="{Binding Detail.Name}"
             NavigationPage.HasBackButton="False">
    

    【讨论】:

      猜你喜欢
      • 2018-04-09
      • 1970-01-01
      • 2016-12-17
      • 2018-10-20
      • 1970-01-01
      • 2020-07-02
      • 2017-11-23
      • 2016-03-09
      • 1970-01-01
      相关资源
      最近更新 更多