MessagePrompt crashing at first launch of application - c#

I have a problem that I am using coding4fun dll in my WP7 application for showing the popup messages.
I am using:
Micrsoft.Phone.Controls.Toolkit
Coding4fun.Phone.Controls
At first launch of deployment on device its crashing saying that value cannot be null(parameter name element) while on emulator its running fine. I have tried the latest version of this dll but the result was same.
While adding Micrsoft.Phone.Controls.Toolkit of latest version 1.4.8 is giving warning that adding a silverlight library may result in unexpected consequences.
while I tried other version of this dll still no success.
I am getting exception in stacktrace
Clarity.Phone.Extensions.DialogService.InitializePopUp
Clarity.Phone.Extensions.DilaogService.Show
Basically i am using that popup inside constuctor of mainpage.xaml(first page) after InitializeComponent() and it is throwing null reference type at first launch while deploying but app is getting installed. again if i run application on device then it is appearing correctly.
My code is:
notificationPrompt = new MessagePrompt();
notificationPrompt.Title = "Notification"
notificationPrompt.Body = "";
notificationPrompt.ActionPopUpButtons.Clear();
Button btnDisclaimer = new Button() { Content = "Yes" };
btnDisclaimerContinue.Click += new RoutedEventHandler(btnNotificationPromptYes_Click);
Button btnDisclaimerCancel = new Button() { Content = "No" };
btnDisclaimerCancel.Click += new RoutedEventHandler(btnNotificationPromptNo_Click);
notificationPrompt.ActionPopUpButtons.Add(btnDisclaimerContinue);
notificationPrompt.ActionPopUpButtons.Add(btnDisclaimerCancel);
notificationPrompt.Show();

I think it's the better to move all this code outside the constructor, and put it inside the Loaded event (occurs when a FrameworkElement has been constructed and added to the object tree: http://msdn.microsoft.com/en-us/library/ms596558(vs.95)) of the PhoneApplicationPage class, or just override the OnNavigatedTo method:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
// What you want here...
...
}
Often when you have exceptions in the constructor of a PhoneApplicationPage, they will not show, making the debug more difficult and annoying...

Related

Using NLog with WPF richtextbox

I have an application which has several tabs. I am trying to add logging with NLog where the output is directed to a richtextbox.
My main form is an instance of a class MyNamespace.MainWindow and its name is MainWindow1. It has a tab and in that tab I have a RichTextBox called rtbLogBox
I have imported WpfRichTextBox extension from NuGetand place this piece of code in the MainWindow class's loaded event
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if(logger == null) logger = LogManager.GetCurrentClassLogger();
WpfRichTextBoxTarget rtbTarget = new WpfRichTextBoxTarget
{
Name = "rtbLog",
ControlName = "rtbLogBox",
FormName = "MainWindow"
};
LogManager.Configuration.AddTarget(rtbTarget);
LogManager.Configuration.AddRule(LogLevel.Info, LogLevel.Fatal, rtbTarget.Name);
logger.Info("This");
}
The problem is that this does not produce any outputs in the RichTextBox control.
I have an output to a file in addition to this and that target gets the log when I run the app.
Since the logger is created before you are changing the LogManager's configuration, you need to notify the logger of the change.
Calling LogManager.ReconfigExistingLoggers() should do the trick.
Documentation is here.
You can also use my wpf control. (https://github.com/dojo90/NLogViewer). There is also a nuget package available.

Reference to the right "System" in c# project

I am new to c# and visual studio and have run into some trouble.
I have created a project with references to "Windows" and ".Net" in visual studio because I want to test a little with smart cards.
The code:
using System;
using Windows.Devices.Enumeration;
using Windows.Devices.SmartCards;
class HandleSmartCard
{
public async void checkNumberOfSmartCards()
{
string selector = SmartCardReader.GetDeviceSelector();
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector);
// return "2";
}
}
So far it looks fine. However I also want the project to be able to use
System.Windows.Forms; which I have used in a previos test.
I add reference to System.Windows.Forms; and try to create a form. However in that form when I try this:
Form prompt = new Form();
System.Windows.Forms.Button confirmation = new System.Windows.Forms.Button() { Dock = DockStyle.Bottom };
confirmation.Click += (sender, e) => { prompt.Close(); };
I get a red line under "Close" with the message:
Reference to type component claims it is defined in system but could
not be found.
System is referenced at top of file, but I am guessing it is the wrong type of system right?
Can I somehow use "both Systems" in one project so to speak?
I hope someone understands what I mean and can help me understand this.
You're most likely working on a UWP app. The API for UWP apps is a very small subset of the full .NET framework. You can find more information here
https://msdn.microsoft.com/en-us/library/windows/apps/mt185501.aspx
You're attempting to reference System.Windows.Forms which is not allowed in UWP applications.
Looks like you're trying to create a popup to ask the user something. For this, use the MessageDialog class.
https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.popups.messagedialog.aspx

