简体   繁体   English

Xamarin - 如何通过 shell 导航传递参数

[英]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.

相关问题 如何在备用 shell rdp 中将参数传递给 exe - How to pass parameters to exe in alternate shell rdp 如何在顶部导航中没有返回按钮的情况下在 Xamarin Shell 中切换页面 - How to switch pages in the Xamarin Shell without a back button in the top navigation 如何在Xamarin Android中为Tuple中的转换器传递xml中的两个参数 - How to pass two parameters in xml for a converter in for a Tuple in xamarin android shell 中的 Xamarin Forms 导航页面 - Xamarin Forms Navigation page inside shell 如何将文件路径和参数传递给shell.shellExecute命令 - How to pass filepath and parameters to shell.shellExecute command 如何在顶部导航栏上显示标题名称,而不是在xamarin窗体外壳中显示“汉堡包”图标 - How to show Title Name on Top Navigation bar instead of Hamburger icon in xamarin forms shell Xamarin.Forms Shell 如何在路线导航中向字符串注入多个不同的值 - Xamarin.Forms Shell how to inject multiple and different values ​to string in route navigation Xamarin Shell:将多个arguments传递到另一个页面 - Xamarin Shell : Pass multiple arguments to another page 我可以将参数传递给Xamarin中的Clicked事件吗? - Can I pass parameters to a Clicked event in Xamarin? 将参数传递到Tap Gesture Xamarin表单 - Pass Parameters to Tap Gesture Xamarin Forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM