gpt4 book ai didi

c# - 使用 BindableObject 实现 IValueConverter 并且绑定(bind)的 Property 始终为默认值

转载 作者:行者123 更新时间:2023-12-04 17:32:57 27 4
gpt4 key购买 nike

我正在尝试找到一种方法来处理 CommandParameter 不能与 IValueConverter 绑定(bind)的事实。通过阅读,我发现了在实现 IValueConverter 时从 BindableObject 继承的建议。

尝试访问该属性时,它始终为 0,可能采用默认值。

作为引用,我通过使用 Xamarin.Forms Shell 模板作为示例来简化问题:

当使项目使用移动应用程序(Xamarin.Forms)并在创建项目时选择 Shell。从那里我做了少量修改。请注意,我只是设置一个断点来查看分配给 BindableProperty 的内容,它始终为 0。

enter image description here

此示例向已提供的 Item.cs 类添加两个属性。

public class Item {
public string Id { get; set; }

public string Text { get; set; }

public string Description { get; set; }

// Added these two properties. The idea is to Bind Number1 to the Text property of a label, and pass Number2 to the Property
public int Number1 { get; set; }

public int Number2 { get; set; }
}

MockDataStore.cs 的构造函数中,我为 Number1Number2 属性添加了值。

var mockItems = new List<Item>{
new Item { Id = Guid.NewGuid().ToString(), Text = "First item", Description="This is an item description.", Number1 = 5, Number2 = 20},
new Item { Id = Guid.NewGuid().ToString(), Text = "Second item", Description="This is an item description.", Number1 = 2, Number2 = 8},
new Item { Id = Guid.NewGuid().ToString(), Text = "Third item", Description="This is an item description.", Number1 = 3, Number2 = 6},
new Item { Id = Guid.NewGuid().ToString(), Text = "Fourth item", Description="This is an item description.", Number1 = 4, Number2 = 7},
new Item { Id = Guid.NewGuid().ToString(), Text = "Fifth item", Description="This is an item description.", Number1 = 1, Number2 = 10},
new Item { Id = Guid.NewGuid().ToString(), Text = "Sixth item", Description="This is an item description.", Number1 = 8, Number2 = 2}
};

带有 BindableObject 和 IValueConverter 的转换器

public class Number1Number2Converter : BindableObject, IValueConverter {
public static readonly BindableProperty ValueProperty =
BindableProperty.Create(nameof(Value), typeof(int), typeof(Number1Number2Converter), null);

public int Value {
get {
return (int)GetValue(ValueProperty);
}
set {
SetValue(ValueProperty, value);
}
}

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
// object value does have a value assigned, which is good however the bound Property of Value does not. This should have Number2 being passed in.
var testing123 = Value;

return null;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
return null;
}
}

var testing123 = Value; 行上,我放置了一个断点。我还没有尝试使用 Value,我只是想看到它不是 0。我在 MockDataStore 中分配给 Number2 的值没有一个是 0。

这是修改后的 ItemsPage.xaml。请注意绑定(bind)了 Number1 的标签,当然还有转换器。

<?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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converters="clr-namespace:App2.Converters;assembly=App2"
mc:Ignorable="d"
x:Class="App2.Views.ItemsPage"
Title="{Binding Title}"
x:Name="BrowseItemsPage">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Add" Clicked="AddItem_Clicked" />
</ContentPage.ToolbarItems>
<StackLayout>
<ListView x:Name="ItemsListView"
ItemsSource="{Binding Items}"
VerticalOptions="FillAndExpand"
HasUnevenRows="true"
RefreshCommand="{Binding LoadItemsCommand}"
IsPullToRefreshEnabled="true"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
CachingStrategy="RecycleElement"
ItemSelected="OnItemSelected">
<d:ListView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>First Item</x:String>
<x:String>Second Item</x:String>
<x:String>Third Item</x:String>
<x:String>Forth Item</x:String>
<x:String>Fifth Item</x:String>
<x:String>Sixth Item</x:String>
</x:Array>
</d:ListView.ItemsSource>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="10">
<Label Text="{Binding Text}"
d:Text="{Binding .}"
LineBreakMode="NoWrap"
Style="{DynamicResource ListItemTextStyle}"
FontSize="16" />
<Label Text="{Binding Description}"
d:Text="Item description"
LineBreakMode="NoWrap"
Style="{DynamicResource ListItemDetailTextStyle}"
FontSize="13" />
<Label>
<Label.Text>
<Binding Path="Number1">
<Binding.Converter>
<converters:Number1Number2Converter Value="{Binding Path=Number2}" />
</Binding.Converter>
</Binding>
</Label.Text>
</Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>

Value 在此示例中始终为 0。参数 object value 被赋予了 Number1 的 int 值,但是 Value 的可绑定(bind)属性应该被赋予 Number2。这个概念是否存在根本性缺陷,或者我是否在正确的轨道上但未能实现某些东西?

最佳答案

您可以尝试将整个 Item 对象发送到您的转换器:

<Label Text="{Binding Path=., Converter={StaticResource Number1Number2Converter}}" />

然后,在您的转换器中您应该可以访问这两个属性:

public class Number1Number2Converter : IValueConverter {    
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
if (value is Item item)
{
var number1 = item.Number1;
var number2 = item.Number2,
}
return null;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
return null;
}
}

关于c# - 使用 BindableObject 实现 IValueConverter 并且绑定(bind)的 Property 始终为默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57892298/

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