UIWebView throwing exception on LoadRequest

I have the following code trying to load a web page on my app:
Console.WriteLine("login view class");
NSUrl url = new NSUrl("http://deekor.com");
NSUrlRequest request = new NSUrlRequest(url);
loginWebView.LoadRequest(request);
The last line:
loginWebView.LoadRequest(request);
is throwing an System.Reflection.TargetInvocationException exception.
loginWebView is a UIWebView
Whats going wrong?
Edit
I think I was forgetting this line: loginWebView = new UIWebView(); After adding it the exception is gone... however the page is not loading..
Looking at the locals in debug I see loginWebView is set to null before I call loginWebView = new UIWebView();. Do I want to be creating a new one like this, or is something wrong with my outlet?
LoginViewController.Designer.cs
StoryBoard
#Deekor,
I loaded up a new project, and I tried to make exactly what you were making. I ran into the same issue if I followed these steps:
Create a new iPhone, single view, storyboard project.
Open up the storyboard file and drag a UIWebView onto the UIViewController that is created automatically in the new project.
Go back to Xamarin Studio and in the LoginViewController.cs file that was created for me, try to put in the loginWebView.Request code.
This doesnt work, as you noticed because the web view is null. It should not be. I don't know why, but there was also missing methods in that LoginViewController.cs file that is auto created. I could not override ViewDidLoad() which is where you want to be. Even though the class is inheriting from UIViewController, the override was just not available. So here is how I fixed the problem:
Delete the auto generated UIViewController in the Storyboard.
Delete the auto generated class that came with it (LoginViewController.cs)
Now in the storyboard, drag in a new UIViewController.
In the Custom Class box in interface builder, enter your class name: LoginViewController.
Go back to Xamarin Studio, and let it sync up and create the files for you again.
No go back to Interface Builder/Xcode and drag a UIWebView onto that new LoginViewController you created in step 3.
Drag your outlet for the loginWebView just as you did before.
Go back to Xamarin Studio and let it sync up.
Open your LoginViewController.cs file and now you can override ViewDidLoad().
Now you can simply have this:
public partial class LoginViewController : UIViewController
{
public LoginViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
loginWebView.LoadRequest(new NSUrlRequest(new NSUrl("http://deekor.com")));
}
}
I don't know why sometimes the auto-generated files from a new project act strange, but basically all I did was delete what it created, and manually add the same thing again and then it worked. The web view being null when you launched is what made me think something was wrong. Please comment if something is confusing, I will do my best to explain better if I can. I did get this working following the steps above. In the image attached you can see it running in the simulator, showing your page.
Just to see what is happening try the following:
Console.WriteLine("login view class");
var webView = new UIWebView(this.View.Frame);
this.View.Add(webView);
NSUrl url = new NSUrl("http://deekor.com");
NSUrlRequest request = new NSUrlRequest(url);
webView.LoadRequest(request);
If that works it means it is an issue with how the WebView has been added to your Storyboard and then connected as an outlet...

Silverlight C# - ComponentOne Spellchecker not loading dictionary

