I have created a custom Class Library(named it as MiEngine), in that I have created an Application class(MiEngineApp.xaml and MiEngineApp.xaml.cs). In my project(application), I have integrated the Class Library and my project's App class extends the Class Library's Application class(ie. public partial class App:MiEngineApp). I have made changes in the App.xaml also using the z name space.
I want to implement the Application life cycle methods only in the Class Library's Application class not in the project's Appication class. But If I do not implement the Application Life cycle methods in the project's Application class, at run time XamlParseException is thrown in the MiEngineApp.g.i.cs file's InitializeComponent method. I have no clue why this happens and how to implement the life cycle methods only in the Class library's Application class. Please give me some idea!
You could put the logic for the lifecycle events in the library and then call them from the event handlers in the (each?) app. Yes this means adding 4 lines of code to the app but this is the best solution currently available.
As per the default comments in app.xaml, handlers for the Launching, Activated, Deactivated and Closing events of the PhoneApplicationService are REQUIRED.
Related
I'm trying to implement integration testing in my app and have test class like that:
#ExtendWith(value={MyDockerExtension.class})
#ExtendWith(value={SpringExtension.class})
#WebAppConfiguration
#ContextConfiguration(classes={...})
#TestInstance(TestInstance.LifeCycle.PER_CLASS)
public class TestClass{ ... }
Is there any way to make MyDockerExtension execute some code, before whole SpringExtension start working and generate whole Context with Configurationc classes?
I've heard that order in which we declare extensions is the key, but sadly MyDockerExtension that implements BeforeAllCallback, AfterAllCallback executes right before test method and after whole context is loaded. In that situation it's to late to start containers with docker, becuase since whole context is loaded my app already tried to connect to the container.
At first I was skeptical about the order being fixed but you're correct:
Extensions registered declaratively via #ExtendWith will be executed in the order in which they are declared in the source code.
Regarding the MyDockerExtension, you may want to look at the extension point TestInstancePostProcessor, which is called before #BeforeAll. SpringExtension implements it and I guess it's there where it sets up the application context. If you also implement it, you should be able to act before it does.
In Xamarin forms app, How can we invoke Shared code Method from Platform specific Dependency class.
I need to call one method implemented in my ContentPage class from my iOS dependency class.
Thanks...
There are different solutions to this:
Use a static method to call it where ever you need to.
Use the messaging center to send a message to your Shared/PCL project and do what ever you need. (link: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/messaging-center/)
If this is a Custom renderer you can use binded command properties and Execute those in your platforms specific code.
In my case, what I did was having a static class called Helper, that contains all the static methods that I need to call on all platforms/projects.
Hope this helps.
I am not sure how to use Dependency Injection on Xamarin Android project solution. Currently my Android solution holds a reference to another class library solution. I have used Unity on my service layer and registered the container via WebApiConfig.cs.
My question is, how do i go about using Unity on Android side in order to run on start up, would be grateful if code was included. I dont want to new-up the container through main activity of Android. I want the container to register behind the process i.e. AppStart or Global asax where it does it for you for MVC apps. Is there a way to do it for Android? Also I noticed on Main Activity I am unable to create constructor. I guess this isnt possible but how do I go about holding object reference to my Class Library solution ? example that i attempted to do:
private IExample _ex;
MainActivity(IExample ex){
_ex = ex; //depedency Injection rather than newing it up
}
public void DoSomething(){
_ex.HelloWorld();
}
Is there a way to do it via Attribute ? Also for each of my layer do I need to install and create container in order to resolve current solution dependency ? or can I use container from android which would resolve all dependency in each layer as DDD architecture goes from outer to inner ?
In terms of setting up DI at startup you can create a custom Application implementation like so:
// Must include this attribute so that Android knows we want to use this as our Application implementation
[Application(Icon = "#drawable/Icon", Label = "#string/ApplicationName")]
public class MyApplication : Application
{
public override void OnCreate()
{
base.OnCreate();
// Do your DI initialization/registration here
}
}
I'm not sure exactly what you mean by not being able to create a constructor on the main activity. You can create constructors for any activity you feel like. You don't always see it though because people tend to put their initialization logic in OnCreate.
hi i am having a different projects in my solution in the initial project (default project) i am accessing the global reference to App.xaml.cs in this way :-
App objref = (App)Application.Current;
But now i have added new project to my solution and trying to access the app.xaml.cs in the same way as defined earlier but i am not able to access app.xaml.cs ?
1)can i know the reason
2)What should i do if i want to use it in both the projects ?
Please let me know
Thanks in advance.
You can access it, but the new project will not be familiar with the derived App class that is in your project. To explain further we need to take inheritance into consideration.
There's a generic definition for the Application class that exposes a number of predefined methods. Your App.xaml.cs is a new class definition that is derived from the Application class. It has the methods it inherited plus what ever methods and properties that you've added. To make use of these any code that is seeking to use your extra properties or methods must have access to the class definition. Your classes in the other projects that you've added do not have access to this definition.
You'll need to make a class or interface definition that both projects have access to. There are several ways of organizing this. I'll present one.
Create your main project in the solution. This contains your
App.xaml.cs.
Create your class library project that contains the
other code.
Create a third project called Common that only contains
an Interface definition.
On the Interface definition define all of the methods/properties
that you want both your class library and main project to have
access too.
Have App.Xaml.cs implement this interface.
In the Class Library access var appReference =
(IMyInterfaceName)Applcation.Current. You'll have access to the
methods that were defined in the interface
I have a Windows Phone app with Ninject IOC.
At some point I realized that my MainPage.xaml.cs (initial page of the application that gets initialized first) need to have a constructor with parameters.
I have added params to a constructor like this:
public partial class MainPage : PhoneApplicationPage
{
private readonly Settings _settings;
// Constructor
public MainPage(Settings _settings)
In my Ninject modules I have the binder for Settings type:
this.Bind<Settings>().ToSelf().InSingletonScope();
However, whenever I am to run an app, I get a MissingMethodException at startup.
I have worked around this problem by retaining a parameterless constructor in my MainPage(), and I use a service locator pattern with Ninject to get the Settings instance.
I want to know if there is a way for me to still have my app service locator free?
I'm afraid this isn't possible with the way the Silverlight navigation works. You have to have the parameterless constructor. Typically you would use the service locator to resolve your ViewModel which is where you need your dependencies injected, rather than your view.
As a side note don't fall into the trap of thinking that you should be developing your mobile apps the same way as you do desktop apps. The same rules don't automatically apply. IOC is an Enterprise Design Pattern, that aims to reduce the complexity of large apps with many developers developed over long periods. Phone apps are typically small apps with few developers developed over short periods - so its not necessarily true to that you have to rigidly follow the design pattern to the letter or even at all.