Can't find Foundation.NSJavaScriptExtension class in app extension - c#

I'm developping an app extension for iOS with Xamarin Studio. I tried to use a class that is supposed to be in the Foundation API, but I can't find it. The class is called NSJavaScriptExtension.
I tried to access it by specifying the namespace:
Foundation.NSJavaScriptExtension
I also tried to use the using directive:
using Foundation;
When I tried compiling it, I got the following error message:
Error CS0103: The name 'NSJavaScriptExtension' does not exist in the
current context (CS0103)
This is really strange because the Xamarin documentation says that it exists.
Here's my code :
// Create an extension Item
var extensionItem = new NSExtensionItem ();
// Create a Final value Dictionary
var finalize = new NSMutableDictionary ();
finalize.Add (NSJavaScriptExtension.FinalizeArgumentKey, new NSString ("{bgColor:red}")); // <- here
// Create an item provider and attach it to the extension item
var provider = new NSItemProvider (finalize, MobileCoreServices.UTType.PropertyList);
extensionItem.Attachments = new NSItemProvider[]{ provider };
// Send results to the calling Host App
ExtensionHelper.ExtensionContext.CompleteRequest (new NSExtensionItem[]{ extensionItem },
(completionHandler) => {
return;
});

The type NSJavaScriptExtension was added in XI 9.0. The easiest way to get it is to upgrade to the latest stable release, which can target older iOS versions as well.
If you cannot update then you can load any constant manually at runtime, like:
using Foundation;
using ObjCRuntime;
...
IntPtr foundation = Dlfcn.dlopen (Constants.FoundationLibrary, 0);
NSString FinalizeArgumentKey = Dlfcn.GetStringConstant (foundation.Handle, "NSExtensionJavaScriptFinalizeArgumentKey");

Related

My System.CommandLine app won't build! It can't find a CommandHandler. Do I need to write it?

I am using VS 2022, .Net 6.0, and trying to build my first app using System.CommandLine.
Problem: when I build it, I get an error
The name 'CommandHandler' does not exist in the current context
The code I'm trying to build is the sample app from the GitHub site: https://github.com/dotnet/command-line-api/blob/main/docs/Your-first-app-with-System-CommandLine.md , without alteration (I think).
It looks like this:
using System;
using System.CommandLine;
using System.IO;
static int Main(string[] args)
{
// Create a root command with some options
var rootCommand = new RootCommand
{
new Option<int>(
"--int-option",
getDefaultValue: () => 42,
description: "An option whose argument is parsed as an int"),
new Option<bool>(
"--bool-option",
"An option whose argument is parsed as a bool"),
new Option<FileInfo>(
"--file-option",
"An option whose argument is parsed as a FileInfo")
};
rootCommand.Description = "My sample app";
// Note that the parameters of the handler method are matched according to the names of the options
rootCommand.Handler = CommandHandler.Create<int, bool, FileInfo>((intOption, boolOption, fileOption) =>
{
Console.WriteLine($"The value for --int-option is: {intOption}");
Console.WriteLine($"The value for --bool-option is: {boolOption}");
Console.WriteLine($"The value for --file-option is: {fileOption?.FullName ?? "null"}");
});
// Parse the incoming args and invoke the handler
return rootCommand.InvokeAsync(args).Result;
}
I have installed the latest version of System.Commandline: 2.0.0-beta2.21617.1
SURELY I am just being a big fat idiot in some respect. But I don't see it.
Any insight would be welcomed.
This issue is caused by updating the CommandLine 2.0 Beta 2 package. Add the reference System.CommandLine.NamingConventionBinder to the references to fix the problem. Follow the announcements on command-line-api's GitHub account:
In your project, add a reference to System.CommandLine.NamingConventionBinder.
In your code, change references to the System.CommandLine.Invocation namespace to
use System.CommandLine.NamingConventionBinder, where the CommandHandler.Create
methods are now found. (There’s no longer a CommandHandler type in
System.CommandLine, so after you update you’ll get compilation errors until you
reference System.CommandLine.NamingConventionBinder.)
If you want to continue with the old habits, try using older versions of the System.CommandLine package.
References
Announcing System.CommandLine 2.0 Beta 2 and the road to GA
Think you are missing a using line:
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
I can't swear that's it, but it looks like CommandHandler is defined in a namespace not referenced by a using (in your current code), so System.CommandLine.Invocation may be the key!

PyScope missing in Python .NET

