Custom control in reference does not render correctly - image

I have made a custom control (round button with a fixed image) and tested that it works. However, I would like to reuse this particular control in other projects and hence thought of making a class library out of it. However, when I try to get the custom control to show in my other applications, the icons does not show even though the button responds to clicks.
I have tried to build the icon in the class library projects as Content and Resources and test but to no avail. (of course, I change the addressing of the icon in the code when I updated its build icon). At the moment, i have decided to leave the icon /icons/myimage.png to be built as Content. And, in the class library code (XAML), i am accessing it as "/icons/myimage.png".
So, would anyone have an idea on how I could get the round button to render properly in my projects? How should I build the class library project?

I would't make a graphics part of my class library, because most likely in the future you will need to customize it, so what I suggest is to make the following property in your custom module and set the image where you use it:
public ImageSource ButtonImage {get { return <button image>; } set { <set button image>; } }

I experimented a bit and found the solution. The idea is to use embedded resource to store the image in the dll. And then for loading it by the CustomControl, one can use constructs like this:
BitmapImage img = new BitmapImage();
img.SetSource(Assembly.GetExecutingAssembly().GetManifestResourceStream("MyLib.icons.my_icon.png"));
MyLib here is the name of the assembly for the class library. icons is the folder where the resource is kept in the assembly.

Related

Custom renders of nuGet Package conflict in Xamarin.forms

I'm making app with using Xamarin.forms. (PCL)
I just noticed serious problem of using custom renderer.
I came from this thread.
https://forums.xamarin.com/discussion/54317/new-xamarin-forms-guide-custom-renderers/p2
This thread says, and I experienced that Only ONE renderer class works at the same time.
It means that if two different package use same renderer, one of them will not work. (It follows class hierarchy. so subclassed class will work and other stop working)
Package just added might break original package or mine, and real problem is that developer might not notice at that time.
Why is that how it works?
Or, am I not correct or did mistake?
example
If you already made your custom renderer for ContentPage and installed package which has custom renderer for ContentPage or Page. In my case, KeyboardOverlap.Forms.Plugin does. And Both Renderer class overrided OnApearing(). When you run your app. Only one renderer's OnApearing will be fired.
Of course I can hand merge that. But what if both of them are nuGet Package and can not modify them. (Of course I can use Github's but that cause another problem)
Each custom renderer should have a unique name. The only way you'll get collisions is if your renderer is overriding the base class... NEVER DO THAT, but instead you should inherit from the default and make sure to call base when you override.
If a nuget package is broken because they're overriding the default, you have to tell them to fix it.
In the code you're referring to, the author is overriding Page instead of making his own KeyboardOverlapPage
This line is causing you pain!
Instead it should be
[assembly: ExportRenderer (typeof(KeyboardOverlapPage), typeof(KeyboardOverlapRenderer))]
And then have a class in the PCL
public class KeyboardOverlapPage : Xamarin.Forms.Page {}
In other words, the author has a poorly implemented package and should fix it. Once fixed, you can use a wherever you want that functionality in place of a page.
If there is no custom renderer on a specific platform, it will automagically fall back to the default behaviour.

How to customize Xamarin.Forms app?

How can I customize the appearence of xamarin.forms components?
I want each button to have the same image, for instance. Or for all systems to share the same login screen with the same background image.
I know I can do that by adding one specific screen to each platform project. But I want to be able to customize the component itself. Example: All buttons will have the same background image no matter which platform is running.
I've read these:
http://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/
http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/styles/
http://code.tutsplus.com/tutorials/getting-started-with-xamarinforms-customizing-user-interface--cms-22144
The most promising one seems to be the last one. Is a custom renderer the only way to accomplish this? Can't I just add an image at the shared project and it automagicaly works for all platforms?
Thanks.
It's not absolutely clear, what exactly you want to reach... but I try to answer you.
If you use a shared project (based on template "Blank App (Xamarin.Forms shared)", you can use the same page / page-definition in all of your platforms.
You can add - e.g. a login-form in the shared-folder and then call it from code (e.g. from another page in the shared folder).
If you want to create your own controls, you can create "user-controls" and use it everywhere you want (what I recommend to every user).
You do this, by create a own class (e.g. with an Entry and a Label), implement the events and then instantiate it where you want to use it. E.G.
var OeFirma = new EntryErfassung(cDefaultText: "Firma", iMaximalLaenge: 45);
where OeFirma is the name of the object-instance on the page (to access it), EntryErfassung is my own class (that contains a description-label, an Entry an error-label and more) and cDefaultText is a parameter, that overtakes and set the default-text to the Entry and iMaximalaenge is a further parameter that is overtaken whereby my class then take care, that not more then 45 Characters are accepted for this Entry).
I have e.g. created a class with a description-label, a delete-button, an Entry and a error-label ad use it on all data-entry-forms.
If you want to change the look-and-feel of a specific control for a specific platform, you can create a "custom-render" (I have done this e.g. to change the font-size to the edit specific for iOS, as the font-sinze cannot be set in the XF-Entry control).
Hope this answers your question...
To do this, you can just specific a style in the App class, (you will need to change the app class to be made up of 2 partials: App.xaml & App.xaml.cs, and then create a ResourceDictionary to hold your customisations.
<Style TargetType="Button">
<Setter Property="Image" Value="MyImage.png"/>
</Style>
Hope that helps.
Cheers,
Tristan

How to add method to MFC-ActiveX

The question seems to be stupid since there are many explanations in internet, that describe how to add a new method that can be called by users of the resulting OCX later. Unfortunately it does not work for me.
I have a MFC-based ActiveX-control project that was created with Visual Studio 6 and was imported to VS2010. There I have NO class view where I could use the Wizard with to add a method (the class view tab pane is there but it is empty). The existing code also does not provide any callable methods until now so that I simply could copy them.
So: how can I enable/invoke the class view generation in VS2010 to use the Wizard?
And as soon as it works: What type should such a method be to be externally visible? From what I learned the Wizard asks for some type...
To add a method to your ActiveX control you have to follow the folliwng steps:
1. Declare the function in the header file.
e.g.
public:
int Connect(int timeout);
2. Add the definition in the CPP file.
int CSLWebLinkCtrl::Connect(int timeout)
// Your logic here.
return 0;
}
3. Expose your methods in the .idl file
[id(4), helpstring("method Connect")] int Connect(int timeout);
Hope it will help you. :)
Maybe the SDF file is corrupt?
If you right-click the Class View dialog bar, you should see a context menu option for Class Wizard. From there, you should be able to work with your project's classes.

