Uno-Platform: How to embed JavaScript libraries in Uno-Platform Library? [closed] - uno-platform

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
TL,DR: How do I embed JavaScript into an Uno-Platform Cross Platform Library project?
BACKGROUND
Below are some great resources on how to embed a JavaScript library into a WASM platform project of an Uno Platform Application (not a Library):
KazukiOta from Microsoft
Uno Bootstrap docs
QUESTION
However, the above does not address, nor have I been able to find anything on, how to do this within an Uno Platform Library. How do I embed JavaScript into an Uno-Platform Cross Platform Library project?

OK, I'm pretty embarrassed at how easy it was to get it to work. After I created my Uno Platform Library project using Uno Solution Templates in Visual Studio 2019, I then:
Created a WasmScripts folder in the root of my library project
Added my JavaScript file (LibTest.js) to the WasmScripts folder:
function libTest() {
return 'IT WORKS!';
}
Created a class (Test.cs) that will use the above JavaScript function when targeting NetStandard - which is the target used by Uno WebAssembly apps:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#if !NETFX_CORE
using Uno.Foundation;
#endif
namespace WasmTricksLib
{
public partial class Test
{
public static string PerformTestAsync()
{
#if __WASM__
var result = WebAssemblyRuntime.InvokeJS("libTest()");
System.Diagnostics.Debug.WriteLine("WasmTricksLib.Test.PerformTestAsync: RESULT=" + result);
return result;
#else
return "NOT THE RIGHT PLATFORM";
#endif
}
}
}
The above results in the following project structure for the library:
So far, all the steps are what I expected to do, based upon the documentation for Uno-Platform apps projects. However, it won't compile and VisualStudio IntelliSense will not like some of the code in Test.cs.
THE SOLUTION
Add the following <ItemGroup> to the Uno-Platform Library's .csproj file:
<ItemGroup Condition=" $(TargetFramework.StartsWith('netstandard2.')) ">
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Filter" Version="1.1.1" />
<PackageReference Include="Uno.UI.WebAssembly" Version="3.3.0" />
</ItemGroup>

This link shows how to do it - https://platform.uno/blog/how-to-embed-javascript-components-in-c-built-uno-webassembly-web-applications/
It has two examples on using a date/time picker control Flartpickr as well as syntax visualizer PrismJS.

Related

How to use .NET 6-windows and .NET 6 targets in one library

