实时QR更新使用Firebase

发布于 2025-01-29 20:54:45 字数 3368 浏览 0 评论 0原文

我一直在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文