我是Xamarin的初学者,我试图使用QueryProperty将值从一个页面传递到另一个页面,但是我总是得到空值。
以下是值的来源页面:
<StackLayout>
<Button Text="Pass" Command="{Binding passCommand}"></Button>
</StackLayout>
后面的代码:
public Page()
{
InitializeComponent();
passCommand = new Command(passFunc);
BindingContext = this;
}
public ICommand passCommand { get; }
private async void passFunc()
{
string str = "Hello";
await Shell.Current.GoToAsync($"{nameof(Page3)}?str={str}");
}
下面是接收页面:
<StackLayout>
<Label Text="{Binding str}"/>
</StackLayout>
后面的代码:
[QueryProperty(nameof(str), nameof(str))]
public partial class Page3 : ContentPage
{
public Page3()
{
InitializeComponent();
BindingContext = this;
showdisp();
}
public string str { set; get; }
public async void showdisp()
{
await App.Current.MainPage.DisplayAlert("Hello", str, "OK");
}
}
传递的值应该放在标签和弹出窗口显示警报中。当我试图放置断点时,str值仍然为空。在页面之间导航是好的。
有人能指出错误在哪里吗T_T提前谢谢。
1条答案
按热度按时间ntjbwcob1#
您的属性“str”需要引发PropertyChanged事件,以便绑定更新值:
但是,由于参数只在Page3构建完成后设置,所以showdisp()方法不会有正确的值,需要稍后调用。
您还应该考虑使用ViewModel并应用MVVM。