xaml.cs中的Xamarin局部变量,并通过XAML文件打印

时间:2017-03-01 19:18:40

标签: c# xaml xamarin xamarin.forms

我希望能够在XAML中打印'CurrentOrder'中的属性。

这是我到目前为止所做的:

// 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类,它与其他类有几个属性。

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的位置。订单的所有支持类都有ToString(s),它返回订单页面所需的信息,因此我并不担心打印'_currentOrder.PickupAddress.Address1'。

1 个答案:

答案 0 :(得分:2)

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

this.BindingContext = this;

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

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