I've segregated my WinUI 3 application into different layers: Application, Infrastructure, Presentation etc.
And all the projects have targets: net6.0-windows and I want to move it to the Uno Platform.
So, I've added a new target: net6.0
And at this moment the problem arises:
Type [WinUI] component already defines a member, called InitializeComponent or some problems with binding.
Is it possible to make such type of library with targets to net6-windows and net6.0?
For the Windows app project itself, you need to use net6-windows, as that provides additional dependencies specific to Windows app projects. All other libraries (non-app projects) can then use net6.0.
Regarding the specific error message, you are getting, the reason might be the generated files have some kind of conflict - you can try deleting the obj and bin folders and rebuild.
The easiest way to make existing Windows app support Uno would probably be to create a blank Uno solution and then migrate the code there (as the solution has already the required setup for platform-specific projects prepared and you can then just add your code.
Uno also provides templates for cross-targeted libraries, so you might be able to use similar approach. The one linked is for "UWP" solution however, so to make it WinUI, you would need to switch from uap10.0.18362 to net6-windows.

How do I include multiple proto files in a .NetCore gRPC project?

I'm creating a .NetCore C# desktop app using gRPC.
I'm attempting to use the Orchestrator pattern (microservices).
The basic design is:
Client (requester) <--> Orchestrator <--> multiple different request handlers
Each RequestHandler will have it's own set of messages.
Is it possible to have "Client" GrpcServices within the same project file,
or do I need to include all my proto message definitions within a single file?
With the setup listed below, my projects fail to load.
Here is the related snippet from my .csproj file:
<ItemGroup>
<Protobuf Include="../Protos/linuxservice.proto" GrpcServices="Client" ProtoRoot="../Protos/">
<Link>Protos/linuxservice.proto</Link>
</Protobuf>
<Protobuf Include="../Protos/orchestrator.proto" GrpcServices="Server" ProtoRoot="../Protos/">
<Link>Protos/orchestrator.proto</Link>
</Protobuf>
<Protobuf Include="../Protos/service.proto" GrpcServices="Client" ProtoRoot="../Protos/">
<Link>Protos/service.proto</Link>
</Protobuf>
Protobuf Include="../Protos/windowsservice.proto" GrpcServices="Client" ProtoRoot="../Protos/">
<Link>Protos/windowsservice.proto</Link>
</Protobuf>
</ItemGroup>
Well after so much trial and error I managed to create several props and that each one of them is consumed by its respective service.
Create your proto file
Check the properties of the file.
Build Action => Protobuf, compiler grpc subclasses => server only,
Compile protobuf => yes
Recompile the project and this automatically generates the Base class
add your service in app.MapGrpcService
I put the link where he gave me a guide, I hope it helps you.
https://medium.com/#lukastosic/part-1-grpc-its-fairly-simple-c-example-d4b266c4c72e
Blessings

Can't Resolve Platform Effect in Xamarin Forms component

I am having issues with Platform Effects in Xamarin Forms. These effects worked in a shared library format, and we are now migrating to .NET Standard 2.0 for reasons outside of the scope of this post.
Project Setup
VS 2017 15.8.0
Xamarin Forms 3.1.0.697729
Two .Net Standard 2.0 projects (one is for components, the other for the main UI)
Two Android projects (one that runs, the other is an Android .dll)
Two iOS projects (same deal)
Main Issue
None of my platform effects in the Android .dll work. They all resolve as null effect.
According to the documentation, my setup is correct. (Documentation here)
I found two different issues and have hit a brick wall.
When I follow the instructions exactly and place [assembly: ResolutionGroupName("MyGroupName")] on the platform renderer itself, I get exceptions with Xamarin's dependency resolver. There is no message, just an exception and stack trace that leads back to the constructor of the RoutingEffect.
When I use a slightly different pattern and register that on the namespace of an empty file in my .NET Standard 2.0 project, all effects return as null effect.
I've been Googling and digging through forums to no avail. Does anyone have an idea what could be the problem?
Side notes: All of the custom renderers work after using the static Init() trick, but not the effects. I have also tried making the typeof() statement reference both the Android version of the class and the .NET Standard 2.0 version of the class.
For the sake of completeness, here's what I have going.
NET Standard 2.0:
namespace MyBaseNamespace.Components.Effects
{
public class SearchBarStylingEffect : RoutingEffect
{
public SearchBarStylingEffect() : base("MyGroupName.SearchBarStylingEffect")
{
}
}
}
Android:
[assembly: ResolutionGroupName("MyGroupName")]
[assembly: ExportEffect(typeof(SearchBarStylingEffect), "SearchBarStylingEffect")]
namespace MyBaseNamespace.Components.Android.Renderers.Effects
{
public class SearchBarStylingEffect : PlatformEffect
{
…
}
}
(iOS is the same as Android, only the namespace is different)
I also posted this on Xamarin's forums, and Billy Liu from Xamarin asked a question that ultimately led me down the right path. I'll link to that here and also post what fixed it.
link: Xamarin Forms Post
Here's the resolution from that post:
In short, I had to do the following things to get this functional
(which it now is):
Ensure that the GroupNameResolution tag was on the first Effect ever initialized in the app.
Remove the empty file initializer from my .Net 2.0 project.
???
Profit.

InitializeComponent not defined in current context- Xamarin Form

We are developing Shared Code based Xamarin Forms app to be deployed across Android and iOS platforms. We are facing this strange issue number of times. There is compile time error on InitializeComponent which didn't let code to compile. We have gone through almost all the stackoverflow questions and xamarin discussions on this topic. None to them helped.
Following is code-
DocumentSearchPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace FinderApp.Views
{
public partial class DocumentSearchPage : ContentPage
{
public DocumentSearchPage ()
{
InitializeComponent ();- This line show compilation issue- InitializeComponent doesn't exist in current context
}
}
}
DocumentSearchPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="FinderApp.Views.DocumentSearchPage">
<ContentPage.Content>
</ContentPage.Content>
</ContentPage>
Till now we have tried-
Changing property custom tool to different values
Checking if Namespace names are matching in xaml and cs file
Updated xaml forms to v2.3.0.38 pre2. Obiviously we have tried with stable version first.
Clean Rebuild solution
Enviroment
iMac OS X 10.11.2
Xamarin Studio 5.10.3
Xamarin Forms 2.3.038 pre2
We are facing issue on windows8, visual studio 2015 also.\
We have this issue with PCL approach too.
Expert help needed!
If you are using VS 2015, seems naive but this is the state even as of today, no reasons to explain, but as of Aug 2016 and latest xamarin this is the status
Close and repoen solution
Clean Solution
Rebuild Solution

Inishtech / Software Potential Code Protector integration with Visual Studio build or Setup project

Have anyone tried InishTech Software Potential Code Protector ?
Can we integrate this with our build programmatically? I found tutorials but those were to do with the help of the Code Protector standalone GUI. What I want to know is do they have an SDK so that we can integrate with our code or some perfect tutorial which can opt-out dependencies from our setup.
UPDATE: There's been a Getting Started guide that covers code Protection added since I wrote the answer. (Exec summary: You add 2 NuGet packages and then put attributes on what you want to protect and protection is automatically integrated into the build process, be that from within Visual Studio or on a Build Server. Key simplifications are that you no longer need to add a SLPS_PROTECT Symbol or install an SDK anywhere.)
You can use the automatic Protection during building as detailed in the relevant KB article.
This hooks in an MSBuild-based build extension which will feed each assembly EXE/DLL as you compile it into the Command-Line Code Protector when you #define SLPS_PROTECT in Project| settings|Build tab|General area|Conditional compilation symbols (you might only want to do this for the Release configuration).
The Protected code then continues on its way, e.g., into your Setup Project or WebDeploy package.
Typically when using this approach, you declaratively mark the Feature to be associated with each Protected Method using the [Feature] attribute:-
[Feature]
void ProtectedCode()
{
...
}
You may also find further relevant details in the knowledgebase, FAQs and in the forum.
UPDATE: There's a new http://docs.softwarepotential.com/ which provides more focused getting started guides than there were at the time I answered the question. There are also a set of samples up at: https://github.com/SoftwarePotential/samples including installer-related ones.
Any further questions, please feel free to ask - either here or on the forum.
Disclaimer: I work for InishTech.
Code Protector is fantastic product from InishTech
I am using this for my startup and it works great and do it's job extensively well
just go for it

Resources