实时QR更新使用Firebase
我一直在Xamarin.Forms中进行一个项目,这些项目使用Firebase(目前只能保存项目)来列出列表。 然后,我使用ivalueconverter
将该列表检索并将其转换为字符串,然后我最终使用相同的字符串将其转换为使用ZXing将其转换为QR代码。
在我的代码itemviewpage.xaml
中,我使用CollectionView
检查数据库是否首先是在更新。
我的问题是,直到在热重加载过程中删除并在BarCodeValue
中删除并重新输入绑定之前,QR码不会显示。
itemViewPage.xAML:
<ContentPage.Resources>
<ResourceDictionary>
<local:CollectionConverter x:Key="CollectionConverter"/>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<CollectionView ItemsSource="{Binding DatabaseItems}">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding Product}" FontSize="Large"/>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<zxing:ZXingBarcodeImageView BarcodeValue="{Binding DatabaseItems, Converter={StaticResource CollectionConverter}}" WidthRequest="300" HeightRequest="300">
<zxing:ZXingBarcodeImageView.BarcodeOptions>
<zxingcommon:EncodingOptions Width="300" Height="300"/>
</zxing:ZXingBarcodeImageView.BarcodeOptions>
</zxing:ZXingBarcodeImageView>
</StackLayout>
</ContentPage.Content>
itemViewPage.xaml.cs:
namespace InventorySample.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ItemViewPage : ContentPage
{
public ObservableCollection<MyDatabaseRecord> DatabaseItems { get; set; } = new
ObservableCollection<MyDatabaseRecord>();
readonly FirebaseClient firebaseClient = new FirebaseClient("https://inventorysample-eca49-default-rtdb.asia-southeast1.firebasedatabase.app/");
public ItemViewPage()
{
InitializeComponent();
BindingContext = this;
var collection = firebaseClient
.Child("Products")
.AsObservable<MyDatabaseRecord>()
.Subscribe((dbevent) =>
{
if(dbevent.Object != null)
{
DatabaseItems.Add(dbevent.Object);
}
});
}
}
}
collectionConverter.cs
namespace InventorySample
{
public class CollectionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
ObservableCollection<MyDatabaseRecord> Items = (ObservableCollection<MyDatabaseRecord>)value;
var sb = new StringBuilder();
foreach (var item in Items)
{
sb.AppendLine(item.Product);
}
return sb.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
}
我已经尝试了多件事并完全被陷入困境。希望有人可以帮忙。提前致谢。
I've been making a project in Xamarin.Forms that use Firebase (which for now only saves items) to make a list.
I then retrieve and convert that list to string using IValueConverter
, and I finally use that same string to convert it to QR code using ZXing.
In my code ItemViewPage.xaml
, I used a CollectionView
to check whether the database is updating in the first place.
My problem is that the QR code doesn't show until I remove and retype my binding in BarcodeValue
during hot reload.
ItemViewPage.xaml:
<ContentPage.Resources>
<ResourceDictionary>
<local:CollectionConverter x:Key="CollectionConverter"/>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<CollectionView ItemsSource="{Binding DatabaseItems}">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding Product}" FontSize="Large"/>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<zxing:ZXingBarcodeImageView BarcodeValue="{Binding DatabaseItems, Converter={StaticResource CollectionConverter}}" WidthRequest="300" HeightRequest="300">
<zxing:ZXingBarcodeImageView.BarcodeOptions>
<zxingcommon:EncodingOptions Width="300" Height="300"/>
</zxing:ZXingBarcodeImageView.BarcodeOptions>
</zxing:ZXingBarcodeImageView>
</StackLayout>
</ContentPage.Content>
ItemViewPage.xaml.cs:
namespace InventorySample.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ItemViewPage : ContentPage
{
public ObservableCollection<MyDatabaseRecord> DatabaseItems { get; set; } = new
ObservableCollection<MyDatabaseRecord>();
readonly FirebaseClient firebaseClient = new FirebaseClient("https://inventorysample-eca49-default-rtdb.asia-southeast1.firebasedatabase.app/");
public ItemViewPage()
{
InitializeComponent();
BindingContext = this;
var collection = firebaseClient
.Child("Products")
.AsObservable<MyDatabaseRecord>()
.Subscribe((dbevent) =>
{
if(dbevent.Object != null)
{
DatabaseItems.Add(dbevent.Object);
}
});
}
}
}
CollectionConverter.cs
namespace InventorySample
{
public class CollectionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
ObservableCollection<MyDatabaseRecord> Items = (ObservableCollection<MyDatabaseRecord>)value;
var sb = new StringBuilder();
foreach (var item in Items)
{
sb.AppendLine(item.Product);
}
return sb.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
}
I've already tried multiple things and am completely stumped. Hope someone can help. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论