Create a RibbonControl in code-behind - c#

I'm working on a PowerPoint Add-In project.
I'm trying to create a RibbonToggleButton in code-behind but I can't do it the way I thought,
RibbonToggleButton toggleButton = new RibbonToggleButton();
Is it possible to create RibbonControls in code-behind and not the RibbonDesigner or RibbonXML?

According to the docs:
You can create a RibbonToggleButton at
run time by using the
CreateRibbonToggleButton method of the
RibbonFactory object.
It looks like you get at the RibbonFactory via the RibbonBase.Factory property.
You may also want to look through this walk-through.

Related

How can I use settings stored with Xam.plugins.settings in my XAML?

Thanks to James Montemagno for this plugin.[https://jamesmontemagno.github.io/SettingsPlugin ]
I have used it in my c# code easily: CrossSettings.Current.GetValueOrDefault("abc", "")
But I also want to use those settings in my XAML.
Before this plugin, I was using my own basic (inefficient) array setup and used: ...Text="{x:Static local:Settings.abc}"... Now I have completely converted over to this plugin. And it works well in c#, but am struggling to get it working in XAML.
I Tried {x:Static local:Helpers.Settings.GeneralSettings.abc}, and {x:Static helps:Settings.GeneralSettings.abc} (creating xmlns:help).
How can I use those settings in my XAML code?
The best way to do this is to set up a Property Accessor (get/set) - and bind the control to that method.
As a quick (one way) example..
Page Code behind
public int MyNumber
{
//This may be different, depending on what your Settings class has been named and where its reference has been stored - but it is the same plugin.
return App.Settings.GetValueOrDefault("myNumber",0);
}
XAML
<Label Text="{Binding MyNumber}"/>
You'll also want to make sure you've set the BindingContext. I find it easiest to do this after InitializeComponent(); in the page constructor, simply by using BindingContext = this;.
A more complex implementation (With two way data binding) can be found in the Settings Plugin Documentation.

How does FindViewById() work?

I'm new in mobile app development. I'm using Xamarin to develop Android applications. In the hello world app in the OnCreate method I see the following code:
Button button = FindViewById<Button>(Resource.Id.MyButton);
So I'm trying to create my own button the same way. I create the button in the designer and inside OnCreate method put the line:
Button myOwnBtn = FindViewById<Button>(Resource.Id.MyOwnBtn);
That gives me an error that there is no MyOwnBtn. Then I'm looking the code of Id class and see there a line like:
public const int MyButton=2123344112;
If I put there the line:
public const int MyOwnBtn=2123344113;
Everything works fine. But as I understand it should be generated automatically or it will be a little bit difficult to put there a unique number for each control.
Can anybody tell me what I am doing wrong? And how does FindViewById() work?
You have to give the id MyOwnBtn to the Button that you created in the designer.
findViewById is a method of the View class and it looks for a child view having the id that you provided in the argument.
From official documentation:
Look for a child view with the given id. If this view has the given id, return this view.
MyButton id is not a const value, It will change every launch.
The Activity or ViewGroup's findViewById() method returns a view that already has an id. The findViewById() method should be used in conjunction with XML layouts to provide a reference to the View that was defined in the XML file.
Edit: Not entirely sure if my answer is relevant to Xamarin. I apologize if I have mislead people, I am referring to Java Android application development.
When you declare a button in your .xml file, you should set an id for it (Usually it is done using string.xml file). After that, R.java will be updated automatically and set a number to your declared id and you can access your button by that id like what you have done.
It will try to find it from the XML file that you inflate. So make sure you inflate the correct xml file. This code inflates the xml:
SetContentView (Resource.Layout.MainLayout);
Even if you got the correct id created in a xml file, if you don't inflate it first, the system won't be able to find that view since it is not inflated.

Load a custom server control from another custom server control

I have two custom server controls in a ASP.NET page,
one builds a chart, the other one plays the builded chart.
so my problem is to load the player when the chart is built.
so i send the file to the builder and now ii want just to load it
gChartPlayer Player = new gChartPlayer();
Player.XML_FILE = ChartFile;
something like Player.Load(),
can someone help me??
Question isn't really clear but if you want to decide when to load a usercontrol you can use LoadControl http://msdn.microsoft.com/en-us/library/c0az2h86(v=vs.100).aspx
You can use Add Method
Code : this.Controls.Add
Link : http://msdn.microsoft.com/en-us/library/aa277578(v=vs.60).aspx

Return a UserControl from a method that is not in the Code-Behind file.

I want to create a helper method that could be called from different code-behind files. This method would return a UserControl (myUC). However I do not see any way of accessing myUC except by Registering the UserControl in the ASPX file or in the web.config file, but neither of these methods will give me access in Non-Code-Behind CS files. I thought that maybe I could wrap the UserControl in a Namespace and then use that namespace in the CS file but that did not work either.
I am thinking of giving up on using the UserControl for a composite control ( http://msdn.microsoft.com/en-us/library/aa719734(v=vs.71).aspx ) as this can be wrapped in a Namespace and used in any CS file.
But before I do this I was wondering if anyone can shed more light on this?
Thank you.
Joseph
UserControl is not meant to be create from code behind. It is not its purpose. You have to use CustomControl for that. Read here:
ASP.Net Custom controls vs. user controls: Are these two the same

Failing to add controls to a page dynamically

I'm adding a User Control for each record pulled up in a data reader, here's the basic loop:
while (dr.Read())
{
ImageSelect imgSel = new ImageSelect(dr["Name"].ToString());
myPanel.Controls.Add(imgSel);
}
The problem is that there are no controls added to the page, I check the html output and there is my panel, with nothing in it.
I even stepped through the code in the debugger, and verified that myPanel.Controls gets a control added on each loop, with the count being 6, no errors, but then they dont show up on the page.
I've run the above code in the Page_Init and Page_Load events, both with the same result.
EDIT:
Ok so I've switched to using LoadControl("....ascx") to get my control instance, which is now working. But originally I was also passing in data via the controls constructor.. Is this still possible or do I just need to set them via get/sets?
EDIT 2:
Thanks to Freddy for pointing out that the LoadControl has an overload where you CAN pass in constructor params, see accepted answer.
EDIT 3:
After trying this method both with and without the constructor. I have found its better to just use setters for any properties I want the control to have versus trying to use the passed in object array for my constructor.
Update: As Steve pointed out, the overload of LoadControl that uses the type won't take into account the controls in the ascx. This is also mentioned in this answer: Dynamically Loading a UserControl with LoadControl Method (Type, object[]).
As I mentioned before, the get/set are more in line with the asp.net model, so I recommend using that with the LoadControl variation that receives the user control path. That said, the Steve's version is an interesting alternative: http://www.grumpydev.com/2009/01/05/passing-parameters-using-loadcontrol/.
My take is the LoadControl with type is meant to be used with web custom controls instead.
If it is an user control you should use LoadControl(usercontrolpath) to get the instance of the user control.
You can use a constructor by doing:
var name = dr["Name"].ToString();
var imgSel = LoadControl(typeof(ImageSelect), new object[]{ name });
myPanel.Controls.Add(imgSel);
Notice that depending on the project model you are using, you need to add a Reference to the aspx to use it with the typeof variation:
<%# Reference Control="~/somepath/myusercontrol.ascx" %>
Ps. I usually use the set/get for controls as I find them more in line with the asp.net model
To add UserControls you must call the LoadControl method passing in the path to the .ascx file. You can not create them by just instantiating the object the .ascx file inherits from.
A UserControl consists of both the markup and the class in the code behind. The markup contains a link to the class behind, but the class behind does not know where the markup lives and therefore can not be created on it's own.

Categories