gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:15:30 25 4
gpt4 key购买 nike

我希望能够在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com