UIWebView throwing exception on LoadRequest - c#

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...

Related

Xamarin: Import one page from a different solution using MvvmCross

I've got a Xamarin app using MvvmCross and I would like to exclude one page to a different Solution.
Is that possible at all?
I get following error displayed:
System.Collections.Generic.KeyNotFoundException: 'Could not find view for TestApp.ValidationViewModel2'
Details:
I've got a main app where I would like to navigate to the external page "ValidationViewModel2".
new MvxAsyncCommand(async() => await NavigationService.Navigate<ValidationViewModel2>())
To reach the page in the new solution I added a reference to the 'TestApp' assembly.
The 'TestApp' solution contains a a single C# project containing a "ValidationPage2.xaml" and a "ValidationViewModel2.cs" file. The two files find each other and the project builds successfully.
I can also debug from my main page into the 'ValidationViewModel2' constructor; The error occurs afterwards.
I'm using only Android. My ValidationViewModel2.cs has also references to the main app.
Thanks for helping
If you study the documentation for MvvmCross a bit you will find the documentation about Customizing App and Setup.
This doc describes that if you want to put ViewModels or Views in different assemblies than the default ones MvvmCross expects, you need to tell MvvmCross about it.
This can be done by overriding your Setup.cs file per platform.
For Views:
protected override Assembly[] GetViewAssemblies()
{
var list = new List<Assembly>(base.GetViewAssemblies());
list.Add(typeof(SomeTypeFromAdditionalViewAssembly).Assembly);
return list;
}
For ViewModels:
protected override Assembly[] GetViewModelAssemblies()
{
var list = new List<Assembly>(base.GetViewModelAssemblies());
list.Add(typeof(SomeTypeFromAdditionalViewModelAssembly).Assembly);
return list;
}

WPF Cefsharp instance/object reference in class not working

I am spending too much time trying to solve this issue: i am trying to create a Core, because the project is already confusing me and it is not that big. The Core is a Class outside the main Thread. On the main Thread i named the Browser, browser :
<cefSharp:ChromiumWebBrowser x:Name="browser" x:FieldModifier="public" />
In the class i call it like this:
public class Core : MainWindow
{
public void winstenVerlies()
{
var currentdirectory = "https://www.google.nl";
this.browser.Address = currentdirectory;
}
}
I have tried numerous ways, this is the way i like it but they all give me the same message:
"object reference is not set on an instance of an object."
Any help is appreciated.
Problem solved. i researched it for weeks now, from what i can see is the problem is the root, when i use this or the object/function name when calling it, it does not get passed the root of the class, instead of the root of the project. here is how you bypass it.
pass the object trough the class.
public void winstenVerlies(ChromiumWebBrowser b)
{
var currentdirectory = "https://www.google.nl";
b.Address = currentdirectory;
}
then call it like so.
Core Menu = new Core();
Menu.winstenVerlies(browser);
Now i try'd this befor, it gave me error messages the last time, i did not trace the source of the problems.
All i know is it working now, and i am happy.
Have a nice day.

EditorWindow.GetWindow doesn't work

This is my code:
public class CrazyWindow: EditorWindow
{
[MenuItem("Window/CrazyWindow")]
public static void Window()
{
EditorWindow.GetWindow(typeof(CrazyWindow));
Debug.Log("It should have appeared!");
}
string test = "";
public void OnGUI()
{
test = EditorGUILayout.TextField ("Text Field", test );
}
}
I'm using Unity3D v. 4.3.4f1 (free version) on Windows 7.
I have no idea why this is happening, as I can see in tutorials in the internet, that's how it should be done. The script is also in the Editor folder.
I'm able to click on the option "CrazyWindow" in the window menu, and I also get the Debug message informing me that the window should be working, but nothing happens besides that. No window is created at all!
What might be the cause of my problem?
Problem solved.
As Bart mentioned, I was using a custom Editor Layout, which was the case for the window not showing.
I just switched to one of the factory editor layouts and: ta dah, the window was there...
Pretty buggy thought.
Try renaming the 'CrazyWindow' part in the MenuItem and of the class itself. Unity remembers whether a window is visible or not and somehow something goes wrong there. Probably it thinks your window is visible (in cache) while actually it is not.
As Bart said, it remembers useless things
Just make it remember what we want it to
private void OnLostFocus() {
GetWindow<CrazyWindow>().Close();
}

MessagePrompt crashing at first launch of application

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...

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!

Categories