【问题标题】:Xamarin Forms - TabbedPage - Pass ParameterXamarin 表单 - TabbedPage - 传递参数
【发布时间】:2021-03-21 00:57:54
【问题描述】:

我有一个这样的 TappedPage:

<?xml version="1.0" encoding="UTF-8"?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="using:MyApp" x:Class="MyApp.AccountPatientPage" Title="Account">
    <local:PatientConditions IconImageSource="user.png" />
    <local:PatientPainKillers IconImageSource="user.png"  />
    <local:PatientPainStory IconImageSource="user.png"  />
</TabbedPage>

下面是代码:

public partial class PatientProfilePage : TabbedPage
    {
        public PatientProfilePage(string username)
        {
            InitializeComponent();
        }
    }

我的问题是如何将字符串用户名传递给其他页面?这是一个例子。

public partial class PatientConditions : ContentPage
    {
        public PatientConditions(string username)
        {
            InitializeComponent();
        }
    }

【问题讨论】:

  • 如果这是一个在整个应用程序中保持一致和共享的值,那么在 App 对象上创建一个在所有页面上都可见的属性可能更有意义
  • 不是,只是这些页面。
  • 用代码而不是 XAML 创建页面。或者为内容页面将继承的标签页创建一个 VM
  • 你能给我看看这个@Jason的例子吗

标签: c# xamarin.forms tabbedpage


【解决方案1】:

除非您另行指定,否则您的页面将继承其父级的 BindingContext

// some custom class that contains properties for username and anything 
// else you need
MyViewModel vm;

public PatientProfilePage(string username)
{
    InitializeComponent();

    this.BindingContext = vm = new MyViewModel(username);
}

【讨论】:

    猜你喜欢
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多