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.
Related
I have a WinForms app that I am currently implementing a translation engine in. What I have so far is a bunch of text documents that follow the syntax like:
messages.manualupdate="There is a manual update available for ProgName.\n\nDo you want to update to version {0}.{1}.{2}{3}?"
messages.errorcopy="Clicking OK will copy the error so you can paste it elsewhere!"
messages.error="Error"
messages.notsupported.title="Unsupported client"
messages.notsupported.message="This version is no long supported. Please wait for an update."
I have lots of these for different languages, for example:
messages.manualupdate="é disponibile un'aggiornamento manuale del programma ProgName.\n\nVuoi aggiornare alla versione {0}.{1}.{2}{3}?"
messages.errorcopy="Cliccando OK eseguirete una copia degli errori visualizzati"
messages.error="Error"
messages.notsupported.title="Client non supportato"
messages.notsupported.message="Questa versione non è utilizzabile al momento. attendi il prossimo aggiornamento!"
I then parse this into a DynamicObject which I can access like language.messages.notsupported.error. What I would like to know is if I can somehow link all the controls on the form to use variables from the dynamic object on creation. For instance I have a button on my form that I want to have the text "Error" in. Before the form shows, I set the language variable to the users chosen language, and then when the form shows it simply loads the text from language. Is there a way to do this in the designer rather than having to write a method that is called in the Forms constructor as it seems to me like a little bit of a waste to set all the button text to a value and then change them all when the form loads. I'm looking for a sort of binding, but to the controls Text parameter.
Anyone have any ideas?
MSDN has a walkthrough on string localization that might be of use to you link
Honestly, the approach you are trying to avoid looks best to me. I will suggest you to create a property for the control where you are trying to set the Text. In Set attribute, check for the language selected and get the appropriate text for you.
public string Error
{
set { _errorLabel.Text = value; }
}
private void SetText()
{
if(EnglishSelected)
Error = "English";
}
Regarding waste of time, well, I will just suggest not to set anything in designer and directly set the property in Load form. But I would like to add one more point here that any of the approach will not hit your application speed. First its about making your application expandable and maintainable and then about making it fast. Setting logical things in designer is always a bad practice. If your application is not tiny/small then I will suggest you to follow some design patterns like MVP and move all this logical things in Presenter. Not trying to preach but just suggesting.
And yes, in our company one of team is working in localization part of the application. Using resource may be a better way of doing this.
Hope it helps.
I'm creating a WP7 application using C#, and I require to pass data from one page to the other.
I found solutions on SO, but I'm still running into problems.
On 'Page 1', I wish to display a list, that can be populated by the user, using input from 'Page 2'.
I used the following statement in 'Page 2' while navigating back to 'Page 1': NavigationService.Navigate(new Uri("/MainPage.xaml?text="+WhoBox.Text, UriKind.Relative));
WhoBox is a Text Box.
On 'Page 1', I have the following:
protected override void OnNavigateTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.ContainsKey("text"))
ListBlock.Text = ListBlock.Text + NavigationContext.QueryString["text"];
}
Now, this works, but in a limited fashion. If I try adding something from 'Page 2' for a second time, it replaces what is present in ListBlock (which is a Text Block) with the newly added text instead of appending it.
Shouldn't ListBlock.Text = ListBlock.Text + NavigationContext.QueryString["text"]; cause the new text to be appended, rather than to entirely replace the older text?
EDIT: I may have found the solution. For whatever reason, no changes in the XAML or .cs file are reflected when I run the program using F5. Am I doing something wrong? For example, even if I delete a button, it still appears when I Debug (F5) the program. Is there some setting I need to change? Or am I supposed to use some other command? I'm relatively new to Visual Studio, so please excuse me.
The problem is that the moment you again leave your page 1 it is basically disposed of. Meaning any text that was set in the Listbox is also removed. You will , in other words, need to save the state of that page before leaving it.
There several possibilites here:
Use AppSettings (see Windows phone 7 config / appSettings? )
Write the state to a local database
Do a quick'n dirty fix by saving the Text in the App.xaml.cs which all pages can work with: First you need to create the application-wide variable (and initialize if needed) inside the app.xaml.cs file. For example:
public partial class App : Application
{ public string myText;
From now on you can reach any App-variable through the Application.Current object. So if you need to access bigVar from some page in you application (e.g. MainPage) you simply type:
string Text = (Application.Current as App).myText;
Consider using sessions and datatable: Storing and retrieving datatable from session
I'm trying to learn monotouch at the moment, and following the learn monotouch book by Mike BlueStein. Not a bad book, but it's slightly outdated since xcode 4 (i believe) and a newer version on monotouch has come out.
Anyways, in my project I have a controller and a xib file. I also have a custom view (e.g. myview : UIView), that overrides the draw method. I want to show my custom view next to or on top of the view defined in the xib file. How do I do this?
In the controller, If I override the LoadView method, and set the View to an instance of my custom view, then I can see it, but I loose everything defined in the xib file. If I try to add as a sub view, it does not appear at all.
What am I missing? If the question is not clear, please ask me, so I can clarify.
Cheers.
Follow the following steps to use a custom view in a XIB:
First, decorate the view with the RegisterAttribute:
[Register("MyView")]
public class MyView : UIView
{
}
and implement the following constructor:
public MyView(IntPtr handle) : base(handle) {}
This constructor is needed for when the runtime will try to recreate the view after it has been destroyed by a memory warning. After you have created your custom class:
Open the XIB in Xcode (always by double-clicking on it through MonoDevelop) and add a UIView where you want it.
In Xcode, set that UIView's class to MyView (or whichever name you passed to the RegisterAttribute):
Compile-Run.
EDIT:
Do not override LoadView for controllers that are loaded from a XIB. LoadView is meant to create the controller's view when that controller's view is not loaded from a XIB.
Thanks Dimitris, great answer.
For those who get confused like me, here is the simplist procedure to add and use a Xib file as a partial / subview:
Add a new Xib File in MonoDevelop (ie LoginView.xib)
Add a new (partial) Class, this will be the custom class (The Code-behind file let 's say) for the view. Give it any name "LoginView.cs")
Add the Attribute (RegisterAttribte) and the Constrctor as exaplain above by
Dimitris.
Double click the LoginView.xib to open it in XCode IB. Then Change the Custom Class Attribute of the xib file to point to your "code-behind file" (ie LoginView.cs)
Add any outlets or action if you need. MonoDevelop will generate a .designer file and attach it to your code-behind file, this is where you can see all your outlets and actions.
In your controllers where you want to add this view, load your .xib file as follows:
var views = NSBundle.MainBundle.LoadNib("LoginView", this, null);
LoginView loginView = Runtime.GetNSObject(views.ValueAt(0)) as LoginView;
mainLayout.AddSubview(loginView);
// where mainLoyout is the placeHolder in my main ViewController
These three lines are based on flexaddicted answer here
If I have a button in a navigation page, how do I make it target the frame that it is in?
For example, I have a frame called navFrame in MainPage.xaml and I have a navigation page in the Views folder called Home.xaml with a button on it. Basically I'm trying to make clicking the button Home.xaml target the navFrame in MainPage.xaml.
Is that possible? for the button code I tried using MainPage.navFrame.Navigate(...) but it gave me the error "an object is required to reference a non static property..." but I don't know what kind of object it wants or how I would use it.
I'm sorry if this is a stupid question. I'm new to all of this and I spent all last night trying to figure out. Any help would be really appreciated!
From the error message it seems that you don't have a reference to the instance of MainPage in the Home class.
You could get a reference to it through searching up the Visual Tree or calling Application.Current.RootVisual or using some kind of locator framework.
But you don't really need to get a reference to MainPage. From inside the Home : Page class you could instead try
this.NavigationService.Navigate(new Uri("/About", UriKind.Relative))
I am working with the web browser in C# and I was wondering how can I invoke a click on
How does one invoke a click on a button on a site that has a class name?
For example on some sites they have an ID attached to their buttons so the way in clicking the button would be:
webBrowser1.Document.GetElementById("ID NAME HERE").InvokeMember("click");
...but I need it for a button that uses a class instead of an ID
Ok, I think I get it what you are saying. You have this browser object you are using. OK, try GetElementsByTagName and try all the tags you expect to have the class name, could be p,a,div,span, etc but that's on you because you are looking for a class name, you could also try using XPath if that supports it, either way you will be getting back a collection of tags, then you must loop through them, check the existence of that class name then "click" it. Otherwise, I'm lost on this one dude.