How can i use an SQLiteAsyncConnection for Android using Xamarin? - c#

I am trying to implement SQLite in a Xamarin solution using Visual Studio. However, i can not find the proper way to implement an SQLiteAsyncConnection.
I am following along a tutorial, which just passes a string to the constructor of the SQLiteAsyncConnection, however this is (no longer?) accepted. I googled to find a newer solution and almost everywhere found the solution to add a platform to the constructor, however that results in an error for me.
I think i might not have added the correct NUGet packages to the project(s). I have added the sqlite-net-pcl package to both the shared project and the android project.
I have also updated all NUGet packages today, rebuilt the solution and even deleted the /bin and /obj folder and it still is not working.
using System.IO;
using SQLite.Net;
using SQLite.Net.Async;
using Xamarin.Forms;
using SQLiteTest.Droid;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
[assembly: Dependency(typeof(SQLiteDB))]
namespace SQLiteTest
{
public class SQLiteDB : ISQLiteDB
{
public SQLiteAsyncConnection GetAsyncConnection()
{
const string fileName = "test.db3";
var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(documentsPath, fileName);
var platform = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
var cwLock = new SQLiteConnectionWithLock(platform, new SQLiteConnectionString(path, true));
var connection = new SQLiteAsyncConnection(() => cwLock);
return connection;
}
}
}
The error i get is:
Error CS0234 The type or namespace name 'Platform' does not exist in the namespace 'SQLite.Net' (are you missing an assembly reference?) SQLiteTest.Android D:\Mobile Apps\Mosh\SQLiteTest\SQLiteTest\SQLiteTest.Android\SQLiteDB.cs

Well, I am pretty sure you have not added the SQLite.Net.Platform.XamarinAndroid.dll package to your native Android project which is causing this issue when you use this dependency service.
If you are using SQLite-net and or SQLite.Net-PCL look for the Xamarin.Android platform dll int he path below:
packages\SQLite.Net-PCL.3.1.1\lib\MonoAndroid\`SQLite.Net.Platform.XamarinAndroid.dll`

Related

running C# libraries inside another project

I'm trying to get this library to work inside my project. I've downloaded it to my documents folder and in the same documents folder I've created a new C# console app with just a Program.cs to try and run it. I can compile the library as far as I know.
Now, I've imported the project into my project using:
dotnet add C:/blabla/documents/tachograph-reader-test-code/tachograph-reader-test-code.csproj reference C:/blabla/documents/tachograph-reader/src/tachograph-reader-lib.csproj
and this worked just fine. As the following piece of code is added to my project file:
<ItemGroup>
<ProjectReference Include="..\tachograph-reader\src\tachograph-reader-lib.csproj" />
</ItemGroup>
Now for the life of me, I can't figure out how to use the library?
Intellisense does not see the library in any way. the using keyword doenst work either. De namespace should be DataFileReader but I cannot use it, and it won't compile.
By the way, I'm using VS CODE not vs studio.
So, how do I use such a library inside my project?
The problem is not in your project reference. I guess you have to add these three namespaces as follow:
using System.IO;
using System.Text;
using System.Xml;
Your Program.cs should look like:
using System;
using System.IO;
using System.Text;
using System.Xml;
using DataFileReader;
namespace tachograph-reader-test-code
{
class Program
{
static void Main(string[] args)
{
...
}
...
}
}

Metro Modern UI " Failed to create component" C# Visual Studio 2019

I was trying out the Metro UI to create a C# application so I was testing out the framework tools in a new project. I've followed the instructions from https://n-a-r-w-i-n.github.io/MetroSet-UI/#how-to-use, as well as watching youtube videos where they install it via the Package Manager Console. All result in the same error I have been getting when I try to add an item to the form. Encountered Error
After running the design view goes completely blank and I cannot edit or do anything in that regard. Here is the code used for Form1.
using MetroFramework.Forms;
using MetroFramework;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Test_App
{
public partial class Form1 : MetroForm
{
public Form1()
{
InitializeComponent();
}
}
}
I have notices I also have this warning.
Severity: Warning
Code: NU1701
Description: Package 'MetroModernUI 1.4.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETCoreApp,Version=v3.1'. This package may not be fully compatible with your project.
Project: Test App.csproj
Line: 1
Please assist me to get a hang of this! Thank you for your time in advanced! :)

