使用Firebase Wrapper,FireSharp。 [英] Working with Firebase Wrapper, FireSharp.
问题描述
我正在使用Firebase Wrapper for .Net,FireSharp。
I'm working with Firebase Wrapper for .Net, FireSharp.
我遇到了一些问题。
的 1。如果我的Firebase为空并且我向其添加了新值,它会完美地添加它们但是当我立即更新任何值时,它会在后端更新它并且前端不显示任何更改,除非我重新启动应用程序然后更新工作正常。
2.如果我的Firebase有值,则会将它们加载到列表视图中,我可以更新,删除它们,但如果我尝试更新索引0处的值,即listview的第一个条目,它只在后端更新它,并且在前面没有显示任何变化,同样的
用于新添加的值,我总是要重新启动应用程序才能看到更改。
这是我的代码:
MainPage.Xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using FireSharp.Interfaces;
using FireSharp.Config;
using FireSharp;
using FireSharp.Response;
using FireSharp.EventStreaming;
using FireSharp.Extensions;
using Windows.UI.Popups;
using System.Diagnostics;
namespace Firebase_FireSharp
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
// var key = setDataToFirebase();
ListenToStream();
//ListUpdate();
}
IFirebaseConfig config = new FirebaseConfig
{
AuthSecret = "dlBXpQ4mV0Oc3xA81YblahwEsnegLBdkHU9DHDQs",
BasePath = "https://torrid-inferno-8908.firebaseio.com/"
};
IFirebaseClient client;
private static Dictionary<string,string> keyHolder = new Dictionary<string,string>();
private async void ListenToStream()
{
client = new FirebaseClient(config);
await client.OnAsync("FireSharp/Name/", (sender, args) =>
{
//Gets the Unique ID and deletes the any other string attached to it
string dataFromFB = args.Data;//.Remove(22, 4);
string paths = args.Path;
string key = RemoveNameSubstring(paths);
string uniqueKey = key.Split('/').Last();
if (keyHolder.ContainsKey(uniqueKey))
{
keyHolder[uniqueKey] = dataFromFB;
AddToListView(dataFromFB);
}
else
{
keyHolder.Add(uniqueKey, dataFromFB);
//foreach (string value in keyHolder.Values)
AddToListView(dataFromFB);
}
});
}
private async void AddToListView(string data)
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
() =>
{
if (send.Content.Equals("Update"))
// if ("Update".Equals(send.Content))
{
listView.Items.Insert(listView.SelectedIndex, data);
listView.Items.RemoveAt(listView.SelectedIndex);
send.Content = "Send";
}
else
{
listView.Items.Add(data);
}
});
}
private async void SetDataToFirebase(string text)
{
client = new FirebaseClient(config);
// List<string> values = new List<string>();
Dictionary<string, string> values = new Dictionary<string, string>();
// PushResult keys = new PushResult();
values.Add("Name", text);
// var key = response.Result;
// values[keys] = textBox.Text;
var response = await client.PushAsync("FireSharp/Name/", values);
// response = await client.PushAsync("FireSharp", values);
var key = response.Result.Name; //unique key
//return key;
}
Dictionary<string, string> val = new Dictionary<string, string>();
private async void send_Click(object sender, RoutedEventArgs e)
{
string selectedIndex;
// string keys;
string pathToUpdate;
if (send.Content.Equals("Update"))
// if ("Update".Equals(send.Content))
{
selectedIndex = keyHolder.Keys.ElementAt(listView.SelectedIndex);
// if (selectedIndex.Length == 26)
// {
pathToUpdate = selectedIndex;
// val.Add("Name", textBox.Text);
val["Name"] = textBox.Text;
// val.Add("Name", textBox.Text);
ListUpdation(pathToUpdate, val);
// val.Remove("Name");
// }
// else
// {
// keys = RemoveNameSubstring(selectedIndex);
// pathToUpdate = keys.Split('/').Last();
// val.Add("Name", textBox.Text);
// ListUpdation(pathToUpdate, val);
//}
}
else {
// textBox.Text = listView.SelectedItem.ToString();
if (textBox.Text == "")
{
MessageDialog message = new MessageDialog("Enter Some Text", "Sorry");
await message.ShowAsync();
}
else
{
SetDataToFirebase(textBox.Text);
// textBox.Text = "";
}
}
}
private async void DeleteFromFirebase(string val) {
client = new FirebaseClient(config);
FirebaseResponse delete = await client.DeleteAsync("FireSharp/Name/"+val);
var status = delete.StatusCode;
if (status.ToString() == "OK")
{
listView.Items.Remove(listView.SelectedItem);
keyHolder.Remove(val);
}
}
private async void ListUpdation(string key, Dictionary<string,string> updatedValue) {
FirebaseResponse response;
response = await client.UpdateAsync("FireSharp/Name/"+key, updatedValue);
//keyHolder[key] = textBox.Text;
textBox.Text = "";
}
private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
int currentIndex = listView.SelectedIndex;
string currentKey = keyHolder.Keys.ElementAt(currentIndex);
// string getUniqueKey = RemoveNameSubstring(currentKey);
//string pathToDelete = getUniqueKey.Split('/').Last();
if (listView.SelectedItem.ToString() == keyHolder.Values.ElementAt(currentIndex))
{
DeleteFromFirebase(currentKey);
}
}
public string RemoveNameSubstring(string name) {
int index = name.IndexOf("/Name");
string uniqueKey = (index < 0) ? name : name.Remove(index, "/Name".Length);
return uniqueKey;
}
private void EditBarButton_Click(object sender, RoutedEventArgs e)
{
// if (textBox.Text == "")
// {
// MessageDialog message = new MessageDialog("Please select an Item to Update", "Sorry");//////
// await message.ShowAsync();
// }
// else {
textBox.Text = listView.SelectedItem.ToString();
// string Update = textBox.Text;
// string selectedIndex = keyHolder.Keys.ElementAt(listView.SelectedIndex);
// string keys = RemoveNameSubstring(selectedIndex);
// string pathToUpdate = keys.Split('/').Last();
send.Content = "Update";
// Dictionary<string, string> values = new Dictionary<string, string>();
// }
// // PushResult keys = new PushResult();
// values.Add("Name", textBox.Text);
// if (textBox.IsEnabled)
// {
// DataUpdated(pathToUpdate, values);
// }
}
}
}
MainPage.Xaml
<Page
x:Class="Firebase_FireSharp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Firebase_FireSharp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView Name="listView" HorizontalAlignment="Left" Height="666" Margin="20,95,0,0" VerticalAlignment="Top" Width="1321">
</ListView>
<TextBox Name="textBox" HorizontalAlignment="Left" Margin="20,41,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="1215"/>
<Button Name="send" Content="Send" HorizontalAlignment="Left" Margin="1245,38,0,0" VerticalAlignment="Top" Width="114" Click="send_Click"/>
</Grid>
<Page.BottomAppBar>
<CommandBar>
<CommandBar.SecondaryCommands>
<AppBarButton Label="Edit" Icon="Edit" Click="EditBarButton_Click"/>
<AppBarButton Label="Remove" Icon="Remove" Click="AppBarButton_Click" />
</CommandBar.SecondaryCommands>
</CommandBar>
</Page.BottomAppBar>
</Page>
我已经浪费了大约一个月左右,但无法提出任何解决方案,我不知道我的代码是否有任何问题或者是什么。
I have been wasted like a month or so on this but unable to come up with any solution, I don't know if anything is wrong with my code or what.
请帮忙。
谢谢。
Aaqib
推荐答案
您好Aaqib,
Hi Aaqib,
问题听起来更像是与Firebase相关的问题,我强烈建议您从以下方面获得支持:
https: //www.firebase.com/docs/help/ ,也引用firebase网站:
The question sounds more like the issue related with Firebase, I would highly recommend you find support from: https://www.firebase.com/docs/help/, also quote from firebase site:
对于API问题和一般问题技术帮助,我们建议您在Stack Overflow上使用Firebase标记。我们非常密切地监控
问题,使用StackOverflow可以让其他人从答案中受益。
这篇关于使用Firebase Wrapper,FireSharp。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!