Inherit from MvxAppCompatActivity - mvvmcross

I'm trying to create my own base class that inherit from MvxAppCompatActivity.
I can use the MvxAppCompatActivity, but using passing the ViewModel gives me error.
WORKS
public class MyBaseActivity : MvxAppCompatActivity
DOES NOT WORK
public class MyBaseActivity<T> : MvxAppCompatActivity<T> where T : class, IMvxViewModel
I keep receiving the error: An unhandled exception occured.
Do I am missing something?

Your code works without any issues on my side.
Anyway, if you don´t need to re-implement MvxViewModel stuff, just do this:
public class BaseActivity<T> : MvxAppCompatActivity<T> where T: MvxViewModel

Related

DotnetCore : OnException method in a custom filter is called two times

I am using dotnet core and have created a custom exception filter to handle exceptions. The problem i face is that in case of exceptions , the onException method in the custom filter is called two times. Below is the code :
public class CustomExceptionFilter : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext context)
{
// Code
base.OnException(context);
}
}
Controller Code is :
[CustomExceptionFilter]
public class MyController : Controller
{
// Raise an exception in any apis
}
Why onException is called two times?
Using Visual Studio I created a new Asp.Net Core Web Application and used the standard Web Application Template. Then I added the CustomExceptionFilter class and added the [CustomExceptionFilter] attribute to the HomeController and threw and exception inside it's Index method:
[CustomExceptionFilter]
public class HomeController : Controller
{
public IActionResult Index()
{
throw new Exception("Time to bail!");
return View();
}
}
Finaly, I set a breakpoint on this line in the CustomExceptionFilter:
base.OnException(context);
And ran the website. The debugger stops on the throw of course, and then it stops on the line with the breakpoint. The breakpoint is only hit once. So my setup verifies the expected behavior.
Troubleshooting
In the past, I have ran into similar situations where something that should only be called once is called twice and it's almost always turned out to be because a second http request came in that I wasn't expecting. So it really was only being called once per http request. One way to check on that is to look at the path and query in the method which you can do as indicated below:
public class CustomExceptionFilter : ExceptionFilterAttribute {
public override void OnException(ExceptionContext context) {
//set breakpoing on the following line to see what the requested path and query is
string pathAndQuery = context.HttpContext.Request.Path + context.HttpContext.Request.QueryString;
// Code
base.OnException(context);
}
}
Another scenario that can cause this is adding the attribute in two places:
I had this:
config.Filters.Add( new StandardExceptionHandlingAttribute() );
AND this:
[StandardExceptionHandling]
public async Task<int?> ....

Sonar blocker for managed "ame" + flex project

I'm trying to fix bocker given by sonar for a Flex+ actionScript3 web application.
I was faced to this unresolvable blocker:
The managed event "ame" is either misspelled or is missing a companion
Event metadata tag
My code is as follows:
[Bindable]
[ManagedEvents(names="message")]
public class ClassName extends EventDispatcher
{
.........
}
I tried to fix this issue as follows:
[Bindable]
[Event(name="message",type="package.ClassEvent")]
[ManagedEvents(names="message")]
public class ClassName extends EventDispatcher
{
....
}
Where in package.ClassEvent is the declared event "message"
[Command(selector="message")]
public function message(evt:NameEvent):AsyncToken
{
.....
}
PS: Sonar suggest as solution :
The "ManagedEvents" metadata tag allows you to flag an event as being
managed. By definition this "ManageEvents" metadata tag should be used
in pair with an "Event" metadata tag.
Noncompliant Code Example
[Event(name="message", type="my.package.MyEvemt")]
[ManagedEvents("mes")] //This "mes" event is not defined with
the "Event" metadata tag public class MyClass {...}
Compliant Solution
[Event(name="message", type="my.package.MyEvemt")]
[ManagedEvents("message")] public class MyClass {...}
This is a bug in Sonar.
This ticket https://jira.sonarsource.com/browse/SONARFLEX-88 should fix your problem. While it's not fixed you can mark issue as false positive.

Is it possible to bind hiding/showing a UIAlertController in Mvvmcross?

