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

标签 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 的位置。 Order 的所有支持类都有 ToString(s),它返回订单页面所需的信息,所以我并不真的担心打印“_currentOrder.PickupAddress.Address1”,例如。

最佳答案

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

this.BindingContext = this;

所以你的绑定(bind)路径应该是这样的:

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

关于c# - xaml.cs 中的 Xamarin 局部变量并通过 XAML 文件打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42540182/

相关文章:

xamarin - Google Analytics 的用户代理字符串

c# - 获取要在自定义属性中使用的属性值

c# - 如何在 asp.net 中使用 png 文件中的菜单

c# - FormsAuthentication 登录在设置授权票证后拒绝重定向

c# - WPF:带有重置项的组合框

c# - 未定义的 CLR 命名空间 - 未找到解决方案

jquery-mobile - 在 Xamarin 解决方案中使用 jQuery Mobile 是否可行?

c# - 将 HTTP GET 方法添加到 C# Web 服务

xaml - Xamarin Forms ListView 在框架中显示行项目

asynchronous - 异步 block 中的 Xamarin Android F# 更新 UI