This may be a long shot, but I'm using ComponentOne's Spellchecker control for Silverlight. I made a test project, added a plain textbox and a button to it, added the references to the C1.Silverlight and C1.Silverlight.SpellChecker bits, and added the dictionary file to my project.
In the code, I called up the spellchecker on button1's click event and it worked SPLENDIDLY. The spellchecker dialog shows up, and works exactly as it should.
Since that test was successful, I then tried to implement this into my existing project. I've had no success for absolutely NO reason that I can determine, since I used the EXACT SAME code.
Here's the code I use to call the component:
using C1.Silverlight;
using C1.Silverlight.SpellChecker;
using C1.Silverlight.Resources;
public partial class MainPage : UserControl
{
C1SpellChecker spellChecker = new C1SpellChecker();
public MainPage()
{
InitializeComponent();
spellChecker.MainDictionary.LoadAsync("C1Spell_en-US.dct");
}
private void btnSpelling_Click(object sender, RoutedEventArgs e)
{
var dlg = new C1SpellDialog();
spellChecker.CheckControlAsync(txtArticle, false, dlg);
}
The references to C1.Silverlight and C1.Silverlight.Spellchecker are added to this project as well, and the dictionary as been added in the same fashion as well. The issue seems to be that for whatever reason the dictionary is not loading, because the spellChecker.Enabled method returns whether or not the main dictionary has been loaded. If I call MessageBox.Show("SpellChecker Enabled = " + spellChecker.Enabled.ToString()); it shows false, even though the call to load the dictionary is there (as you can see).
What would cause the dictionary to not load? Have I added it to my project incorrectly somehow?
EDIT: I suspect that I have added the dictionary to the project incorrectly, because the ComponentOne reference states:
If C1SpellChecker cannot find the
spelling dictionary, it will not throw
any exceptions. The Enabled property
will be set to false and the component
will not be able to spell-check any
text.
I just don't know what's wrong though because it was added in the same way that it was in the test project (Right clicked on the project.web->Add->Existing Item)
As always, thank you!
-Sootah
You could add the dictionary to the Silverlight app as an embedded resource and then load it using this code:
public MainPage()
{
InitializeComponent();
// load C1SpellChecker dictionary from embedded resource
var asm = this.GetType().Assembly;
foreach (var res in asm.GetManifestResourceNames())
{
if (res.EndsWith(".dct"))
{
using (var s = asm.GetManifestResourceStream(res))
{
sc.MainDictionary.Load(s);
break;
}
}
}
}
I think this post is duplicated in our forum as well, but will answer first here. Please try this:
1) Try to access the .dct file using your browser. If you cannot see it, it's probably because your web server is not serving that type of files. You need ton configure the web server to allow it.
2) verify the URL you are using is correct.http://helpcentral.componentone.com/CS/silverlight_161/f/78/p/86955/241328.aspx#241328
3) Check you are setting everything correctly: http://helpcentral.componentone.com/CS/silverlight_161/f/78/p/81924/227790.aspx#227790
Hope this helps!

COMException in System.WIndows.Forms.dll with Interactive Brokers TWS API

I've been trying to run the sample project provided with the tutorial at stockbotprogramming http://www.stockbotprogramming.com/sharpcibtutorial1.php , but I keep getting a COMException every time I run the application.
I have the TWS Client running and the sample VB projects provided with the API are able to connect just fine, but when I try to use the C# sample provided by the tutorial then I get the following exception:
An unhandled exception of type
'System.Runtime.InteropServices.COMException'
occurred in System.Windows.Forms.dll
Th exception happens when I try to add the TWS ActiveX control:
namespace CSharpTutorial1
{
public partial class Form1 : Form
{
private AxTWSLib.AxTws tws;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
tws = new AxTWSLib.AxTws();
tws.BeginInit();
tws.Enabled = true;
tws.Location = new System.Drawing.Point(32, 664);
tws.Name = "tws";
Controls.Add(tws); // <-- EXCEPTION HERE!
tws.EndInit();
tws.connect("127.0.0.1", 7496, 0);
String msg = "Connected to TWS server version " + tws.serverVersion + "at " + tws.TwsConnectionTime;
MessageBox.Show(msg);
}
}
}
The original project was probably done with Visual Studio 2005, but I have Visual Studio 2008 and it automatically converted the project (I've been reading that there are some problems there). Does anybody know what could be causing this exception? Any ideas on how to fix it?
Important: Make sure you add the component to the IDE using the visual designer. First right click in the toolbox, click "choose items", click the COM tab, then check the TWS control. Now using the visual IDE drag this item to the surface of your form. It must be visible in your app. (Don't create it in the Form1_Load())
Do something like this... (don't use an IP Address of 127.0.0.1, leave it blank):
axTws1.connect("", 7496, 0);
axTws1.reqMktData(0, "AMD", "STK", "", 0, "", "", "SMART", "ISLAND", "USD", "", 0);
Last (if you're using 64-bit Windows) compile the project in 32-bit code. Go to the configuration manager and then create a new profile "x86" and set all the configurations to x86.

Categories