Why doesn’t using a missing package cause Visual Studio to complain about my Azure Function?

I’m new to C#. In my project, I have a file which contains this code:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace MyNamespace {
public static class MyClass {
// ...
static MyClass () {
// ...
}
[FunctionName("...")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req,
ILogger log) {
log.LogInformation("...");
// ...
}
}
}
For all those using statements, I have a corresponding package listed under Solution explorer → Dependencies → Packages, except for Microsoft.Extensions.Logging. Now, Visual Studio doesn’t complain about this fact and the build process works just fine. Running the application, however, this error appears when it reaches the log.LogInformation("...") statement:
So, I install it from NuGet:
but doing that, I obtain a different error from the console of the Azure Function, that is:
What should I do?
Have you installed dependent packages like Microsoft.Extensions.Logging.Abstractions. Its strange by default dependent packages are installed as part of creating new Azure function. I would suggest try once again using Visual studio template of Azure function.

ASP.NET PhysicalFileProvider not available in Startup.cs

I am trying to serve static files in a ASP.NET 4.6 web application, MVC 5.2.3 using the Entity Framework. I'm following this tutorial.
I'm getting a compiler error:
The type or namespace name 'PhysicalFileProvider' could not be found
(are you missing a using directive or an assembly reference?)
It seems that the using Microsoft.Extensions.FileProviders is not actually being used (it is grayed out in Visual Studio.
I have tried importing various assemblies without any luck. I'm not a .NET developer and any help would be greatly appreciated.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
using Microsoft.Owin;
using Microsoft.Owin.StaticFiles;
using Microsoft.Extensions.Configuration;
using Owin;
[assembly: OwinStartup(typeof(Monica.Startup))]
namespace Monica
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseStaticFiles(); // For the wwwroot folder
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), #"MyStaticFiles")),
RequestPath = new PathString("/StaticFiles")
});
}
}
}
You should install the nugget package:
Microsoft.Extensions.FileProviders.Physical
Right click the project -> Manage Nugget Packages -> Browse -> Microsoft.Extensions.FileProviders.Physical -> Install.
The using statement should be available to you afterwards.

How to write test case using Appium in C#?

I want to use Appium to make automated testing for my Android app. I downloaded the Appium for Windows and could run my app on device from within appium.exe. Now I want to write test cases in C# which make use of selendroid integrated in Appium. I googled a lot but couldn't find any example demonstrating the same. I found one project on github but it's giving a lot of compiler errors. Could anybody guide me about how to write test cases using Appium in C#? If you have any resources, please provide me the same. Thanks.
You can go with installing Visual Studio 2012 or visual express for web. Use NUnit framework to write the test cases and execute them as a class library project in that.
1) Create a class library project and create a new class e.g "Class1". Add the packages as provided in the links above. Try the below code
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using NUnit.Framework;
using OpenQA.Selenium.Interactions;
using System.Threading;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.MultiTouch;
using OpenQA.Selenium.Appium.Interfaces;
using System.Drawing;
namespace ClassLibrary2
{
[TestFixture]
public class Class1
{
public AppiumDriver driver;
public DesiredCapabilities capabilities;
public Class1()
{
Console.WriteLine("Connecting to Appium server");
capabilities = new DesiredCapabilities();
capabilities.SetCapability(CapabilityType.BrowserName, "Android");
capabilities.SetCapability(CapabilityType.Platform, "Windows");
capabilities.SetCapability(CapabilityType.Version ,"4.1.2");
capabilities.SetCapability("Device", "Android");
//Application path and configurations
driver = new AppiumDriver(new Uri("http://127.0.0.1:4723/wd/hub"), capabilities);
}
[Test]
public void login()
{
driver.FindElement(By.Name("Country")).Click();
//Your further code as per the application.
}
Then Build a solution for your project and run it in NUnit.

Categories