简体   繁体   English

在代码隐藏中访问XAML绑定

[英]Access XAML binding in code-behind

I am working in Xamarin but I believe this applies to any UWP application using XAML. 我在Xamarin中工作,但我相信这适用于使用XAML的任何UWP应用程序。

First I have two ContentPages. 首先,我有两个ContentPages。 On the first page, I want to pass some data to the second page, so I do this as part of the navigation: 在第一页上,我想将一些数据传递到第二页,因此我将其作为导航的一部分:

    async void BuyTickets(object sender, EventArgs e)
    {
        var ticketOrderTotal = new TicketOrder
        {
            OrderTotal = lblOrderAmount.Text,
            OrderTotalList = ticketsPrices.Where(o => o.TicketQuantity > 0).ToList<Ticket>()
        };

        var paymentPage = new PaymentPage();

        paymentPage.BindingContext = ticketOrderTotal;

        await Navigation.PushAsync(paymentPage);
    }

The above works fine in XAML. 上面在XAML中工作正常。 On the second page (PaymentPage), I am able to reference the BindingContext like this, for example, and the Text property is correct: 在第二页(PaymentPage)上,例如,我能够像这样引用BindingContext,并且Text属性是正确的:

<Label x:Name="lblOrderAmount" Text="{Binding OrderTotal}" />

What I would like to do is access the "{Binding OrderTotal}" value in the C# code-behind of the second page. 我想做的是访问第二页后面C#代码中的“ {Binding OrderTotal}”值。 I found a way to do this, too, but it just does not seem optimal. 我也找到了一种方法来执行此操作,但是它似乎并不是最佳方法。 This is the kludge I have in place: 这是我所处的位置:

<Label x:Name="lblOrderAmount" Text="{Binding OrderTotal}" BindingContextChanged="GetChargeAmount" />

And this is the code-behind for the label: 这是标签的代码背后:

    public static string m_charge_amount = "";
    ...
    private void GetChargeAmount(object sender, EventArgs e)
    {
        var lbl = ((Label)sender);
        m_charge_amount = lbl.Text;
    }

So my question is this: is there a better way to do this? 所以我的问题是:还有更好的方法吗? It is particularly hard to research as XAML seems to be rooted in WPF, Silverlight, Xamarin, Windows 8, and now Windows 10 (UWP). 由于XAML似乎植根于WPF,Silverlight,Xamarin,Windows 8和现在的Windows 10(UWP)中,因此特别难以研究。 It is all over the place. 到处都是。 I am constantly fighting with the framework to do things that I think should be quite easy to do....like this. 我一直在与框架竞争,以完成我认为应该很容易做的事情。 Please help but do be nice . 请帮忙,但要很好

Thank you. 谢谢。

EDIT: 编辑:

Per @Jason's comment, you can pass an object to the page constructor, and that will work. 根据@Jason的评论,您可以将一个对象传递给页面构造函数,这将起作用。 This is what the re-worked function looks like now: 改写后的函数现在看起来像这样:

    async void BuyTickets(object sender, EventArgs e)
    {
        var ticketOrderTotal = new TicketOrder
        {
            OrderTotal = lblOrderAmount.Text,
            OrderTotalList = ticketsPrices.Where(o => o.TicketQuantity > 0).ToList<Ticket>()
        };

        var paymentPage = new PaymentPage(ticketOrderTotal);

        paymentPage.BindingContext = ticketOrderTotal;

        await Navigation.PushAsync(paymentPage);
    }

And then the result from debugging: 然后是调试的结果:

在此处输入图片说明

instead of having PageA set PageB's BindingContext , instead pass the ticketOrderTotal object as a parameter on PageB's constructor. 而不是让PageA设置PageB's BindingContext ,而是将ticketOrderTotal对象作为参数传递给PageB's构造函数。 Then PageB can set it's own BindingContext as well as keep a local reference to the ticketOrderTotal object. 然后PageB可以设置它自己的BindingContext并保留对ticketOrderTotal对象的本地引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM