I have a layout in my Xamarin Android project. I want to confirm that the resource is actually present in build.
I tried the following code:
var layout = Resources.GetLayout(Resource.Layout.my_xml_resource);
var xml = layout.ReadInnerXml();
System.Diagnostics.Debug.WriteLine(xml);
The GetLayout call does not throw the NotFoundException so presumably the resource exists. However, layout object, upon inspection, displays None.
The xml variable is empty and all attempts to read the xml are unsuccessful.
I am down this rabbit hole, because I am trying to use the layout with Inflate. Unfortunately, the output of inflate does not have the child controls I would expect and I suspect the resource layout is empty.
nativeView = inflater.Inflate(Resource.Layout.my_xml_resource, view, true);
What am I missing? Is there another way to verify the resource exists?
Use the Resources.GetIdentifier(String, String, String) Method could verify that Android Layout Resource Xml exists or not.
In C#, you could try the code below. If the resId not be 0, the resource exists.
int resId = Resources.GetIdentifier("textlayout", "layout", "com.companyname.app1");
textlayout: This is the layout name of my project, please note it need lower case.
layout: The type of resource you want to verify. It need lower case as well.
com.companyname.app1: PackageName, you could get from the AndroidManifest.xml file.
Related
Currently I am only testing on an Android emulator. I have installed the Theme Nuget packages.
In my App constructor I have:
// Load the desired theme (default to Light)
if (Current.Properties.TryGetValue("Theme", out object theme))
Resources = theme as ResourceDictionary;
else
Resources = new LightThemeResources();
I then have a method in the App class:
public async Task SwitchTheme()
{
// Switch the current theme from List to Dark to Light
if (Resources?.GetType() == typeof(DarkThemeResources))
Resources = new LightThemeResources();
else
Resources = new DarkThemeResources();
// Persist the Theme
Current.Properties.Add("Theme", Resources);
await Current.SavePropertiesAsync();
}
When I call the method the theme switches from light-dark-light etc. But when I restart the App, it always defaults to Light. As if the "await Current.SavePropertiesAsync();" did not work.
Can anyone suggest what the problem may be?
Xamarin Forms Properties is intended for use with C# value types and objects that can be easily serialized - not complex objects like Resources.
From the docs
Values saved in the properties dictionary must be primitive types,
such as integers or strings. Attempting to save reference types, or
collections in particular, can fail silently.
All you really need to do is store a string value - either 'light' or 'dark' and then load the appropriate theme based on that. You don't actually need to store the theme itself.
I have a webview and would like to show an image in the webview (html)
My HTML :
hello !img src="myimage.png" alt="myimage" height="42" width="42"!
(I used ! as tagend and tagstart, because I don't know how to add this here without be interpreted as HTML, even I pasted as code)
The myimage.png should be stored in app itself and not be loaded from a websource.
I don't know how to do that in a best practice way. Any help ?
UPDATE
I tried with referenced Article, but still not succeeded:
My Code for this:
let path:NSString = NSBundle.mainBundle().bundlePath;
var baseURL:NSURL=NSURL(fileURLWithPath: path as String)!;
var htmlString:String! = texttemp
myWebView.loadHTMLString(htmlString, baseURL: baseURL)
The same Image I can already load like the following -> works:
var image = UIImage(named: "myimage.png");
Your updated code isn't right. You are creating a path to the bundle, not to the specific file. You need to use the NSBundle method pathForResource:ofType (or one of its variants) to build a path to your file. Then use that to create the URL.
The pathForResource:ofType family of methods return nil if the file can't be found, so you should check that you are getting back a path.
EDIT:
Looking at it more closely, I see that you are using the URL as the base URL for a call to loadHTMLString. This does look like a sound approach. What is your HTML string, and where is the image in your bundle?
I am developing a form application with monodevelop. I'm using gtk#.
I need to add an image to a widget in the form. I am also trying to make this image an embedded resource as well, before including it in the form.
so far this is what I have:
HBox CharacterPic = new HBox();
Image LegionnairePic = new Image('somehow load the embedded resource image here');
CharacterPic.PackStart (LegionnairePic);
In the 'Solution' section to the left, I have added .jpeg files and changed their 'build action' to 'embedded resource'. However, I cannot access/load them onto the form them as so:
Image LegionnairePic = new Image(<namespace>.<resource>);
How do I add the image resource to the form? Am I even adding the resource correctly?
I believe you have to access the embedded files with the following method:
// string resource_id is the Resource ID of the file in the sidebar “Properties”
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resource_id);
This returns the resource file as a Stream.
If this helped, thank that guy like I did :)
I have an ASP.NET MVC 3 site that makes use of a ClientDependency framework for dep. resolution (CSS/JS).
My base path's are defined in /Shared/_Layout.cshtml like this:
#MvcHtmlString.Create(Html.RenderCssHere(new List<IClientDependencyPath> {
new BasicPath("Base", "~/Content/themes/base"),
new BasicPath("Content", "~/Content")
}))
I want to have one page without a standard layout. I force it by calling
#{
Layout = null;
Html.RequiresCss("FileUpload/fileUpload.css", "Content", 20);
}
However, I can no longer request a dep. like shown above, since "Content" path isn't defined.
I am rather new to ClientDependency framework, so which is the best way for me to get my dependencies in a non-layout view?
I need to have the Path declared somewhere in the View, as well as the RenderCss/JsHere element, so if I don't use a shared layout, I had to re-declare it in my custom page all together.
I am working on an application which needs to load fonts dynamically based upon the fonts used in a given document that the user opens. The fonts are used in a RichEditableTextControl so need to be CFF format.
If I add the code:
[Embed(source="/assets/fonts/AvenirLTStd Book.otf",
fontFamily="EmbedAvenir LT Std 45 Book",
mimeType="application/x-font",
embedAsCFF="true")]
public const embeddedFont:Class;
to the main SWF then the text displays correctly with the embedded font but moving the code to a separate file and adding a loader as per the information I found at the following link does not load the font - http://www.scottgmorgan.com/blog/index.php/2007/06/18/runtime-font-embedding-in-as3-there-is-no-need-to-embed-the-entire-fontset-anymore/
The loader code is:
private function loadFont(url:String):void {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fontLoaded);
loader.load(new URLRequest(url));
}
private function fontLoaded(event:Event):void {
var FontLibrary:Class = event.target.applicationDomain.getDefinition("FontAvenirLTStd") as Class;
Font.registerFont(FontLibrary.embeddedFont);
}
There is an error thrown at the Font.registerFont line to say that the parameter being passed cannot be null. I have checked in debug mode and the issue seems to be that the class exists but does not have any content. The FontLibrary class is instantiated but the only child entry in the debugger is _prototype so trying to access the embeddedFont property does return undefined.
At the moment the font SWF is in the assets folder of the main project so I don't believe there should be any security restrictions and, as I said, the SWF loading part appears to work.
One thing which is hampering my diagnostics is that I am not sure if the problem is the font SWF not being created correctly and having no content or if the main app is unable to load it. Any help on at least being able to narrow that down would be appreciated.
I would appreciate all the help I can get on this as I have been stuck at this problem for some time and it is a key part of the application.
Thanks in advance to everyone.
Just a quick note for anyone who ends up here from Google, the problem was that I had managed to lose the static keyword from the embeddedFont constant definition in the top block. It should have been public static const embeddedFont:Class;
Hope this helps someone.