in application bar firstly i given some images.when click on that icon need to assign new image.for that in click event wrote the bellow code.
private void searchbtn_Click(object sender, EventArgs e)
{
searchbtn.IconUri = new Uri("/Images/settings_high.png", UriKind.RelativeOrAbsolute);
}
Getting NULL Refernce exception.please help me
In case you did not know (I didn't) you must set the image Build Action property to Content, not resource. Then you can access the file like this:
StreamResourceInfo resourceInfo = Application.GetResourceStream(new System.Uri("ima1.png", UriKind.Relative));
If the stack trace shows a NullReferenceException directly in your method, that suggests that searchbtn is null.
I suggest you put a breakpoint on that line of code and run it in the debugger, then use the Auto window to check the value of searchbtn.
Is it possible that you've declared your own searchbtn variable while there's another similarly-named variable which actually has a reference to a button? The event handler name would suggest otherwise, but it's at least worth checking.
If the stack trace shows a NullReferenceException deeper in the code, that suggests there's a different problem. If you could post the full stack trace, that would help.
Related
I have implemented a custom DataTemplateSelector according to: GitHub Xamarin Forms.
This allows for a datatemplate to be selected based on an item, which is received through a data binding. This works fine to select a proper datatemplate and render it. However, I am now at a point where I want to add custom controls to this datatemplate (custom buttons). This works on any other page, but for some reason not in this datatemplate.
The relevant lines in the datatemplate:
xmlns:controls="clr-namespace:Universal_ONE.Views.Controls"
<controls:IconButton Command="{Binding RobotLocationSave}"
Image="{StaticResource BoltBlack}"/>
The part the datatemplate is selected and created:
var templateToUse = templateSelector.SelectTemplate(item, null);
View view = (View)templateToUse.CreateContent();
view.BindingContext = bindingContext;
In the catch block I get the error (after calling CreateContent()):
Xamarin.Forms.Xaml.XamlParseException: Position 371:26. Type controls:IconButton not found in xmlns clr-namespace:Universal_ONE.Views.Controls
The problem has to be with the CreateContent(), since using the control outside of this datatemplate works.
EDIT 1
I've created a Minimal Working Example: GitHub.
The datatemplate is now hardcoded, so I'm sure that one is selected.
The same parseexception gets thrown.
I am trying to compile Xamarin.Forms myself so I can debug the framework itself. However, compiling is not going smooth; thus might take a bit longer.
EDIT 2
A bit more info on the MWE:
The MainPage.xaml has the default Xamarin.Forms app code. Below I've added a custom control, which is simply a frame with a label. The text of this label is set via a bindableproperty (not really relevant). Below this control is the datatemplatecontrol added, which in turn calls the datatemplateselector, which returns the datatemplate. This datatemplate contains the same control as the mainpage. Thus the rendered app should have 2 controls. However, in de datatemplatecontrol you can breakpoint on the catch statement (look for my comment). This is where the parseexception will show, which is caused after calling CreateContent() on the datatemplate.
EDIT 3
I've not been clear enough I think. But you have to put a breakpoint on line 41 of datatemplatecontroler.cs. Since the content of the datatemplate is set to null if the createcontent() fails, thus fails gracefully. When hitting the breakpoint you can read the parseexception.
EDIT 4
I've made an issue and a pull request on the Xamarin repository: GitHub. The problem resides with the XamlParser, which has a small bug where it selects the wrong assembly. Inside the issue I've mentioned a workaround which can be used for now.
I try to download your example and run it. When I run to Content = CreateTemplateForItem(ItemTemplate); the program does not report an error, so the program skips the catch method. Here is a screenshot of the runtime:
Then I tried to actively throw an exception in the try statement (throw new Exception();) to make the program enter the catch method, Here is a screenshot of the runtime:
I'm using Visual Studio 2013 and MvvmCross to create an Android application using Portable Class Libraries. When I try and load a second ViewModel based on an action in the first View, an Unhandled Exception is being thrown.
In my first ViewModel, CatalogViewModel, I've got an ICommand called ShowCamera that is supposed to display the second ViewModel, CameraViewModel, when a button in the CatalogView is clicked.
In CatalogView.axml I've bound the button to the ShowCamera method in CatalogViewModel.
Right now the CameraViewModel is just an empty class but it inherits from MvxViewModel just like CatalogViewModel
And there's also a corresponding CameraView.cs and CameraView.axml.
Has anyone else ever encountered this or have any idea why the exception is being thrown. I've tried using a try/catch in the Get to see if I can get any more specific info about the unhandled exception without any luck (the catch block is never hit, the exception is always thrown on the ShowViewModel line).
Thanks in advance.
As Stuart pointed out it his comment, it was the lack of an [Activity] attribute on CameraView that was throwing the exception. The class definition for CameraView should be.
[Activity(Label="View for CameraViewModel")]
public class CameraView : MvxActivity
{
...
}
instead of
public class CameraView : MvxActivity
{
...
}
I initially thought about deleting the whole question out of embarrassment but decided to answer it and leave it here for anyone else who might encounter this situation in their own code.
I have a WPF application that is utilizing the reporting tools included with Visual Studio 2010. I've had some other problems that I've solved by creating a graph of objects that are all marked as serializable, etc., as mentioned on various other web pages.
The ReportViewer control is contained in a WindowsFormsHost. I'm handling the SubreportProcessing event of the ReportViewer.LocalReport object to provide the data for the sub report.
The object graph that I'm reporting on is generated in my viewmodel, and that viewmodel holds a reference to it. The SubreportProcessing handler is in my code behind of my window (may not be the best place - but I simply want to get the ReportViewer working at this point).
Here's the problem: In my event handler, I'm attempting to get a reference to my viewmodel using the following code:
var vm = DataContext as FailedAssemblyReportViewModel;
When the handler is called, this line throws an InvalidOperationException with the message The calling thread cannot access this object because a different thread owns it.
I didn't realize the handler might be called on a different thread. How can I resolve this?
I attempted some searches, but all I've come up with is in regards to updating the UI from another thread using the Dispatcher, but that won't work in this case...
I solved this problem using something I believe is a hack, by adding the following function:
public object GetDataContext() {
return DataContext;
}
And then replacing the line of code from my question with:
object dc = Dispatcher.Invoke(new Func<object>(GetDataContext), null);
var vm = dc as FailedAssemblyReportViewModel;
However, this seems like a hack, and I might be circumventing some sort of safety check the CLR is doing. Please let me know if this is an incorrect way to accomplish this.
That's a nasty problem you have there.
Why don't you use in the view a content presenter which you bind to a windows form host?
And in the view model you would have a property of type of type WindowsFormsHost. Also,in the view model's constructor you could set the windows form's host Child property with the report viewer.
After that is smooth sailing, you could use your report viewer anywhere in your code. Something like this:
View:
<ContentPresenter Content="{Binding Path=FormHost}"/>
ViewModel:
private ReportViewer report = new ReportViewer();
private WindowsFormsHost host = new WindowsFormsHost();
public WindowsFormsHost FormHost
{
get {return this.host;}
set
{
if(this.host!=value)
{
this.host = value;
OnPropertyChanged("FormHost");
}
}
}
public ViewModel() //constructor
{
this.host.Child = this.report;
}
After that happy coding. Hope it helps.
How can i move
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
to the viewmodel and still be able to control "back'ing"? In the codebehind, i can use e.Cancel = true;, but how to use it in the viewmodel?
You can't bind something that isn't bindable per say. All you can do is either create a fake binding using a Behavior<T>, but not much point in that.
Instead you could just simply forward the event in the ViewModel, doing something like:
e.OnCancel = ViewModel.OnBackKeyPress();
And then have OnBackKeyPress() return a bool.
The very first idea that i got - is to leave it in codebehind, and send message to viewmodel, so it should shange its state. But i'd still prefer to bind event to VM.
I am using this code to map a database to my Windows Phone 7 application.
In particular when I add a new Card item (in a Phone Page different from the home page) and when I get back to the home page the balance value of my Credit Card list does not update (Which is bound to an observable collection of type Card). I think I have added all the necessary NotifyPropertyChanged.
Am I missing something:
Code: Here
This is kind of a shot in the dark, but I don't see PropertyChanged being set anywhere... if PropertyChanged does not get set, then it will equal null and none of the NotifyPropertyChanged calls will actually call PropertyChanged().
I had a problem once that wouldnt update my URI, just try to actualize when you can, not sure how to go about your particular problem but I had a problem that when I changed teams in my app, the main page wouldnt update the statistics when you returned to it, so I just added a method that retrieves the value once more, I use nested classes with setters and getters so its all handled for me
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
method();
base.OnNavigatedTo(e);
}