Use ApplicationBar Icons from other assemblies?

I have a Page in a Windows Phone class library. This page has an appbar. I would prefer everything this page needs to be included within the class library so the setup list for consumers of this page / assembly is minimal.
However, When I set ApplicationBar icons, they get added to the class library project under the folder 'icons' just like normal, they show up fine in Blend, but at runtime they are no where to be found!
When I put the icons in the Windows Phone Application project all is well. However this is not my desired scenario as it is additional configuration / setup by the application author to use my pages.
I tried using the resource pathing using /{AssemblyName};component/icons/{IconName} but of course the AppBar needs them to be of type "Content" not "Resource". So I am thinking this is impossible but I wanted to know if anybody out there figured out how to do this.
Sorry guys. I just figured it out. I changed the newly added icons from "Resource" to "Content" but I forgot to set "Copy if newer" so they weren't getting outputted to the Bin\Debug\icons folder of the windows Phone Application. something to keep an eye on I guess.

what is the correct build action for windows phone 7 image files?

I've been working on a Windows Phone 7 app, and after a bit of Googling it seems that for images that I have added to the Visual Studio project, I need to set the build action to "Content" in order to be able to reference the images in my app.
However, the Windows Phone List Application project template includes an image (ArrowImg.png) that has its Build Action set to "Resource", and is still available to be referenced from the application.
I was wondering if anyone could confirm that we should definitely be using the Content build action, or whether there is some way to access images added to a project with the Resource Build Action as shown in the project sample, which we should be using instead?
If you set the action to "Content" the image is included "as-is" in the XAP.
If you set the action to "Resource" the image is embedded into a shared DLL.
In a lot of situations you can use either. There may be a performance, or other, issue with using one rather than another but I'm not aware of and have never noticed any.
For what it's worth, unless I need to specifically make it a resource, I use content.
With the current (Beta) tools, I have seen VS complain that images directly referenced in XAML should be set to "Resource" (if set to "Content") but the app works fine with either. Hopefully this is an issue which will be addressed in the RTM tools.
For more information see the discussion in What are the various "Build action" settings in Visual Studio project properties and what do they do?
Either build action is correct.
Also worth looking at when resolving issues relating to build action is the pathing you use.
I've seen a fair few people running into trouble with this because they assume they've set their build action inappropriately.
You can set the build action either way to suit your requirements of when to incur the load time cost, you just need to adjust the pathing to suit.
Some more info on the topic from this post.
Set source to image in C#
You can liken content to the lazy
loading version of resources.
The difference is you will incur the
performance hit of all resources when
the assemblies are loaded to start
your app.
Content, on the other hand, the
performance hit is deferred to when
you use it.
Which is more suitable will vary on a
case by case basis.
Note also that the pathing to
reference resources and content is
different as can see here.
//Uri uri = new Uri("/Resources/Images/MyImage.jpg", UriKind.Relative); // Content
Uri uri = new Uri("/PhoneApp;component/Resources/Images/MyImage.jpg", UriKind.Relative); // Resource
BitmapImage imgSource = new BitmapImage(uri);
image1.Source = imgSource;

Resources