C# Xamarin在页面之间传递参数

C# Xamarin在页面之间传递参数,c#,ios,xamarin,xamarin.forms,xamarin.ios,C#,Ios,Xamarin,Xamarin.forms,Xamarin.ios,我试图在点击事件上打开一个新的页面,该页面将显示某个对象的一些详细信息。为此,我需要将对象本身或其ID传递到新页面。因此,我在细节页面的构造函数中添加了一个参数,如下所示 void onItemTapped(object sender, ItemTappedEventArgs e) { if (e.Item != null) { bool convOk = Int32.TryParse((string)e.Item, out int id); if

我试图在点击事件上打开一个新的
页面
,该页面将显示某个对象的一些详细信息。为此,我需要将对象本身或其ID传递到新页面。因此,我在细节页面的构造函数中添加了一个参数,如下所示

void onItemTapped(object sender, ItemTappedEventArgs e)
{
    if (e.Item != null)
    {
        bool convOk = Int32.TryParse((string)e.Item, out int id);
        if (convOk)
        {
            Navigation.PushAsync(new DetailPage(id));
        }
    }
}
DetailPage
有自己的
DetailViewModel
,它被设置为代码隐藏中的
BindingContext

DetailPage
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="Foo.Views.DetailPage">
    <ContentPage.Content>
        <StackLayout Orientation="Vertical">
            <Label Text="FooBar" />
            <Label Text="{Binding trackID}" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
DetailViewModel

namespace Foo.ViewModels
{
    public class DetailViewModel : BaseViewModel
    {
        // trackID prop
        int _trackID;
        int trackID 
        { 
            get { return _trackID; }
            set 
            { 
                _trackID = value;
                notifyPropertyChanged(nameof(trackID));
            } 
        }

        public TargetDetailViewModel(int tid)
        {
            trackID = tid;
        }
    }
}
但是,
DetailPage
DetailViewModel
之间的绑定似乎不起作用,页面没有显示任何内容。id本身被正确地传递到
DetailViewModel

这是由于初始化的顺序造成的吗?我假设用XAML编写的所有内容都将在
DetailPage.InitializeComponent()方法中执行?如果正确,在
DetailPage.InitializeComponent()
之前实例化
ViewModel
是否安全/正确


感谢您的任何提示。

您的
trackID
属性不是公共的

注意:如果您查看应用程序日志输出,您可以发现类似这样的绑定问题(通过字符串
绑定过滤:

未绑定的私有变量的日志示例: DetailViewModel修复程序:
您的
trackID
属性不是
public
是的,您是对的。现在它起作用了。我恨我自己。非常感谢你!我们都做了,你可以回答这个问题,所以你得到了一些荣誉!完成后,我添加了如何快速捕获这些绑定问题感谢日志消息的提示,伙计!我不知道。祝你过得愉快:)
namespace Foo.ViewModels
{
    public class DetailViewModel : BaseViewModel
    {
        // trackID prop
        int _trackID;
        int trackID 
        { 
            get { return _trackID; }
            set 
            { 
                _trackID = value;
                notifyPropertyChanged(nameof(trackID));
            } 
        }

        public TargetDetailViewModel(int tid)
        {
            trackID = tid;
        }
    }
}
Binding: 'trackID' property not found on 'XXXX.VM', target property: 'Xamarin.Forms.Label.Text'
public class DetailViewModel : BaseViewModel
{
    int _trackID;
    public int trackID;
    { 
        get { return _trackID; }
        set 
        { 
            _trackID = value;
            notifyPropertyChanged(nameof(trackID));
        } 
    }
    ~~~~
 }