I want to import an camera/Gallary image into a page, not into the App() page, the function ShouldTakePicture() doesn't work, can anyone provide a sample code where I can import image to another page and display in a listview?
This import image was done form code in the link http://xforms-kickstarter.com/#camera
However when doing in another page, the MainActivity throws bellow error
Severity Code Description Project File Line Suppression State
Error CS1503 Argument 1: cannot convert from 'Gallary2.Injury' to 'Xamarin.Forms.Application' Gallary2.Droid C:\Users\haris\Documents\Visual Studio 2015\Projects\Gallary2\Gallary2\Gallary2.Droid\MainActivity.cs 32 Active
can anyone help me with image import demo project in Xamarin Forms? (Only c# please, we are not making changes in XAML )
It sounds like you created a new page and in that new page you created a ShouldTakePicture property of type Action. The reason that the code is now failing, is because of this line in the iOS and Android projects:
(Xamarin.Forms.Application.Current as App).ShouldTakePicture += () => {
That line is expecting the ShouldTakePicture property to be in your App class, but it has been moved to your new page.
To fix this, you could either move that property back into your App class and then reference it from your new page by calling Command = new Command(o => App.ShouldTakePicture()),.
A better way to fix this would be to use Xamarin's DependencyService, info about that is here. If you need help setting up the DependencyService, let me know.
Related
I am creating a webview2 wpf application, everything works well till now, but when I tried to load HTML file which is having youtube link, It's worked well along with autoplay .
But when I change the HTML file to some another site , the audio of youtube video still playing in background and webview2 load the new content.
I want something to disable the cache to be stored will runtime instead of clearing the cache.
If someone is having any idea related to above issue please help
This is the common issue faced by the webview2 when external site media player is played.
To overcome this issue you have to create a new instance of webview2 before you load second content to webView, this will create new Webview Page but still the UI will not stop the audio, so use Webview.dispose() as well.
WebView.Dispose()
Note : this will give you issue related to Null object reference for the 1st time .
So you have the check the the object before doing dispose.
Code should be like this :
MainPage :
WebViewPage webpage;
private void updateNewContent ()
{
...
WebView2 webView2 = Mywebvew2; // MYwebview2 is UI object from webviewPage,
//you have to pass this object from webview Page
if (webView2 != null)
{
webView2.Dispose();
}
webpage = null;
webpage = new WebViewPage();
GridPrincipal.Children.Add(webpage);
...
}
I am very new to Xamarin.
I have created a login page for my app but I need a new page to use as the main menu. The new page should be editable on Xcode to design the UI. It also needs a ViewController.cs to run some code inside it.
Are there any possible ways that I can create a page which needs my requirements that I have just mentioned?
I solved the issue by creating a new ViewControl in Xcode then I specified it's class at identity inspector and I also gave it a storyboard ID from the same menu.
Then I wrote the following code to display the new ViewControl.
MainViewController mainViewController = this.Storyboard.InstantiateViewController("MainViewController") as MainViewController;
mainViewController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen;
PresentViewController(mainViewController, true, null);
MainViewController is the class that I mentioned in identity inspector and second MainViewController which is inside the quotation mark is my storyboard ID.
Am building an application where by i wand to link an image button to a string. Bellow is my code
ImageButton imageEnglish = FindViewById(Resource.Id.disclaimer);
When is debug my code, its tells me
"resource ID does not contain a definition the disclaimer"
yet disclaimer is defined in my strings.
Kindly take me through this
Did you try this :
You are missing cast in your code.
ImageButton imageEnglish = FindViewById<ImageButton> (Resource.Id.disclaimer);
Check your ImageButton Id in designer too.
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.
I'm trying to use an icon that I've added as a resource as the image on a button. I know it's possible because I can do it in other projects through the designer. However, I'm trying to do this with code. I added the icon as a resource to my project by following the steps in the accepted answer to this question. The resource is named CancelButtonIcon.
Now, I'm trying to add that icon as the image on a standard button with this code:
this.CancelButton.Image = (System.Drawing.Image)Properties.Resources.CancelButtonIcon;
However, I get an error message:
Cannot convert type 'System.Drawing.Icon' to 'System.Drawing.Image'
In the code that Visual Studio automatically generates when I use the designer, it looks like this:
((System.Drawing.Image)(resources.GetObject("SaveButton.Image")));
which results from manually adding a resource through the Properties window. How can I convert this icon resource to an image so it can be used on the button? Adding it through the designer is not an option (this button is created programmatically and thus isn't present in the designer).
You can use the Icon.ToBitmap method for this purpose. Note that a Bitmap is an Image.
CancelButton.Image = Properties.Resources.CancelButtonIcon.ToBitmap();
Not sure why, but any time I tried using the accepted answer's approach, the .ToBitmap() call was giving me array index out of bounds exceptions. I solved this by doing it this way instead:
System.Drawing.Icon.FromHandle(Properties.Resources.CancelButtonIcon.Handle).ToBitmap();