NoesisGUI - Loading PNG in Noesis.Image Element - c#

I am having troubles assigning a png image to Image element in XAML. At runtime the application crashes with exception:
InvalidCastException: Unable to cast object of type 'System.Drawing.Bitmap' to type 'System.Byte[]'.
My steps are as follows:
App.xaml.cs
protected override TextureProvider GetTextureProvider()
{
EmbeddedTexture[] textures =
{
new EmbeddedTexture {filename="icon.png", resource="icon"},
new EmbeddedTexture {filename="download.jpg", resource="download"}
};
return new EmbeddedTextureProvider(textures, Properties.Resources.ResourceManager);
}
MainWindow.xaml
<Image Source="icon.png"/>
I added icon.png in project dir with build action "Resource" and added the same in Resources.resx file.
I ran the Menu3D sample from Github which loaded png images without any trouble. Can someone please point out what I might be doing wrong? I'd appreciate the help.
Thanks.

I posted this question on their forums and got the workaround here:
http://www.noesisengine.com/forums/viewtopic.php?f=3&t=1404
The workaround was to edit Resources.resx file and replace "System.Drawing.Bitmap, System.Drawing" with "System.Byte[], mscorlib" for every image file. Also change the getters in Resources.Designer.cs and replace "Bitmap" type with "Byte[]" type.

Related

Included file generates error "value cannot be null"