I have a command which interacts with an API. If the command doesn't return a desired result it sets a property of the ViewModel called Error.
I want to bind Error to a UIAlertController in my View and have it display when the error occurs.
Here's roughly what I have (although obviously the visibility converter isn't the way to go). I should add that I'm aware PresentViewController should be used to display the UIAlertController.
UIAlertController myAlert = UIAlertController.Create ("", Error.Text, UIAlertControllerStyle.Alert);
set.Bind(myAlert).For("Visibility").To((myViewModel vm) => vm.Error).WithConversion("Visibility");
Check out Observer design pattern.
The way I prefer to achieve that is simple:
Create class which inherits from MvxMessage - let say ShowAlertDialogMessage with properties like title, content and so on.
Create abstract MessageObserver where TMessage : MvxMessage class, ex.:
public interface IMessageObserver
{
void Subscribe(IMvxMessenger messenger);
void Unsubscribe();
}
public abstract class MessageObserver<TMessage> : IMessageObserver where TMessage : MvxMessage
{
public void Subscribe(IMvxMessenger messenger) {
messenger.SubscribeOnMainThread<TMessage>(OnMessageDelivered);
}
public abstract void OnMessageDelivered(TMessage message);
}
Create MessageObserverController
public class MessageObserverController {
public void SubscribeObserver(IMessageObserver msgObserver) {
msgObserver.Subscribe(messenger);
}
.. unsubscribe, dispose and so on goes here
}
Implement ShowAlertDialogMessageObserver class (inherit from MessageObserver<ShowAlertDialogMessage>() which shows UIAlertViewController with data from ShowAlertDialogMessage (title, content and so on). Pass root UIViewController as constructor if needed (you will register MessageObservers in your viewcontrollers anyway - so that's not a problem).
Use MessageObserverController in your ViewControllers (preferably create base view controller to simplify things).
Voilà - you get reusable UI logic, which you can raise by publishing message in your PCL ViewModel (without creating any platform-specific coupling!).

How to turn a Ceylon class which extends a Java class into the Java class

Suppose you have a Java method in a legacy library you need to use that takes a Class as an argument:
public void takeClass(Class<? extends JavaClass> cls);
Now, suppose you have a Ceylon class which extends JavaClass:
shared class CeylonClass() extends JavaClass() {}
Now, how can I use the method takeClass in Ceylon such that this works?
javaThing.takeClass( `class CeylonClass` );
// or maybe this should work?
javaThing.takeClass( javaClass<CeylonClass>() );
As shown above, I've been trying function javaClass in module ceylon.interop.java without success... if I do javaClass<JavaClass>() then it works, but this is no use for me, of course.
EDIT:
The error I get when using javaClass<CeylonClass>() as shown above:
argument must be assignable to parameter class of takeClass:
Class<CeylonClass> is not assignable to Class<JavaClass>?
Unfortunately, this is a case where you need to drop back to Java to add some glue. You can't, today, write it completely in Ceylon.
Explanation
The problem is that since Ceylon doesn't have use-site covariance, and since the Ceylon typechecker doesn't even understand Java's use-site covariance, the typechecker treats this method:
public void takeClass(Class<? extends JavaClass> cls);
As if it had this more restrictive signature:
public void takeClass(Class<JavaClass> cls);
Furthermore, the typechecker treats all Java classes as if they were invariant types.
Therefore, since javaClass<CeylonClass>() produces a Class<CeylonClass>, it's not considered assignable to the parameter of takeClass(). :-(
Workaround
The workaround is to add the following Java method:
public static <T extends JavaClass> void takeClass2(Class<T> cls) {
takeClass(cls);
}
Now this method can be called like this from Ceylon:
javaThing.takeClass2( javaClass<CeylonClass>() );
HTH
P.S.
While writing this up, I noticed that in fact java.lang.Class is actually a covariant type, and I think that Ceylon should easily be able to notice that too. So I created this issue:
https://github.com/ceylon/ceylon-compiler/issues/1474

JAXB Unmarshal Exception

I generated some classes off of an xsd that I made from a web service response that I am calling.
I'm getting an Exception when I run a JUnit test class that reads in an InputStream from the web service call.
I'm stuck as to what the exception means, so I'm looking for some ideas on things to check:
javax.xml.bind.UnmarshalException: Unexpected element {http://bar.foo.com/bbs}:rule
I have a class in my generated classes at:
com.foo.bar.bbs.Rule
Does the Exception mean I do not have the Rule class in the proper package?
The following are some things to check:
#XmlRootElement
Check that the Rule class is annotated with #XmlRootElement:
#XmlRootElement
public class Rule {
}
#XmlElementDecl
Or that there is a corresponding #XmlElementDecl in the ObjectFactory class:
#XmlElementDecl(namespace = "http://bar.foo.com/bbs", name = "root")
public JAXBElement<Root> createCustomer(Root value) {
return new JAXBElement<BigInteger>(_ROOT_QNAME, Root.class, null, value);
}
#XmlSchema
You will also need to ensure that the namespace information is specified correctly. A package-info class was probably generated something like the following for you. Ensure the correct namespace is specified.
#XmlSchema(
namespace = "http://bar.foo.com/bbs",
elementFormDefault = XmlNsForm.QUALIFIED)
package com.foo.bar.bbs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Alternatively you could include the namespace in the #XmlRootElement annotation:
#XmlRootElement(namespace="http://bar.foo.com/bbs")
public class Rule {
}
For more information see:
http://bdoughan.blogspot.com/2010/08/jaxb-namespaces.html
Make sure you are unmarshalling the correct type of object.