Reference is not a valid visual DependencyObject - c#

I'm trying to use the coding4fun toolkit for windows phone 7.
InputPrompt input = new InputPrompt();
whoAreYou.Completed += input_Completed;
input.Title = "Who are you?";
input.Message = "Enter your name";
input.Show();
void input_Completed(object sender, PopUpEventArgs<object, PopUpResult> e)
{
//add some code here
InputPrompt input = sender as InputPrompt;
MessageBox.Show(input.Value);
}
This throws the exception below:
Reference is not a valid visual DependencyObject
I want to ask the user for his/her name.
I'm following this tutorial and have added the necessary assembly references.
How can I get the coding4fun toolkit to work?
Edit:
If I do this in xaml like
<c4f:InputPrompt x:Name="input" Completed="input_Completed_1" />
Then this works, but I need it to work with code.

I've reproduced your bug when I've added the code presented to the Main Page constructor. It seems like an InputPrompt isn't added to the visual tree (just a theory) and that's why can't be shown.
When I've changed the code to show InputPrompt upon Loaded event, everything worked fine. Basically, you have to wait until the page is loaded or put the control in your XAML file.

Related

How to interrogate GUI controls in a WPF application?

Still setting my first steps in WPF, this time I'm dealing with this "simple" situation:
dt_Proposal.Rows[0].ItemArray[i] = highest_val + min_val;
This piece of code has been run for every value of i (also for value 2), so I expect to see something on the place of the red rounded rectangle:
However I don't see any value there.
I decided to pauze my application (Visual Studio "Debug" menu, "Break All" menu item), and then I thought of having a look at the controls of my form, but how?
Visual Studio's "Locals" window contains this, which is mentioned being a <Application_Name>.MainWindow, but I can't look deeper.
When adding this to the "Watchlist", this seems not even to be accessible.
So this becomes a very general question: I am working on the GUI of a WPF application, and I would like to see the properties/fields of the controls/GUI components of my main form, and I would like to do this while my application is waiting on any kind of user input (typical "wait state" for a GUI application).
How can I do that?
In WPF you can see Visual Tree (even without pausing the app at all!), you enter this like shown in below picture:
By the way, value hello world was set programatically:
private void Button_Click(object sender, RoutedEventArgs e)
{
txtInfo.Text = "hello world";
}
Once in Visual Tree, you can right click control and show its properties:
And there you can see values of properties set:

uwp c# 2019: How to refer to RootGrid

Sorry. I think I've found it.
I thought the suggestion reference to "RootGrid" was to a VS Named Space, like System.Windows.RootGrid I'm relatively sure they intended me to refer to a Grid in my XAML.
Realizing this probability was a "DUH" moment. So, just replacing RootGrid with my XAML name "GridBoard" will probably solve the issue. I haven't got the close of the Control to work yet, but he naming issue is at least being found.
To answer the question. Help, is a User control file in ScqWander Program. localUCHelp was the name used to create an instance on the Page.
Quick overview: How do I refer to RootGrid?
I get RootGrid "Does not exist in current context"
I am using VS 2019 and have a C# project, using XAML. I wrote a UserControl and am trying code a Button to Close the control (when clicked work from within the control). I got a suggestion which included the sample below. Google has turned up nothing.
The code is within the namespace
public sealed partial class MainPage : Page
void Page_Loaded(object sender, RoutedEventArgs e)
{
ScqWander.Help localUCHelp = new ScqWander.Help();
localUCHelp.HelpUserControlCloseEvent += new EventHandler(BtnPXClose);
RootGrid.Chilren.Add(localUCHelp);
}
I was simply thinking that "RootGrid" reference to which I could refer (as in Using System.Whateverxxxx) when I should have been referring to the Grid I defined in my XAML. I just did not recognize what was being said in the example.

Xamarin - Constantly getting NSInvalidArgumentException on Button click

