[英]Xamarin - How to pass parameters with shell navigation
I have been searching everywhere how to achieve passing parameters with shell navigation but I can't get this to work.我一直在到处寻找如何通过 shell 导航实现传递参数,但我无法让它工作。
This is the line where I navigate to my InvoicePage
这是我导航到我的InvoicePage
的行
await Shell.Current.GoToAsync($"//main/InvoicePage?total ={ lblTotalCart.Text.Trim('€')}");
What I want is to be able to retrieve the value on my View Model but I'm trying at least on my ContentPage and it is not working.我想要的是能够检索我的视图模型上的值,但我至少在我的 ContentPage 上尝试并且它不起作用。
[XamlCompilation(XamlCompilationOptions.Compile)]
[QueryProperty("Total", "total")]
public partial class InvoicePage : ContentPage
{
string total;
public string Total
{
set
{
total = Uri.UnescapeDataString(value ?? string.Empty);
OnPropertyChanged();
}
get
{
return total;
}
}
public InvoicePage()
{
InitializeComponent();
BindingContext = new InvoiceViewModel(Total);
}
Tried the same on my ViewModel and it is also not working.在我的 ViewModel 上尝试了同样的方法,它也无法正常工作。
Here is my View.这是我的观点。 I just have a Label to test it我只有一个标签来测试它
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewmodels="clr-namespace:AppCrijoya.ViewModels"
x:Class="AppCrijoya.Views.InvoicePage"
x:DataType="viewmodels:InvoiceViewModel">
<ContentPage.Content>
<StackLayout>
<Label Text="{Binding Total}"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
EDIT编辑
I tried doing this on my ViewModel but it still doesn't return anything我尝试在我的 ViewModel 上执行此操作,但它仍然没有返回任何内容
[QueryProperty("Total", "Total")]
public class InvoiceViewModel : BindableObject
{
string _total;
public string Total
{
set
{
try
{
_total = Uri.UnescapeDataString(value);
OnPropertyChanged();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
get { return _total; }
}
}
Then on my InvoicePage
I have this然后在我的InvoicePage
我有这个
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class InvoicePage : ContentPage
{
public InvoicePage()
{
InitializeComponent();
BindingContext = new InvoiceViewModel();
}
}
I don't know if it is because of the route that is faling.我不知道是不是因为路线正在下降。 Here is my AppShell
这是我的AppShell
<TabBar Route="main">
<Tab Route="CategoryPage" Title="Explorar" Icon="tab_feed.png">
<ShellContent ContentTemplate="{DataTemplate local:CategoryPage}" />
</Tab>
<Tab Route="CartPage" Title="Carrito" Icon="tab_feed.png">
<ShellContent ContentTemplate="{DataTemplate local:CartPage}" />
</Tab>
<Tab Route="InvoicePage" Title="Factura" Icon="tab_feed.png">
<ShellContent ContentTemplate="{DataTemplate local:InvoicePage}" />
</Tab>
</TabBar>
And here are the routes I registered这是我注册的路线
public AppShell()
{
InitializeComponent();
Routing.RegisterRoute(nameof(CategoryPage), typeof(CategoryPage));
Routing.RegisterRoute(nameof(CartPage), typeof(CartPage));
Routing.RegisterRoute(nameof(InvoicePage), typeof(InvoicePage));
}
How can I achieve this?我怎样才能做到这一点? Please help, I don't know what else I can do.请帮忙,我不知道我还能做什么。 Thanks.谢谢。
The BindingContext
is not set at the right time in your code. BindingContext
未在您的代码中正确设置。
When we navigate to page InvoicePage
, above code is executed in the following order:当我们导航到InvoicePage
页面时,上面的代码按以下顺序执行:
1.the constructor of page InvoicePage
InvoicePage
页面的构造函数
public InvoicePage()
{
InitializeComponent();
//BindingContext = new InvoiceViewModel(Total);
}
2.get the passed data from previous page: 2.从上一页获取传递的数据:
set
{
total = Uri.UnescapeDataString(value ?? string.Empty);
// other code
}
So, we should set the BindingContext
for page InvoicePage
after we get the passed data.因此,我们应该在获得传递的数据后为页面InvoicePage
设置BindingContext
。
You can refer to the following code:您可以参考以下代码:
[XamlCompilation(XamlCompilationOptions.Compile)]
[QueryProperty("Total", "total")]
public partial class InvoicePage : ContentPage
{
string total;
public string Total
{
set
{
total = Uri.UnescapeDataString(value ?? string.Empty);
// here we can get the passed data and set the `BindingContext` for page `InvoicePage`
BindingContext = this;
// OnPropertyChanged();
}
get
{
return total;
}
}
public InvoicePage()
{
InitializeComponent();
}
// other code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.