My problem is that I'm trying to use Python.NET inside Visual Studio, I installed Python 3.5, and the python.NET package trough nuget and trough pip too.
added namespace Python.Runtime in my Form application, and the Python.Runtime.dll is there in the references too.
I tried to use a sample code from the offical site: offical site
using Python.Runtime;
// create a person object
Person person = new Person("John", "Smith");
// acquire the GIL before using the Python interpreter
using (Py.GIL())
{
// create a Python scope
using (PyScope scope = Py.CreateScope())
{
// convert the Person object to a PyObject
PyObject pyPerson = person.ToPython();
// create a Python variable "person"
scope.Set("person", pyPerson);
// the person object may now be used in Python
string code = "fullName = person.FirstName + ' ' + person.LastName";
scope.Exec(code);
}
}
The Py.GIL() part works and I already tried to import numpy package and do some basic calculations with it, it worked well.
However the PyScope is just not recognized, nor do Py.CreateScope.
("The type or namespace PyScope could not be found")
Tried to write Python.Runtime.PyScope, tried reinstalling, tried older package, used console app and winforms app too, however nothing seems to work.
Am I missing something here?
I ran into this problem too. Using var instead of PyScope worked for me. As in:
using (var scope = Py.CreateScope())
Edit & Alternative: when I visited the definition of CreateScope(), the output type was PyModule:
public static PyModule CreateScope();
public static PyModule CreateScope(string name);
Using this instead of var also works for me:
using (PyModule scope = Py.CreateScope())
If that doesn't work for you, visiting the definition of the function and using the output type listed in your version likely will.
The var isn't picky though.

C# Azure and IOT Reference to type 'stream' claims to be defined in 'System.RunTime', but it could not be found

I am trying to create the sample on the following website. Its suppose to allow me to send data to Azure.
I looked up this error but I am not using the Interactive window and #r doesn't seem to be the answer. I believe that this issue prevents me from the use of my title, but I will try.
https://catalog.azureiotsuite.com/details?title=Raspberry_Pi3&source=home-page
"Reference to type 'stream' claims to be defined in 'System.RunTime', but it could not be found."
The line referring to Message gets this and the line below with the await gets it for 'Task'
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Client;
using Windows.UI.Xaml.Controls;
namespace MyFirstIotCoreApp
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
SendDataToAzure();
}
private async Task SendDataToAzure()
{
DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(<replace>, TransportType.Http1);
var text = "Hello, Windows 10!";
var msg = new Message(Encoding.UTF8.GetBytes(text));
await deviceClient.SendEventAsync(msg);
}
}
}
Good Morning;
Anytime I see an error referencing a missing item in a Framework library like
that, the first thought I have is to check the Framework version. Some functions
exist only in certain versions of the Framework as they develop new features.
Check the Framework version to see if you have selected the version that supports
that feature.

Setting Sqlite ThreadingMode when using sqlite-net + async

I'm trying to set Sqlite ThreadMode to Serialized in my Xamarin.iOS project. Using classic Sqlite, one finds this method on the connection class:
SqliteConnection.SetConfig(SQLiteConfig.Serialized);
However I'm using sqlite-net (+async) and can't find a way to set this configuration property.
Any suggestions?
Ok, I had to update SQLite-Net (and SQLite-Net-PCL etc) to 2.5.
Then this option can be accessed through the platform specific implementation:
public Repository(IPlatform platform)
{
_platform = platform;
_platform.SqlitePlatform.SQLiteApi.Config(ConfigOption.Serialized);
_db = new SQLiteAsyncConnection(() =>
new SQLite.Net.SQLiteConnectionWithLock(
_platform.SqlitePlatform,
new SQLite.Net.SQLiteConnectionString(_platform.DatabasePath, true)));
}

Use Google Calendar API in User Control : System.IO.FileNotFoundException

I fail when trying to use the Google API v3 in a user control.
Steps:
Create a new project in Visual Studio Express 2013 Update 4. Target framework: .NET 4.5 (tried 4.0 too)
Install NuGet package "Google.Apis.Calendar.v3 Client Library" and all and all dependencies
Add new User Control "MyControl"
In MyCalendar_Load add the following line: CalendarService service = new ServiceCalendar();
Add using Google.Apis.Calendar.v3;
So the code of my control looks like:
using System;
using System.Windows.Forms;
using Google.Apis.Calendar.v3;
namespace Google_Calendar_Test
{
public partial class MyCalendar : UserControl
{
public MyCalendar()
{
InitializeComponent();
}
private void MyCalendar_Load(object sender, EventArgs e)
{
CalendarService service = new CalendarService();
}
}
}
Then I compile my solution so I get my newly created MyControl. When I try to put the control on the form the following error message appears:
System.IO.FileNotFoundException / Could not load file or assembly Google.Apis,
In the form I can add the CalendarService and start it without any problems. I'm puzzled.
Update
The version in the error message (V 1.0.0.23042) doesn't match to the version NuGet installed (V 1.0.0.26011). But this doesn't make any sense to me.

Categories

Resources