In my application, i have some zip files:
It is present in the Resources.Designer.cs:
internal static byte[] ContactDocuments
{
get
{
object obj = ResourceManager.GetObject("ContactDocuments", resourceCulture);
return ((byte[])(obj));
}
}
I have this code to extract the file tp c:\temp:
File.WriteAllBytes(#"C:\Temp\ContactDocuments.zip", Properties.Resources.ContactDocuments);
The solution is build without any errors.
However, when I run the application, I get the error"value cannot be null" at the point the above code is executed.
I have been looking at the properties of the zip file, but i did not find a solution.
#mjwills was pointing in the right direction.
I had to "Right click on the project. Click Properties. Click Resources" and add the resource:
Now, i can access this resource when running the application.
Thanks

Xamarin Forms - Load image in a Button

I looked extensively online but I couldn't find anything really specific to help me out in this trouble I am having.
I am trying to make a custom renderer for a Button component, in Xamarin forms. Why am I not using the XLabs ImageButton component? Because it's not compatible with Android 4.0.3, which is my current Android target version.
I also tried writing on my own some code that is that much worthless that I am quite shy to post, unless you really require me to :)
I am in need of this code because, without it, images just fail to load in buttons while trying to use them in any button like this:
<Button Image="someImage.png"/>
I tried following a tutorial, here:
http://blog.falafel.com/learning-xamarin-custom-renderers-in-xamarin-forms/
But I failed adapting it to my needs.
Are no images being displayed at all? Check your locations of the images. Default location in ios is Resources folder, in android it is resources/drawable and in wp it is the root. If you want one image rather than 3 for each solution, put in to the PCL and set the build property to embedded resource and qualify the name with the namespace it is in. ie... ProjectName.Assets.image.png if it is in the PCL Assets directory. Not sure if this is your problem or not, but figured I would offer it up , just in case.
You can solve this by making an Image tap-able using TapGestureRecognizer. For implementation, go through this: http://www.c-sharpcorner.com/UploadFile/e04e9a/xamarin-forms-image-button-recipe/
The proper way to do this is described in Chapter 13 of Charles Petzold's excellent book on Xamarin Forms: https://developer.xamarin.com/guides/xamarin-forms/creating-mobile-apps-xamarin-forms/
My approach to this problem is (different than the book) to use a converter for the image file path. The Button.Image property is a FileImageSource object and it wants a file path. Unfortunately you cannot use embedded resources or a content file in the PCL. You must add an individual image file in each of the iOS, Android and UWP projects. The way I do this is to add the image to the PCL and use linking (option on the add existing file dialog).
So here is my converter approach to the above issue
<Button Image="{Binding Converter={StaticResource FileImageSourceConverter}, ConverterParameter=someImage.png}" />
The static resource...
<ContentPage.Resources>
<ResourceDictionary>
<local:FileImageSourceConverter x:Key="FileImageSourceConverter"/>
</ResourceDictionary>
</ContentPage.Resources>
The converter...
public class FileImageSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string filename = parameter as string;
switch(Device.RuntimePlatform)
{
case Device.iOS:
case Device.Android:
default:
return filename;
case Device.Windows:
return Path.Combine("Images", filename);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
The advantages of this approach is that (1) your XAML is not cluttered up with OnPlatform elements; (2) you don't need to put your images in the root directory of the UWP project; (3) much simpler than using a custom render to solve the same problem as some have suggested.

How can I access a resource image within XAML in a user control library?

I'm writing a library of WPF user controls and am having trouble with a resource image that I'm trying to access via some XAML. Just for fun, the image displays as expected at design time and only fails at run time.
I've tried setting the build action to "none", "content", "resource" and "embedded resource", but I keep getting the following cryptic exception:
'Provide value on
'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an
exception.' Line number '18' and line position '6'.
Looking at the inner exception, it seems to be a problem with the pack URI. When I try:
<ImageBrush x:Key="mybrush" ImageSource="pack://application:,,,/Resources/an image.png" />
I get:
Assembly.GetEntryAssembly() returns null.
OK, so this is probably because my library is now being called from within another assembly. But when I follow the recommendations and try:
<ImageBrush x:Key="mybrush" ImageSource="pack://application:,,,my_assembly;component;/Resources/an image.png" />
I get an inner exception of:
"The URI prefix is not recognized."
To add to the fun, I have another image resource which I have no problem accessing via actual C# code (i.e. not through XAML).
What am I doing wrong? Feel like it will be really simple, but am head-desking at the moment.
Change ImageSource to
ImageSource="pack://application:,,,/my_assembly;component/Resources/your_image.p‌​ng"

How to correctly save a WF4 ActivityBuilder

I am currently saving my .NET FX 4.0.1 StateMachine activity like this:
var sb = new StringBuilder();
var xamlWriter = ActivityXamlServices.CreateBuilderWriter(
new XamlXmlWriter(new StringWriter(sb),
new XamlSchemaContext()));
XamlServices.Save(xamlWriter, activityBuilder);
return sb.ToString();
This works fine and the generated XAML looks good. Unfortunately, it is invalid. I can read it back in using ActivityXamlServices.Load but when I execute it, it says that it doesn't know the properties defined in the workflow. Opening it in the Visual Studio designer yields the same errors:
Compiler error(s) encountered processing expression "ActiveCall". "ActiveCall" is not declared. It may be inaccessible due to its protection level.
Through comparing the original XAML with the XAML produced by my code, I found out how to fix this problem. I have to have this tag before the StateMachine tag:
<mva:VisualBasic.Settings>
Assembly references and imported namespaces for internal implementation
</mva:VisualBasic.Settings>
By the way:
The text inside the tag must be exactly like this, otherwise there will be an error when opening the WF in VS:
Failed to create a 'Settings' from the text 'FooBar'
Question:
What do I have to change in my code to have this tag in the generated XAML?
I think I found the answer.
I have to use the following code before calling Save:
VisualBasic.SetSettings(activityBuilder, new VisualBasicSettings());
If the activityBuilder has been created from a DynamicActivity it is even better to use the following code:
VisualBasic.SetSettings(activityBuilder,
VisualBasic.GetSettings(dynamicActivity));
If this is not used, namespaces that are only needed for extension methods are not written to the XAML and will lead to an error when loading and executing the XAML.
I summarized my findings on code project.

StringTemplate: Loading a Template from disk?

I am using StringTemplate in c# and following code to load a template from a subdirectory of my application.
StringTemplateGroup group = new StringTemplateGroup("myGroup", "/tmp");
StringTemplate query = group.GetInstanceOf("Sample");
query.SetAttribute("column", "name");
Console.WriteLine(query);
I have a template file Sample.st in the tmp directory of my application.
I am getting the following error.
Unhandled Exception:
System.ArgumentException: Can't find
template Sample.st; group hierarchy is
[myGroup]
Does anyone know what is wrong here?
Probably you should specify absolute path as a second parameter for StringTemplateGroup constructor?
In addition to adding a reference to the Antlr3.StringTemplate assembly, you ALSO need to add a reference to the Antlr3.Runtime assembly (not necessarily the Antlr3.Runtime.Debug assembly, although this would also work). This worked for me.

Categories