I've a class which implements Android.Webkit.WebViewClient in my C# Xamarin project. However, the following error show up after I built the project with a fresh-install Microsoft Visual Studio 2017 in another machine:-
'CustomWebViewClient.ShouldOverrideUrlLoading(WebView, IWebResourceRequest)': no suitable method found to override
I guess it's due to the installed AVD API Level as the previous overload was deprecated in Android Nougat but I'm not sure how to fix it. Can someone throw me some light?
Additional info:-
Installed AVD (Android 6.0 | API Level 23)
Overload bool ShouldOverrideUrlLoading(WebView, string) (this should be marked obsolete but it's not the case when I check the class definition in Mono.Android.dll)
You have to put the ShouldOverrideUrlLoading override into CustomWebViewClient class, if you want to have access to your webview from this class the override has the webview in the parameters.
Alright I think I've found the solution. I need to download the latest API Level via Android SDK Manager as the project was set to compile using the latest platform by default which is missing in the new machine.
Related
i am having the following problem. i try to invoke platform specific code in the .NET MAUI from the microsoft tutorial.
https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/invoke-platform-code
The problem here is that it can be compiled and builded with Visual Studio, but with Rider from Jetbrains it cant be compiled.
i m getting the following error message:
DeviceOrientationService.cs(5, 38): [CS8795] Partial method 'DeviceOrientationService.GetOrientation()' must have an implementation part because it has accessibility modifiers.
Anyone got an idea what i am missing?
So the thing is you need to create an implementation for your abstract method on all platform classes for it to be able to build since you are targeting multiple platforms.
So just like you have an implementation in your Android and iOS platforms you need it on others as well.
There is another way as well which is you can create an abstract method with an implementation that already does something on other platforms so assume this method is only relevant on Android and iOS then you would do something like below in this class:
public partial class DeviceOrientationService
{
#if ANDROID || IOS
public partial DeviceOrientation GetOrientation();
#else
public partial DeviceOrientation GetOrientation()
{
return DeviceOrientation.Undefiend;
}
#endif
}
Also if you don't want to support Tizen, Windows or MacCatalyst you can just remove their references from the csproj file and then you can delete their platform folders and you won't need to do the above-mentioned things at all your app will only expect Android and iOS code for the above project.
CoreWebView2.AddHostObjectToScript throws System.Exception: 'The group or resource is not in the correct state to perform the requested operation.'
Version
SDK: 1.0.1245.22 latest stable - (and Microsoft.UI.Xaml 2.8.0-prerelease.210927001)
Runtime:
Framework: UWP
OS: Windows 11, build 22000
Reproduce Steps
Create an UWP project
Add necessary nuget packages (Microsoft.Web.Webview2 latest stable release and Microsoft.UI.Xaml 2.8.0-prerelease)
Add WebView2 component to MainPage.xaml
Create HostObject class with necessary tags
Add code for navigation starting or navigation completed method in WebView2 (MainPage.xaml.cs)
Ensure CoreWebView2 is not null by calling myWebView.EnsureCoreWebView2Async(); inside method
Try use myWebView.CoreWebView2.AddHostObjectToScript("HostObject", hostObject); inside method
(Also tried to make a new project uwp (as Windows Runtime Component) and create the HostObject class there but still same exception. In addition tried to change tags on HostObject class but still same issue)
Example of HostObject class:
[AllowForWeb]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class JsCallbacksUwp
{
public JsCallbacksUwp()
{
}
}
Example code for Navigation Completed that throws the exception:
private async void WebView2_NavigationCompleted(WebView2 sender, CoreWebView2NavigationCompletedEventArgs args)
{
await sender.EnsureCoreWebView2Async();
var browserHostObject = new JSCallbacksUwp();
sender.CoreWebView2.AddHostObjectToScript("HostObject", browserHostObject);
}
I got my answer from github's community at WebView2 Feedback.
Firstly i followed the guide that Syul968 showed me there which was:
The WinRT CoreWebView2 class (the one used by WinUI) tries to automatically create a "wrapper" when this is the case, but it needs a DispatchAdapter instance to be set through CoreWebView2Settings.HostObjectDispatchAdapter.
We have also been working on this feature lately. Our latest pre-release package (1.0.1248-prerelease) includes a tool, wv2winrt, to help project new and existing WinRT components: Call native-side WinRT code from web-side code
Secondly i followed the steps from david-risney answer:
Our current documentation only shows you how to do it for OS provided WinRT APIs and not for your own WinRT APIs. We need to fix this. Until then I would recommend following the existing directions from Call native-side WinRT code from web-side code, and then additionally doing the following:
Add a third project to your VS solution that implements your winrt class.
Have the WinRTAdapter project 'Add a reference' to your new third project containing your winrt class.
Update the WinRTAdapter project's Include filter to also include your new class.
Add an additional line to InitializeWebView2Async to add your winrt class's namespace
WebView2.CoreWebView2.AddHostObjectToScript("MyCustomNamespace",
dispatchAdapter.WrapNamedObject("MyCustomNamespace",
dispatchAdapter));
And optionally add your namespace sync proxy as a global object in script window.MyCustomNamespace =
chrome.webview.hostObjects.sync.MyCustomNamespace; You should then be able to use your type via script.
Lastly you can check my changes to my example.
I am trying to implement GraphQL into Unity3D (version 2017.1.0f3 Personal). I am using .NET 4.6 (Experimental), but despite this, Unity does not support dynamic keyword. Which is odd, since .NET 4.0 it is the part of .NET. Except in Unity. I was googling for some solution how to get it work, but no solutions to the dynamic keyword. The error is this:
Severity Code Description Project File Line Suppression State
Error CS1980 Cannot define a class or member that utilizes 'dynamic'
because the compiler required type
'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you
missing a reference? skiing-prototype (1) D:\skiing-prototype
(1)\Assets\Scripts\GraphQL.cs 62 Active
That is the only caveat of using GraphQL C# client. Has anyone tried it yet to get it work? I hadn't found any greater efforts to get it up and running yet.
EDIT:
I am using this client here: https://github.com/bkniffler/graphql-net-client
Also this is an error from visual studio, but in Unity console it shows errors too, will update what exactly momentarily
Assets/Scripts/GraphQL.cs(80,16): error CS1980: Dynamic keyword requires
`System.Runtime.CompilerServices.DynamicAttribute' to be defined. Are you
missing System.Core.dll assembly reference?
this is the unity editor error, which seems to be the same that in visual studio
The first step is to check if Unity recognizes these 2 basic C# 6 features from MS site.
1.Try "Index Initializers" feature:
private Dictionary<int, string> webErrors = new Dictionary<int, string>
{
[404] = "Page not Found",
[302] = "Page moved, but left a forwarding address.",
[500] = "The web server can't come out to play today."
};
2. then "String Interpolation" feature:
private string FirstName = "";
private string LastName = "";
public string FullName => $"{FirstName} {LastName}";
If they give you error then the problem is not just the dynamic keyword but a problem that Visual Studio cannot recognize the .NET version being set by Unity.
From the comment section your Unity failed to compile the first example.
Go through the steps one by one for a possible fix. Do not skip of them.
1.Go to Edit --> Project Settings --> Player --> Other Settings --> Configuration --> Scripting Runtime Version --> Experimental (.Net 4.6 Equivalent).
2.Go to Edit --> Project Settings --> Player --> Other Settings --> Configuration --> Api Compatibility Level --> .NET 4.6
3.Restart Unity Editor and Visual Studio. You must restart both.
Test both C# features above. If they work then the dynamic keyword should as-well. If they don't then move on to #4.
4.Update Visual Studio. This is very important. Update the visual Studio to the latest version/patch.
5.If you can't still get both C#6 features above to compile then Re-install both Visual Studio and Unity then perform step #1 and #2 again as some files are missing.
6.Finally, if you get both C#6 features working but the dynamic keyword is still not working then update from Unity 2017.1 to Unity 2017.2. This version fixed many .NET issues.
Note that I am using Unity 2017.2 with the dynamic keyword without any issue. Also, GraphQL is working fine.
I seem to have found a solution
Navigate to Edit > Project Settings > Player > Other Settings > Configuration > API Compatibility Level and change from .NET Standard 2.0 to .NET 4.x
This immediately removed the compiler error and allowed me to run code using the dynamic keyword.
Let me know if that was useful
We start to prepare project with generic solution for few apps using Xamarin studio (the idea is to let all our apps use same "Core" components).
We started from preparing general "Core" with base functionality. Also we add "subCore" - libraries with functionality specific to each platform (iOS, android, windows etc).
Currently setup such structure for Android and iOS.
During second step of implementation for macOS, we faced with few blocking points.
The problem appear when we try to configure (in way described above) structure for macOS platform.
I was able to create macOS class Library with components that should be reused in apps (for now this is just 1 class with xib file). But, when I want to use this package (with xib inside) in macOS target, got exception:
External Modification Warnings: Debugger attached to process.
Application Specific Information:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[NSNib
_initWithNibNamed:bundle:options:] could not load the nibName: MainViewController in bundle (null).' terminating with uncaught
exception of type NSException abort() called
After some investigating, found that Mac Build server is not converting xibs into nibs, so this nib file is just missing in package created by Xamarin.
Additional testing info, based on proposed solutions found in Xamarin forum:
rename, recreate, reposition of xib file in project - not resolve the issue
recreate project - not resolve the issue
check Build action in property is set to InterfaceDefinition (also check all other possible variants) - not resolve the issue
I found this post, with root cause exactly as I have:
Mac Build server is not converting xibs into nibs.
According to discussion on this post, issue should be fixed, but I still faced with this.
Can some one advice how to fix described problem?
To anyone who faced same issue, please install
xamarin.ios-10.13.0.27.pkg
and
xamarin.mac-3.7.0.27.pkg
This build include fix for described problem.
For more, visit this page.
i have seen in the link below:
Can execute Code Dynamically in monotouch?
that it is impossible to use dynamic in ios xamarin. how about in Andoid xamarin?
I tried to do the following:
dynamic MyDynamic = new System.Dynamic.ExpandoObject();
MyDynamic.A = "A";
MyDynamic.B = "B";
However, when I want to access MyDinamic.A, it says that Unknown Member: A.
Can someone please help? Thanks.
Edit:I also have added the Microsoft.Csharp dll in the solution reference as per the screenshot:
I'm not sure which IDE did you use for developing your xamarin android app, by my side I used VS2015, and the error is
Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'
This is because when use the dynamic keyword in your project. The assembly contains the C# runtime binder. To solve this issue, we need to add reference to Microsoft.CSharp library(Microsoft.CSharp.dll) in our project:
After adding this reference, your code runs well by my side.
But I'm not sure if the Xamarin.Android will limit the use of dynamic object, through I didn't find any limitation in xamarin's official document. For more information about dynamic object, you can refer to:
System.Dynamic.DynamicObject Class
System.Dynamic.ExpandoObject Class
Go to NuGet Package Manager > Manage NuGet packages menu. Then change Source to Microsoft Visual Studio Offline Packages. After adding, it should compile successfully.