本文介绍了xaml.cs 中的 Xamarin 局部变量并通过 XAML 文件打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够从 XAML 中的CurrentOrder"打印属性.

I want to be able to print properties from 'CurrentOrder' in the XAML.

这是我目前所拥有的:

// OrderPage.xaml.cs
public partial class OrderPage : ContentPage
{
    private Order _currentOrder;

    public Order CurrentOrder
    {
        get { return _currentOrder; }
    }

    public OrderPage()
    {
        InitializeComponent();

        _currentOrder = Order.DefaultOrder;

        addPin("Start", _currentOrder.PickupAddress.Latitude, _currentOrder.PickupAddress.Longitude);
        addPin("End", _currentOrder.DropoffAddress.Latitude, _currentOrder.DropoffAddress.Longitude);

        this.BindingContext = this;
    }

    public OrderPage(Order order)
    {
        InitializeComponent();

        _currentOrder = order;

        addPin("Start", _currentOrder.PickupAddress.Latitude, _currentOrder.PickupAddress.Longitude);
        addPin("End", _currentOrder.DropoffAddress.Latitude, _currentOrder.DropoffAddress.Longitude);

        Debug.WriteLine(_currentOrder.ToString());

        this.BindingContext = this;
    }
}

这是 Order 类,它与其他类有几个属性.

Here is the Order class, which has several properties with other classes.

public class Order : INotifyPropertyChanged
{
    public static Order DefaultOrder
    {
        // I have a default order return here, but in the sake of privacy, I'm removing my test addresses
    }

    // event to handle changes in the order status
    public event PropertyChangedEventHandler PropertyChanged;

    public enum Status { Preview, NeedsDriver, WaitingDriver, InTransit, NeedsSignature, Complete, Refunded }

    public string ID { get; set; }
    public string Description { get; set; }
    private Status _orderStatus;
    public Status OrderStatus {
        get
        {
            return _orderStatus;
        }
        set
        {
            _orderStatus = value;
            // tell the view that the order status has changed
            OnPropertyChanged("OrderStatus");
        }
    }
    public Contact PickupContact { get; set; }
    public Contact DropoffContact { get; set; }
    public Address PickupAddress { get; set; }
    public Address DropoffAddress { get; set; }
    public DateTime PickupTime { get; set; }
    public DateTime DropoffTime { get; set; }

    // Formatted Pickup and Dropoff Times
    public string PickupTimeFormatted
    {
        get { return PickupTime.ToString("g"); }
    }
    public string DropoffTimeFormatted
    {
        get { return DropoffTime.ToString("g"); }
    }

    public Order()
    {
    }

    // Handler to tell the view that the order status has changed
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    public override string ToString()
    {
        return string.Format("[Order: ID={0}, Description={1}, OrderStatus={2}, PickupContact={3}, DropoffContact={4}, PickupAddress={5}, DropoffAddress={6}, PickupTime={7}, DropoffTime={8}, PickupTimeFormatted={9}, DropoffTimeFormatted={10}]", ID, Description, OrderStatus, PickupContact, DropoffContact, PickupAddress, DropoffAddress, PickupTime, DropoffTime, PickupTimeFormatted, DropoffTimeFormatted);
    }
}

最后是 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"
    xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
    x:Class="Divco.OrderPage"
    Title="Order">
<ContentPage.BindingContext>
</ContentPage.BindingContext>
<ContentPage.Content>
    <StackLayout Spacing="10" x:Name="layout" VerticalOptions="FillAndExpand">
        <StackLayout>
            <maps:Map WidthRequest="320"
                      HeightRequest="150"
                      x:Name="MyMap"
                      IsShowingUser="false"
                      MapType="Street" />
        </StackLayout>
        <StackLayout Padding="20, 20, 20, 0">
            <!--<Label Content="{Binding ID, Source={StaticResource CurrentOrder}}"></Label>-->
            <Label Text="{Binding ID}"
                   TextColor="Fuchsia" />
            <Label Text="Description"
                   LineBreakMode="WordWrap" />
        </StackLayout>
        <StackLayout Padding="20, 20, 20, 0">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Label Text="Pickup"
                       Grid.Row="0"
                       Grid.Column="0"
                       Grid.ColumnSpan="2"
                       TextColor="Fuchsia"/>
                <Label Text="Top Left"
                       Grid.Row="1"
                       Grid.Column="0" />
                <Label Text="Top Right"
                       Grid.Row="1"
                       Grid.Column="1" />
                <Label Text="Dropoff"
                       Grid.Row="2"
                       Grid.Column="0"
                       Grid.ColumnSpan="2"
                       TextColor="Fuchsia"/>
                <Label Text="Bottom Left"
                       Grid.Row="3"
                       Grid.Column="0" />
                <Label Text="Bottom Right"
                       Grid.Row="3"
                       Grid.Column="1" />
            </Grid>
        </StackLayout>
        <StackLayout Padding="20, 20, 20, 20" VerticalOptions="EndAndExpand">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Button Text="Call X"
                        BackgroundColor="Fuschia"
                        TextColor="White"
                        Grid.Row="0"
                        Grid.Column="0"/>
                <Button Text="Navigate!"
                        BackgroundColor="Fuschia"
                        TextColor="White"
                        Grid.Row="0"
                        Grid.Column="1"
                        Grid.ColumnSpan="2"/>
            </Grid>
        </StackLayout>
    </StackLayout>
</ContentPage.Content>

您可以看到我尝试在 XAML 中打印订单 ID 的位置.Order 的所有支持类都有 ToString(s),它返回订单页面所需的信息,所以我并不担心打印_currentOrder.PickupAddress.Address1",例如.

You can see where I attempted to print the ID of the order in the XAML. All of the supporting classes for the Order have ToString(s) which return the needed information for the order page, so I'm not really worried about printing '_currentOrder.PickupAddress.Address1', for example.

推荐答案

你的 BindingContext 是对当前页面的引用

your BindingContext is a reference to the current Page

this.BindingContext = this;

所以你的绑定路径看起来像:

so your binding path would look like:

<Label Text="{Binding CurrentOrder.ID}" TextColor="Fuchsia" />

这篇关于xaml.cs 中的 Xamarin 局部变量并通过 XAML 文件打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:12