How to access GetSystemService(AudioService) in the UI layer - c#

I am developing android App in Xamarin. I want to create an interface and its implementing class in the UI layer. As you see in the below code,
I am getting an error when ever, I use
this.GetSystemService(AudioService);
it is never recognized.
please have a look at the imports below. please let me know how to get it working.
code:
public class ImplClass :
InterfaceFile
{
public bool IsAllowed(Context ctx)
{
AudioManager audioMgr =
(AudioManager)this.GetSystemService(AudioService);
}
}
import:
using System;
using System.Runtime.Remoting.Contexts;
using Android.Media;
using MvvmCross.Platform;
using Android.Content.PM;

You can use the "Application Context".
Example:
public class ImplClass : InterfaceFile
{
public bool IsAllowed(Context ctx)
{
AudioManager audioMgr = (AudioManager)Application.Context.GetSystemService(AudioService);
}
}

I am using this and it works.
global::Android.App.Application.Context.GetSystemService

Related

Trying to establish sqlite connection results in null reference

Xamarin forms newbie here, and I am trying to establish an SQLite connection so that I may interact with a created database in my application.
I get no flags on this in the IDE, but when I compile, I'm getting a null reference error! Specifically, my _SQLiteConnection var is null, and I think it may have something to do with not setting the path properly?
I've referenced a few guides on doing this online, and although a little dated, they go about setting this up in the same manner I am.
Heres some code:
UserDb.cs:
using PluralBuddy.Models;
using SQLite;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace PluralBuddy.Data
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public class UserDB
{
private SQLiteConnection _SQLiteConnection;
public UserDB()
{
_SQLiteConnection = DependencyService.Get<ISQLiteInterface>().GetConnection();
_SQLiteConnection.CreateTable<User>();
}
ISQLiteInterface.cs:
using SQLite;
namespace PluralBuddy.Models
{
public interface ISQLiteInterface
{
SQLiteConnection GetConnection();
}
}
References:
https://code.tutsplus.com/tutorials/an-introduction-to-xamarinforms-and-sqlite--cms-23020
https://dzone.com/articles/register-and-login-using-sqlite-in-xamarinforms
As always, any help is appreciated!
So for anyone who stumbles across this post looking for an answer to a similar problem, make sure that you are initializing your database and giving it a path. Be very careful following guides as they will often leave out vital context, either on purpose or by mistake.
Here is what I did
in App.Xaml
using PluralBuddy.Data;
using PluralBuddy.Views;
using System;
using System.IO;
using Xamarin.Forms;
namespace PluralBuddy
{
public partial class App : Application
{
static UserDB userDB;
public static UserDB UserDB
{
get
{
if (userDB == null)
{
userDB = new UserDB(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "user.db3"));
}
return userDB;
}
}
UserDB.cs
namespace PluralBuddy.Data
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public class UserDB
{
private readonly SQLiteConnection _SQLiteConnection;
public UserDB(string dbPath)
{
_SQLiteConnection = new SQLiteConnection(dbPath);
_SQLiteConnection.CreateTable<User>();
}
and just in case anyone needs it, here is a more up to date guide from one of the same guys as referenced in the original post:
https://xmonkeys360.com/2019/07/10/register-login-and-update-using-sqlite-in-xamarin-forms/

HttpContext.GetGlobalResourceObject not exist in Class Library

I'm writing various class libraries (tools and helpers) for my future ASP.Net project.
Right now my solution doesn't have web project, but it's not supposed to be a problem.
I added reference to System.Web to project and now I able to access HttpContext in code.
Below my code for resource manager class that suppose to get application global resources:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace ResourceManager
{
public class ResourceManager : IResourceManager
{
HttpContext context;
public ResourceManager(HttpContext ctx)
{
context = ctx;
}
public string GetError(string key)
{
return context.GetGlobalResourceObject("Errors", key).ToString();
}
//And so on........
}
}
But unfortunately Visual Studio intellisense can't find GetGlobalResourceObject.
Hitting F12 and I can see this static function inside HttpContext class.
After running build I'm getting this error message:
Member 'HttpContext.GetGlobalResourceObject(string, string)' cannot be accessed with an instance reference; qualify it with a type name instead
It's strange behaviour and interesting for me why it's happening.
Thanks for help
This is untested, but from the error message I think the usage should be as below.
Essentially you just use the static HttpContext rather than creating an instance of it.
using System.Web;
namespace ResourceManager
{
public class ResourceManager : IResourceManager
{
public string GetError(string key)
{
return HttpContext.GetGlobalResourceObject("Errors", key).ToString();
}
//And so on........
}
}

Cannot create an instance of the abstract class or interface on WCF Silverlight Service

I'm working through two tutorials to create a super simple WCF web service and Silverlight app.
Buiding a Service
Accessing a Service from Silverlight
Everything was going fine. I created my service:
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
namespace TestOnline.Web.Data
{
[ServiceContract(Namespace = "")]
[SilverlightFaultBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class DataService
{
[OperationContract]
public String TestService()
{
return "Service Worked!";
}
}
}
I added it as a service reference, then tried to create an instance but I'm getting the error "Cannot create an instance of the abstract class or interface" on the line "proxy = new DataService();"
I pretty much followed the steps of the tutorial exactly, I'm unsure what I've missed. I've certainly not seen many Service examples with constructors, and the reference code is auto-generated - so I don't want to go adding them manually to that.
Does anyone know of a solution/what I've done wrong? Thanks
using System.ServiceModel;
using TestOnline.ServiceReference1;
namespace TestOnline
{
public partial class MainPage : UserControl
{
DataService proxy;
public MainPage()
{
InitializeComponent();
proxy = new DataService();
}
private void TestServiceButton_Click(object sender, RoutedEventArgs e)
{
//call service and get response
}
}
}
You should be creating an instance of the generated proxy client class.
It'll be named DataServiceClient() if it's been added correctly.

Custom Class Inside a WCF Service Object

A bit rusty here with regards to WCF Services.
I have a custom class named cSecurity.cs which does some custom functions.
I want to use this custom class inside my Service:
This is the App.svc.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace AppServices
{
public class App : IApp
{
public cSecurity _csec;
public string GetItems(int agentID, string agentName)
{
// Need to use some functions from the cSecurity class here???
return _csec.getItems();
}
}
}
This is the cSecurity.cs class:
using System.Collections;
using System.Configuration;
using System.Net;
namespace AppServices
{
public class cSecurity
{
// Some functions defined here....
public string getItems(){
return string.Empty;
}
}
}
But I am getting an error on the line:
public cSecurity _csec;
"The type or namespace name 'cSecurity' could not be found."
This seems pretty trivial but I seem to be lost here.
Appreciate any insight. Thanks.
For some reason, the solution for this was for me to close and restart VS. Not sure what caused the class for it to be not referenced by VS.

Castle Windsor StartableFacility does not start

I was following the example (Aggressive old mode) given in:
http://docs.castleproject.org/Default.aspx?Page=Startable-Facility&NS=Windsor&AspxAutoDetectCookieSupport=1
Here is my full source code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Castle.Facilities.Startable;
using Castle.MicroKernel;
using Castle.MicroKernel.Registration;
namespace Test
{
public interface IStartable
{
void Start();
void Stop();
}
public class Startable : IStartable
{
public Startable()
{
Console.WriteLine("Created!");
}
public void Start()
{
Console.WriteLine("Started!");
}
public void Stop()
{
Console.WriteLine("Stopped!");
}
}
[TestFixture]
public class StartableFacilityContainerTest
{
[Test]
public void TestOperation()
{
IKernel container = new DefaultKernel();
container.AddFacility<StartableFacility>();
container.Register(Component.For<Startable>());
Console.WriteLine("Registered!");
container.Dispose();
Console.WriteLine("Released!");
}
}
}
However when I run it, I get:
Registered!
Released!
when I expect to get (as given in the example):
Created!
Started!
Registered!
Stopped!
Released!
Basically my Startable did not start.
This is tested in .Net 4.0 and Castle Windsor 3.0
What did I do wrong?
I'm using Installers. This helped me:
container.AddFacility<StartableFacility>(f => f.DeferredTryStart());
try
container.Register(Component.For<Startable>()
.StartUsingMethod(s => s.Start)
.StopUsingMethod(s => s.Stop);
The problem is you have created and implemented your own IStartable interface instead of just implementing the Castle.Core.IStartable

Categories