Add broadFileSystemAccess capability to Unity+UWP - xaml

I am trying to add the capability named broadFileSystemAccess to our UWP application that is built with Unity and has no XAML markup. The documentation says to add the following to the app manifest, but no app manifest exists in the unity / visual studio project. Is this capability not possible on MRP, or are we doing something wrong?
This is what is supposed to be added to your App package manifest, but like I mentioned, we don't have one of those.
<?xml version="1.0" encoding="utf-8"?>
<Package
...
xmlns:uap4="http://schemas.microsoft.com/appx/manifest/uap/windows10/4">
...
<Capabilities>
<uap4:CustomCapability Name="CompanyName.customCapabilityName_PublisherID"/>
</Capabilities>
</Package>

You can add capabilities as follows: PlayerSettingsWSA Capabilities
If a capability is not in the list you must build your project with IL2CPP in order to output an app package manifest named Package.appxmanifest and modify the XML by yourself.
In order to make things magically
You can write a method which have a PostProcessBuildAttribute attibute like so: Unity - Scripting API: PostProcessBuildAttribute that will verify and add the XML tag if it not present in the Package.appxmanifest.
Also you can create your custom build like that: Unity - Manual: Build Player Pipeline and add the same logic to verify and add the capability (an XML tag)
Pseudo code:
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
public class MyBuildPostprocessor {
[PostProcessBuildAttribute(1)]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) {
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load($"{pathToBuiltProject}/{PROJECT NAME HERE}/Package.appxmanifest");
// Find if the `broadFileSystemAccess` capability exists
// add `broadFileSystemAccess` capability
}
}

Related

Dynamically include precompiled (Razor) views in .NET Core 2.1

Idea
I'm trying to build a modular .NET Core MVC application, were I can add 'modules' (extensions) just by copying the required libraries to a predefined folder. The application defines a set of dependencies (DI), error handling and basic layout; the modules provide the actual application or page logic.
We've build a proof of concept, using ExtCore - the concept works great, Controllers are added automatically and the 'plain' Views are recognized easily (the ones set as 'Embedded Resource').
Challenge
However, I don't like the fact that Views are set to 'Embedded Resource', as it will be a small performance hit as the Razor page is now build at runtime.
What we've tried
As .NET Core 2.1 now has the ability to create Reusable Razor UI, I've created a project with a reference to <Project Sdk="Microsoft.NET.Sdk.Razor"> and compiled it to a Razor library (*.Views.dll).
When I link the Razor View library as project dependency in (main) application, everything works fine. However, the goal still is to make the application fully plug-and-play and allow libraries to be included just by deploying them to a folder.
I've tried to achieve this extending the Razor View Engine using a PhysicalFileProvider, but it won't recognize the views in the library.
services.Configure<RazorViewEngineOptions>(opt =>
{
opt.FileProviders.Add(new PhysicalFileProvider("path/to/extension"));
});
The result always is an exception telling me that /Views/[controller]/[action].cshtml is not found.
Searching other answers I came across this project, which creates a ViewLocationExpander. I tried implementing various versions of this idea without luck - most likely because this is an older concept based on .NET Core 1.x which still uses a project dependency; although the Razor View engine now searches the namespace of the assembly.
services.Configure<RazorViewEngineOptions>(opt =>
{
opt.FileProviders.Add(new PhysicalFileProvider("path/to/extension");
opt.ViewLocationExpanders.Add(new DynamicLocationExpander());
});
Where DynamicLocationExpander looks like:
public class DynamicLocationExpander: IViewLocationExpander
{
public DynamicLocationExpander()
{
}
public void PopulateValues(ViewLocationExpanderContext context)
{
}
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
string assembly = ((ControllerActionDescriptor)context.ActionContext.ActionDescriptor)
.ControllerTypeInfo.AsType().Assembly.GetName().Name;
foreach (var viewLocation in viewLocations)
yield return $"{assembly}/{viewLocation}";
}
}
Question
How can we dynamically link a Razor View library, containing precompiled views, to the Razor Engine in a (parent) application?

"The type or namespace name 'App' could not be found" Xamarin Forms