I'm currently doing some cross-platform mobile development through Visual Studio using Xamarin (so in C#) and am about to start the iOS portion. I've never done iOS development before and thought I could get myself acquainted with their "Hello, iOS" Tutorials. Unfortunately, things have not been going smoothly. I constantly get NSInvalidArgumentExceptions from my TouchUpInside actions:
Foundation.MonoTouchException: Objective-C exception thrown.
Name: NSInvalidArgumentException Reason:
-[ViewController TranslateButton_TouchUpInside:]:
unrecognized selector sent to instance 0x7b6200d0
I can occasionally remedy it for a moment by literally remaking the Buttons, but it breaks pretty much right afterwards. The actual error itself occurs in my Main.cs file:
using UIKit;
namespace CheckinIOS
{
public class Application
{
static void Main(string[] args)
{
UIApplication.Main(args, null, "AppDelegate"); //this line is where it breaks
}
}
}
In case it is any helpful, I am trying to deploy to iPhone 5S simulator running iOS 9.3 (but it breaks on iPhone 6 simulator as well). I could also post more of my code if necessary, but I copypasted all the C# from Xamarin's tutorial, and did the same thing as them for Main.storyboard.
I have spent a while looking for people with the same problem as me, but their solutions either did not work, or they got the error for slightly different reasons. Any assistance is appreciated.
EDIT: Here is my implementation of TranslateButton_TouchUpInside:
TranslateButton.TouchUpInside += (object sender, EventArgs e) =>
{
// Convert the phone number with text to a number
// using PhoneTranslator.cs
translatedNumber = PhoneTranslator.ToNumber(PhoneNumberText.Text);
// Dismiss the keyboard if text field was tapped
PhoneNumberText.ResignFirstResponder();
if (translatedNumber == "")
{
CallButton.SetTitle("Call", UIControlState.Normal);
CallButton.Enabled = false;
}
else
{
CallButton.SetTitle("Call " + translatedNumber, UIControlState.Normal);
CallButton.Enabled = true;
}
};
The iOS Runtime is looking for a method called (in Obj-C land) TranslateButton_TouchUpInside: in your ViewController class. However there is no method exported to Obj-C with that name. A first guess is that you added an event to the button in the storyboard that perhaps had that name, but you either deleted that method or never implemented it.
Try opening your storyboard in iOS Designer and removing any event from the Properties->Events tab when your button is selected on the canvas. Also I assume your button has the name TranslateButton in the Properties->Widget pane when the button is selected on the canvas.
There are a couple ways to attach events to controls in Xamarin iOS. One, and the preferred way, is to create an event in iOS Designer for the control. If you do this, a partial method stub will be in the .designer.cs file with an Export attribute that exports the method name to the Obj-C runtime. You will then need to implement this method, using the same signature (without the Export Attribute), in your main .cs file for the ViewController. This is called, in Obj-C land, an action.
The other way is to do as is shown in your code snippet. In this case you ONLY need to give the control a name in the Properties->Widget pane that you can then use in code to subscribe to the TouchUpInside event. This is called, in Obj-C land, an outlet.
My guess is that you did both but without ever implementing the TranslateButton_TouchUpInside: method in your ViewController. Note that this is the Obj-C name used in the Export attribute of the method stub created in the .designer.cs file when you add an event to a control.
But it is hard to say without seeing the storyboard and both the main ViewController.cs file and the ViewController.designer.cs file

How do I add a textblock dynamically to a page?

I'm just learning how to make universal apps for windows 10. Most of the tutorials show you how to work with XAML and how to assign functions to elements when you click on them but I couldn't find a way to make a new control appear when I click a button.
I'm making a note taking application. I've designed most of the stuff that I need. Now I want whenever I click a button to create a new textblock where the user can write their note.
//Create a new note when clicking the add button
private void newNoteBtn_Click(object sender, RoutedEventArgs e)
{
TextBox newNote = new TextBox();
newNote.Text = "Enter your text";
}
This is the code that runs when the button is clicked. When I run the application nothing happens. I think I have to put the new textbox in some kind of Grid or something.
Most of the tutorials are really old or mention windows forms and use some sort of this.Controls.Add(newNote); but Visual studio doesn't give me the Controls.Add option. I've also created a <Grid x:Name="notes"></Grid> which I thought I could use as a placeholder for the notes that are being created but I can't access the Grid element through the code.
Container Controls like Grid have Children property so you should use Childern like this:
TextBox newNote = new TextBox();
newNote.Text = "Enter your text";
notes.Childern.Add(newNote);
When defining
<Grid x:Name="notes"></Grid>
in XAML on the page, you be able to use notes as the identifier to access this Grid from the page's code behind:
notes.Children.Add(newNote);

WP8 - Automatically open keyboard on &123 option

while developing a new Windows Phone 8 software, I got stuck with this issue.
I'd need to open the keyboard straight to this view:
But all I can do is this (just giving focus to a text-box, that's easy enough!):
Set the TextBox InputScope properties to Digits in your xaml code
<TextBox InputScope="Digits " Name="txtNumbers" />
and your code behind page use below line of code
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
txtNumbers.Focus();
}
Check this link for allowed scopes:
http://msdn.microsoft.com/en-us/library/system.windows.input.inputscopenamevalue%28VS.96%29.aspx
I believe the view you are after is called "Numeric Mode"; This following link has some images as well.
http://www.informit.com/articles/article.aspx?p=1706099&seqNum=2

Categories