我正在研究Listview,其中一個Label內部Cell使用Converter,我需要傳遞一個屬性,因為converter parameter它不是其中的一部分,itemsource而是在viewmodel.
這是我的代碼
<Label FontSize="10"
Text="Insufficient Funds"
IsVisible="{Binding balance, Converter={StaticResource IsInsufficientBalanceConverter}, Source={x:Reference Name=multiCardPage}, ConverterParameter={x:Reference BindingContext.Subtotal} }">
得到這個例外
Xamarin.Forms.Xaml.XamlParseException: 'Position 120:52. Can not find the object referenced by BindingContext.Subtotal'
我想要做什么:
我有一個價值Subtotal(不是 itemsource 的一部分)。在 itemsource 中,有balance屬性,如果balance小于Subtotal,我想在上面顯示,Insufficient Funds Label否則這Label應該是不可見的。為此,我想將小計傳遞給帶有余額的轉換器,以便我可以獲得true或false重視。
我怎樣才能使它作業?
編輯1:我想view和converter聽的變化subtotal值,并更新UI相應的使Insufficient balance label能顯示/隱藏按平衡listview。我已經嘗試過多重系結,但這不支持串列/集合的情況。我怎樣才能解決這個問題。
uj5u.com熱心網友回復:
如何將屬性作為引數傳遞給 Converter 并且屬性在串列 itemsource 背景關系 xaml 之外 - Xamarin
您不需要將Subtotal作為引數傳遞給 Converter,您只需Subtotal在 Converter 類中參考即可。
請參考以下代碼(假設型別Subtotal 為int):
public class InsufficientBalanceConverter : IValueConverter
{
const int Subtotal = 5;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int mValue = (int)value;
if (mValue < Subtotal)
{
return true;
}
else {
return false;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
在 xaml 中,我們可以這樣做:
<Label x:Name="mQuantity" Text="Insufficient Funds"
IsVisible="{Binding balance, Converter={StaticResource MyInsufficientBalanceConverter} }" />
而Resources在 xaml 中是:
<ContentPage.Resources>
<ResourceDictionary>
<converts:InsufficientBalanceConverter x:Key="MyInsufficientBalanceConverter"></converts:InsufficientBalanceConverter>
</ResourceDictionary>
</ContentPage.Resources>
更新:
每次用戶從購物車中洗掉時間時,小計都會更新,在這種情況下,轉換器將具有舊的小計值。
您可以subtotal 在其他類中為(而不是常量)定義一個靜態全域變數,并在InsufficientBalanceConverter . 在這種情況下,一旦 的值Subtotal發生變化,我們就可以得到 的最新值Subtotal。
例如:
public class Variables
{ // here we can change the value of `Subtotal `as we need.
public static int Subtotal = 5;
}
我們可以這樣參考它:
public class InsufficientBalanceConverter : IValueConverter
{
//public static int Subtotal = 5;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int mValue = (int)value;
if (mValue < Variables.Subtotal)
{
return true;
}
else {
return false;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
uj5u.com熱心網友回復:
這是對Jessie 的回答的補充。
您可以使用 MessagingCenter 告訴頁面新的小計。
在此示例中,屬性系結到頁面。如果使用 MVVM,一些代碼將改為在 VM 中。
PaymentPage.xaml:
<Label Text="{Binding Subtotal}" />
PaymentPage.xaml.cs:
public partial class PaymentPage : ContentPage
{
public PaymentPage()
{
InitializeComponent();
BindingContext = this;
}
public int Subtotal
{
get => _subtotal;
set
{
_subtotal = value;
OnPropertyChanged();
}
}
private int _subtotal;
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Subscribe<PaymentCalc, int>(this, "NewSubTotal", (sender, value) =>
{
Subtotal = value;
});
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe<PaymentCalc, int>(this, "NewSubTotal");
}
}
為了使用 MessagingCenter,需要有一些類可以發送訊息。在這里,我定義PaymentCalc.
PaymentCalc.cs:
class PaymentCalc
{
public void NewSubTotal(int value)
{
MessagingCenter.Send(this, "NewSubTotal", value);
}
}
現在在代碼中的任何地方,執行此操作,以計算新的小計,并將其發送到 PaymentPage:
var it = new PaymentCalc();
it.NewSubTotal(12345);
如果您已經有一個正在執行此計算的類,則只需將 NewSubTotal 添加到它,或將該 Send 訊息放入某個適當的方法中。您需要更改 PaymentPage 的訂閱和取消訂閱以使用您的類的名稱,而不是 PaymentCalc。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/404814.html
標籤:
上一篇:如何使用XAML和C#在UWP中的TextBox中設定行數限制
下一篇:使用PHP獲取子域