I am trying to create a simple app in Xamarin Android with Xamarin Forms using Visual Studio, however I keep getting this error everytime I try to build. I have double checked my references which should contain the Portable project, as well as made sure I have the latest version of Xamarin.Forms. What could I still be missing that would give this error?
using System;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using App2;
using Xamarin.Forms.Platform.Android;
namespace App2.Droid
{
[Activity(Label = "App2", Icon = "#drawable/icon", MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity :
global::Xamarin.Forms.Platform.Android.FormsApplicationActivity // superclass new in 1.3
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App()); // method is new in 1.3
}
}
}
In my case, I found there is a bug with Visual Studio 2017 when making a cross platform Xamarin Form app with the generated code if there is a dash in the filename.
It looks like it tries to snake case the title and then complains about not finding that file.
For example, for "my-project", it starts looking for "my_project" in some areas of the generated code.
Using a project name with no symbols seems to solve the issue.
If you're still having issues check to see that the PCL project is referenced under the references in the solution explore
the refrences option in the solution explore
Right click on the references option and click add reference.
in the dialogue box, select the PCL project and click okay.
this should solve the error
Found the source of the error! If anyone else is struggling, try updating Xamarin.Forms using NuGet Packages for Solution. I was only using the Package Manager Console which was not giving me the latest version.
Check whether the namespace you defined in pcl under which the class App() is created is defined in Android project. For example, If your pcl class App() is present under the namespace called App2, then you need to add this namespace in Android project as follows,
using App2;
Check this out if the problem is still not resolved :
Go to
project -> Reference - right click and select -.> Add reference -> from left side
select Project -> solution -> then (check ) select Project name --> OK
finish
I was renaming the projects of the solution and had this same error. What I have done is delete the reference to the .Net Standard project and readded with the new name to the Android project.
My case is really rare but I had the same issue and the problem was that I referenced two pcl projects that referenced Xamarin.Forms (one standard shared project and another my custom library with UI controls). Removing dependency to the second one solved the problem.
This answer is not related to Xamarin, but might help others in the future seeing the same error message. This was an ASP.NET 5.0 Blazor Web Assmebly project and I was developing using Visual Studio for Mac.
I got this error when I had renamed a project from Foo to FooBar.
It turns out, VS did not refactor the namespaces in any classes I created, so the root namespace was left as Foo, which was where the startup Program class was, yet the framework was searching for the App.razor file in the project root, which had been renamed to FooBar.
To fix this, I simply renamed the namespace of the Program class from Foo to FooBar.
In my case, the android and iOS projects could not find the PCL.
I had to import the PCL as a project into Android and iOS. Rebuild solution and you should be fine!

How to view the procedural code for any xaml file

I'm working in Visual Studio 2012 with xaml to create an application. When I make a new solution and WPF project and look at the xaml in the application file, I see xmlns, a startupuri, and a tag for application.resources.
When I want to see the code-behind that these tags are creating, I have only the .cs file with maybe a few things in it and the mysterious InitializeComponent() that performs all of the parsing. Here's an example of what it looks like:
public partial class MainWindow : Window //This is generated with a wpf project.
{
public MainWindow()
{
InitializeComponent();
}
}
If possible, how can I view the procedurally generated code that InitializeComponent is creating?
I don't think that the complete xaml source gets compiled into .g.cs files.
Quoting this codeproject article:
When you compile a WPF application in Visual Studio it will compile your XAML files into a compressed representation known as Binary Application Markup Language (BAML). The BAML is then saved as a resource in the resultant assembly. When that assembly is loaded and the resource is requested, the BAML is streamed out and very quickly turned into the object graph described by the original XAML.
In the project directory, under the obj and Debug folders should be files labeled .g.cs that correspond to existing files. Presumably, these are Generated C-Sharp files that contain the generated code that I was looking for.

Metro Style App MFT "Class Not Registered" Error

