I am trying to get data from firebase but it shows an error "Could not cast or convert from System.String to System.Collections.Generic.Dictionary`2"
This is my code:
IFirebaseConfig config = new FirebaseConfig()
{
AuthSecret = "Auth",
BasePath = "Path"
};
IFirebaseClient client;
private void Form1_Load(object sender, EventArgs e)
{
try
{
client = new FirebaseClient(config);
if (client != null)
{
MessageBox.Show("OK");
}
}
catch
{
MessageBox.Show("No");
}
LiveCall();
}
async void LiveCall()
{
while (true)
{
await Task.Delay(1000);
FirebaseResponse res = await client.GetAsync(#"GPS/lat");
Dictionary<string, GPS> data = JsonConvert.DeserializeObject<Dictionary<string, GPS>>(res.Body.ToString());
UpdateRTB(data);
}
}
void UpdateRTB(Dictionary<string, GPS> record)
{
label1.Text += record.ElementAt(1).Key + record.ElementAt(1).Value;
}
Can Anybody help me to solve the problem? thanks!
I am using inetlab.smpp in c# web app to send sms. A client is created and connected successfully and bound but the message is not delivered to the recipient
public partial class sendsmss : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected async void send_Click(object sender, EventArgs e)
{
SmppClient client = new SmppClient();
await client.Connect("xx.xx.xx.xx", 00000);
if (client.Status == Inetlab.SMPP.Common.ConnectionStatus.Open)
{
await client.Bind("user", "pass");
if (client.Status == Inetlab.SMPP.Common.ConnectionStatus.Open)
{
SubmitSm sm = new SubmitSm();
sm.UserData.ShortMessage = client.EncodingMapper.GetMessageBytes("Test Test Test Test Test Test Test Test Test Test", DataCodings.Default);
sm.SourceAddress = new SmeAddress("1111");
sm.DestinationAddress = new SmeAddress("12345678");
sm.DataCoding = DataCodings.UCS2;
sm.RegisteredDelivery = 1;
await client.Submit(sm);
SubmitSmResp response = await client.Submit(sm);
if (response.MessageId != "")
{
Response.Write("response.messageID is " + response.MessageId.ToString() + "</br> ");
}
else { Response.Write("response null </br> "); }
await client.UnBind();
}
}
}
}
I expect the sms is delivered to recipient
Runs for me perfectly
private async void button1_Click(object sender, EventArgs e)
{
Inetlab.SMPP.SmppClient client = new Inetlab.SMPP.SmppClient();
await client.Connect("x.x.x.x", y);
await client.Bind("systemid", "password", ConnectionMode.Transceiver);
var resp = await client.Submit(
SMS.ForSubmit()
.From("SOURCEADDR", AddressTON.Alphanumeric, AddressNPI.Unknown )
.To("mobilenumber", AddressTON.International, AddressNPI.ISDN)
.Coding(DataCodings.Default)
.Text("test text")
);
if (resp.All(x => x.Header.Status == CommandStatus.ESME_ROK))
{
MessageBox.Show("Message has been sent.");
}
else
{
MessageBox.Show(resp.GetValue(0).ToString());
}
}
I have three buttons each with the same loop but different string can I shorten it to one loop so I don't have to keep reusing the loop?
public static string VarOutput { get; set; }
async private void btnCourse1_Click(object sender, RoutedEventArgs e)
{
string VarOutput = "";
string[] names = new string[3] { "COP3488C,", "UWP1,", "This course is mobile app development." };
for (int i = 0; i < names.Length; i++)
{
VarOutput = VarOutput + names[i] + " ";
}
txtBoxCourse.Text = VarOutput;
var dialog = new MessageDialog(VarOutput);
await dialog.ShowAsync();
}
async private void btnCourse2_Click(object sender, RoutedEventArgs e)
{
string VarOutput = "";
string[] names = new string[3] { "DOP3488B,", "UWC1,", "This course is Cloud Computing." };
for (int i = 0; i < names.Length; i++)
{
VarOutput = VarOutput + names[i] + " ";
}
txtBoxCourse.Text = VarOutput;
var dialog = new MessageDialog(VarOutput);
await dialog.ShowAsync();
}
async private void btnCourse3_Click(object sender, RoutedEventArgs e)
{
string VarOutput = "";
string[] names = new string[3] { "BOP3589,", "UWP2,", "This course Computer Programming Java 1." };
for (int i = 0; i < names.Length; i++)
{
VarOutput = VarOutput + names[i] + " ";
}
txtBoxCourse.Text = VarOutput;
var dialog = new MessageDialog(VarOutput);
await dialog.ShowAsync();
}
Just create a method and call it with the proper parameters, like this:
async private void btnCourse1_Click(object sender, RoutedEventArgs e)
{
string[] names = new string[3] { "COP3488C,", "UWP1,", "This course is mobile app development." };
await WorkerAsync(names);
}
async private void btnCourse2_Click(object sender, RoutedEventArgs e)
{
string[] names = new string[3] { "DOP3488B,", "UWC1,", "This course is Cloud Computing." };
await WorkerAsync(names);
}
async private void btnCourse3_Click(object sender, RoutedEventArgs e)
{
string[] names = new string[3] { "BOP3589,", "UWP2,", "This course Computer Programming Java 1." };
await WorkerAsync(names);
}
private async Task WorkerAsync(string[] names)
{
string VarOutput = "";
for (int i = 0; i < names.Length; i++)
{
VarOutput = VarOutput + names[i] + " ";
}
txtBoxCourse.Text = VarOutput;
var dialog = new MessageDialog(VarOutput);
await dialog.ShowAsync();
}
Note: Code is not tested.
Refactor the repeated code to
private async Task displayCourseInfo(string[] names) {
//Replaced for loop with this line;
var message = String.Join(" ", names);
txtBoxCourse.Text = message;
var dialog = new MessageDialog(message);
await dialog.ShowAsync();
}
The for loop is simply constructing a string with spaces which can be replaced with a String.Join
Call the method in the event handlers.
private async void btnCourse1_Click(object sender, RoutedEventArgs e) {
var names = new []{ "COP3488C,", "UWP1,", "This course is mobile app development." };
await displayCourseInfo(names);
}
private async void btnCourse2_Click(object sender, RoutedEventArgs e) {
var names = new []{ "DOP3488B,", "UWC1,", "This course is Cloud Computing." };
await displayCourseInfo(names);
}
private async void btnCourse3_Click(object sender, RoutedEventArgs e) {
var names = new []{ "BOP3589,", "UWP2,", "This course Computer Programming Java 1." };
await displayCourseInfo(names);
}
I believe by adding an extension method to the string class you may clarify the code and then doing some basic refactoring. This is one way to do it.
namespace ConsoleApplication2
{
public static class myExtensionMethods
{
public static string GetSubs(this string[] input)
{
string value = "";
input.Select(sub => value += $"{sub} ");
return value;
}
}
class Program
{
async private void btnCourse1_Click(object sender, RoutedEventArgs e)
{
await ShowDialogAsync(new string[] { "COP3488C,", "UWP1,", "This course is mobile app development." });
}
async private void btnCourse2_Click(object sender, RoutedEventArgs e)
{
await ShowDialogAsync(new string[3] { "DOP3488B,", "UWC1,", "This course is Cloud Computing." });
}
async private void btnCourse3_Click(object sender, RoutedEventArgs e)
{
await ShowDialogAsync(new string[3] { "BOP3589,", "UWP2,", "This course Computer Programming Java 1." });
}
private async Task ShowDialogAsync(string [] myStringArray)
{
string VarOutput = myStringArray.GetSubs();
txtBoxCourse.Text = VarOutput;
var dialog = new MessageDialog(VarOutput);
await dialog.ShowAsync();
}
I have a small doubt about using SQLite database and local settings. I have an idea about how to use local settings for an app. When we use Application data(local settings) in our application we can restore the previous state of the application. Can we use sqlite local database to store and retrieve the values while we use local settings.
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
if (data.Values["check"] != null)
{
this.Frame.Navigate(typeof(BlankPage1));
}
if (data.Values["add"] != null)
{
list_view.Items.Add(data.Values["add"].ToString());
}
if (data.Values["remove"] != null)
{
list_view.Items.Remove(data.Values["remove"]);
}
var dbpath = ApplicationData.Current.LocalFolder.Path + "/Mydb1.db";
var con = new SQLiteAsyncConnection(dbpath);
await con.CreateTableAsync<list>();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if(!mypop.IsOpen)
{
mypop.IsOpen = true;
}
}
public async void Button_Click_1(object sender, RoutedEventArgs e)
{
var dbpath = ApplicationData.Current.LocalFolder.Path + "/Mydb1.db";
var con = new SQLiteAsyncConnection(dbpath);
list l = new list();
data.Values["add"] = l.list1;
l.list1 = text_input.Text.ToString();
await con.InsertAsync(l);
await con.UpdateAllAsync(l.list1);
data.Values["add"] = l.list1;
list_view.Items.Add(data.Values["add"].ToString());
mypop.IsOpen = false;
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
if(mypop.IsOpen)
{
mypop.IsOpen = false;
}
}
private async void Button_Click_3(object sender, RoutedEventArgs e)
{
var dbpath = ApplicationData.Current.LocalFolder.Path + "/Mydb1.db";
var con = new SQLiteAsyncConnection(dbpath);
list l = new list();
data.Values["remove"] = l.list1;
l.list1 = list_view.SelectedItem.ToString();
list_view.Items.Remove(list_view.SelectedItem);
List<list> mylist = await con.QueryAsync<list>("delete from list where list1='" + list_view.SelectedItem + "'");
mylist.Remove(l);
Please check and verify the code. I can only store single value to local settings. When I restart my application only one value will be shown. It wont show the values inserted in the listview. Please also check delete query. It is not working.. please help me...
Hello i have a next code:
When thumbnail is tapped i have to download file if it doesn't exist localy, or open if exists.
The problem is that if i make very quick two taps - it downloads same file two times - how to prevent this?
As you can see i tried using bool - didn't help.
Tried also using private static SemaphoreSlim TapSemaphore = new SemaphoreSlim(1, 1); - didn't help
public bool IsCurrentlyDownloading = false;
private async void assetThumbnail_Tapped(object sender, TappedRoutedEventArgs e)
{
await OpenOrDownload();
}
private async Task OpenOrDownload()
{
if (FileIsDownloaded == true)
{
string filename = Util.GetLocalFileName(CustomerAsset.file.id, "CustomerAssetFile");
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = false;
var sampleFile = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
await Windows.System.Launcher.LaunchFileAsync(sampleFile, options);
}
else
{
if (!IsCurrentlyDownloading)
{
IsCurrentlyDownloading = true;
DownloadFiles();
}
}
}
Why don't you use a flag (bool, sync object, something) to mark that the downloading or displaying operation is in progress and in this case do not show it.
Rather conceptually, something like this:
public bool IsCurrentlyDownloading = false;
bool isWorking = false;
private async void assetThumbnail_Tapped(object sender, TappedRoutedEventArgs e)
{
if(!isWorking)
await OpenOrDownload();
}
private async Task OpenOrDownload()
{
isWorking = true;
if (FileIsDownloaded == true)
{
string filename = Util.GetLocalFileName(CustomerAsset.file.id, "CustomerAssetFile");
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = false;
var sampleFile = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
await Windows.System.Launcher.LaunchFileAsync(sampleFile, options);
}
else
{
if (!IsCurrentlyDownloading)
{
IsCurrentlyDownloading = true;
DownloadFiles();
}
}
isWorking = false;
}