[英]Why this xamarin Binding Converter crashs the application?
我有一個 boolean 屬性,我想將它的否定值(,值)綁定到一個按鈕,但是每次我在 xaml 標簽中配置轉換器時,它都會毫無例外地終止應用程序。 我在應用中心沒有發現任何錯誤。 如果我刪除這部分, Converter={StaticResource inverter}}
一切都會再次工作,但沒有轉換。
我的 xaml:
<ContentPage.Resources>
<local:BooleanConverter x:Key="inverter" />
...
<ContentPage.Resources>
...
<controls:FrameButton IsEnabled="{Binding IsBusy, Converter={StaticResource inverter}}" Margin="5" CornerRadius="5">
還有我的 BooleanConverter.cs:
using System;
using Xamarin.Forms;
public class BooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return !System.Convert.ToBoolean(value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return !System.Convert.ToBoolean(value);
}
}
更新 1
框架按鈕代碼:
using Xamarin.Forms;
namespace CustomControls.Controls
{
public class FrameButton : Frame
{
public FrameButton()
{
var MyTapGesture = new TapGestureRecognizer();
MyTapGesture.Tapped += (sender, e) => { Clicked(); };
GestureRecognizers.Add(MyTapGesture);
}
public async void Clicked()
{
await this.ScaleTo(1.1, 100);
await this.ScaleTo(1, 100);
}
}
}
使用顯式強制轉換,因為您始終知道value
將是 bool ,除非您使用錯誤。 通常不使用ConvertBack
,因為它通常沒有意義。
using System;
using Xamarin.Forms;
public class BooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return !((bool)value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
也許是因為它試圖將 IsEnabled 值轉換回來。 您是否嘗試過使其成為OneWay
,例如
IsEnabled="{Binding IsBusy, Mode=OneWay, Converter={x:StaticResource BooleanConverter}}"
編輯考慮到您的評論。
我相信 SEGSEGV 錯誤是 memory 分段錯誤,即 memory 讀取錯誤。 所以也許沒有找到inverter
。
我發現其中一些深奧的錯誤消息通常是由 XAML 代碼中的錯誤觸發的。
我看到您對該頁面的轉換器聲明是:
<ContentPage.Resources>
<local:BooleanConverter x:Key="inverter" />
...
<ContentPage.Resources>
通常聲明將是:
<ContentPage.Resources>
<ResourceDictionary>
<local:BooleanConverter x:Key="inverter" />
...
</ResourceDictionary>
<ContentPage.Resources>
我懷疑與被調用的鍵“逆變器”的關系沒有聲明,因為它不在ResourceDictionary
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.