I am currently designing an app with the Metro app framework which includes a live video chat feature. I am using the GrayscaleTransform MFT included in the MediaCapture sample (at this point simply copy-and-pasted from the sample).
However, when I try to add the grayscale effect to the camera's image stream, I get a "class not registered" fatal error. I understand this is because I must 'activate and register' the media extension, but I do not know how. How do I register the media extension?
All help is greatly appreciated and I always accept an answer!
UPDATE: My GrayScale IDL file is shown below:
import "Windows.Media.idl";
#include <sdkddkver.h>
namespace GrayscaleTransform
{
[version(NTDDI_WIN8), activatable(NTDDI_WIN8)]
runtimeclass GrayscaleEffect
{
[default] interface Windows.Media.IMediaExtension;
}
}
The media extension is specified as an <Extension> (or extensibility point) in the package manifest's Extensions section, but you need to insert it manually (i.e. open the appxmanifest as code instead of double-clicking.)
Using the GrayscaleTransform example, in the Media extensions sample, open the MediaExtensions project's package.appxmanifest (as code) and look for this in the <Extensions> section:
<Extension Category="windows.activatableClass.inProcessServer">
<InProcessServer>
<Path>GrayscaleTransform.dll</Path>
<ActivatableClass ActivatableClassId="GrayscaleTransform.GrayscaleEffect" ThreadingModel="both" />
</InProcessServer>
</Extension>
There's a bit more general info on extensions in App contracts and extensions.
As Chris Bowen explains in his answer, your application's AppXManifest is missing the required Extension elements for the activatable classes in the media extensions modules. All (non-Windows-provided) activatable classes need to be listed in the AppXManifest. The solution of adding the Extension nodes to the AppXManifest yourself will work, and this is what the MediaExtensions sample applications appear to have done.
However, you should not normally need to hand-edit the list of extensions. If you add a reference to a Windows Runtime Component project, a loose WinMD file, or an extension SDK, the build should automatically generate Extension elements for each of the activatable classes in the referenced components.
The reason this is not happening is that the media extensions are not annotated with the [activatable] attribute in IDL, so they are not attributed with the ActivatableAttribute in the WinMD that is generated. Instead of hand-editing the AppXManifest, you can declare the type as activatable in its IDL definition.
For example, to update GeometricSource.GeometricSchemeHandler, you can change its definition in IDL from:
[version(NTDDI_WIN8)]
runtimeclass GeometricSchemeHandler
{
}
to:
[version(NTDDI_WIN8), activatable(NTDDI_WIN8)]
runtimeclass GeometricSchemeHandler
{
[default] interface Windows.Media.IMediaExtension;
}
Note the added activatable attribute and the added [default] interface. If you make these changes to each of the extensions and clean/rebuild, you should not need to explicitly specify the activatable types in your AppXManifest: the build system will add them automatically.

MEF + SL4 question

I'm working on an app in the Silverlight 4 RC and i'm taking the oppertunity to learn MEF for handling plugin controls. I've got it working in a pretty basic manor, but it's not exactly tidy and I know there is a better way of importing multiple xap's.
Essentially, in the App.xaml of my host app, I've got the following telling MEF to load my xap's:
AggregateCatalog catalog = new AggregateCatalog();
DeploymentCatalog c1 = new DeploymentCatalog(new Uri("TestPlugInA.xap", UriKind.Relative));
DeploymentCatalog c2 = new DeploymentCatalog(new Uri("TestPlugInB.xap", UriKind.Relative));
catalog.Catalogs.Add(c1);
catalog.Catalogs.Add(c2);
CompositionHost.Initialize(catalog);
c1.DownloadAsync();
c2.DownloadAsync();
I'm sure I'm not using the AggregateCatalog fully here and I need to be able to load any xap's that might be in the directory, not just hardcoding Uri's obviously....
Also, in the MainPage.xaml.cs in the host I have the following collection which MEF puts the plugin's into:
[ImportMany(AllowRecomposition = true)]
public ObservableCollection<IPlugInApp> PlugIns { get; set; }
Again, this works, but I'm pretty sure I'm using ImportMany incorrectly....
Finally, the MainPage.xaml.cs file implements IPartImportsSatisfiedNotification and I have the following for handling the plugin's once loaded:
public void OnImportsSatisfied()
{
sp.Children.Clear();
foreach (IPlugInApp plugIn in PlugIns)
{
if (plugIn != null)
sp.Children.Add(plugIn.GetUserControl());
}
}
This works, but it seems filthy that it runs n times (n being the number of xap's to load). I'm having to call sp.Children.Clear() as if I don't, when loading the 2 plugin's, my stack panel is populated as follows:
TestPlugIn A
TestPlugIn A
TestPlugIn B
I'm clearly missing something here. Can anyone point out what I should be doing?
Thanks!
I think most of what you are doing is fine. Although ObservableCollections do support notifications of individual elements being added and removed, MEF doesn't take advantage of this. In your case it will simply clear the collection and then add all the plugins. Since you are using OnImportsSatisfied for the change notification, you don't even need an ObservableCollection. You could just use an IEnumerable for your import.
To add flexibility in downloading different xaps, I would expose a service in your container that can be imported and that provides the functionality to download a xap given a url. Then any component in your container can trigger a download, and the url to download can come from whatever source you deem appropriate.