using App.xaml.cs Reference in another project present in same solution? - windows-phone-7

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

Related

Using .Net Core Identity with generic repository

So I'm trying to get my head around this for a while now, but I don't seem to succeed. In my application I'm using a generic repository with Entity Framework Core.
Hence my Repository always expect that it's accessed from a class who's BaseEntity or has inherited from that certain class.
Now I want to implement .Net Core Identity with it. But My User class is inheriting from BaseEntity. But I'd also need it to inherit from Identity in order to make it work I guess. How am I able to still use Identity?
C# only supports single inheritance. You cannot inherit from two different classes. Additionally your Identity user class, must inherit from IdentityUser. You have no choice in that. As a result, the best you can do is make your user class and the rest of your entity classes implement the same interface, i.e. IEntity. Then, instead of constraining your generic type as BaseEntity, use IEntity instead.
Of course, this means you will incur a bit of code duplication as you'll have to implement IEntity separately on both BaseEntity and your user class. However, that is unavoidable.

Xamarin, How to invoke Shared code Method from Platform specific Dependency class

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.

How to add new DLL locations to WebAPI's controller discovery?

ASP.NET WebAPI has a much appreciated ability to discover ApiController classes in external DLLs even if those DLLs are not referenced. For example, I may have MyWebApiProject that has a set of ApiControllers. I could then create a completely separate project called MyApiProjectPlugin that contains ApiController classes also. I have been able to add the MyApiProjectPlugin.dll file to the bin folder with the first MyApiProject.dll and the original project will discover all the controllers in the plugin project. I really like that ability.
However, What I would like to do is be able to add the plugin project to a sub directory inside of the bin folder. Something like bin/plugins. When I tried this, the original MyApiProject was unable to discover the plugin's controllers.
Is there a simple way to get WebAPI to look for ApiController classes in the bin's subdirectories? If I can avoid rewriting a controller factory from scratch I would like to.
You can write an assembly resolver.
public class PluginsResolver : DefaultAssembliesResolver
{
public override ICollection<Assembly> GetAssemblies()
{
List<Assembly> assemblies = new List<Assembly>(base.GetAssemblies());
assemblies.Add(Assembly.LoadFrom(#"<Path>\MyApiProjectPlugin.dll"));
return assemblies;
}
}
In the Register method in WebApiConfig, register the resolver.
config.Services.Replace(typeof(IAssembliesResolver), new PluginsResolver());

How do I access HttpContext.Current.Session in a class library?

So, I am creating a class library that handles user information like username, password, etc. I want to do this so that I can just reference this library with any of my web apps and not have to continuously rewrite the user information part.
In, the user information class library, I want to handle the login. I've done this before in app_code that was a part of the web project by using HttpContext.Current.Session. But, when I try to use it in my class library (even while using System.Web) it throws a compile error saying that HttpContext does not exist in this context. How can I get access to it?
When creating a utility type class that works with a dependency like HttpContext, your best bet is to pass the context or the session into the class either via a constructor or the method call. That way, it is explicit to the consumers of your class that it requires this object to function. This also allows you to test your class in isolation.
Even better, if you are working with a few specific properties that have basic types then you can accept those properties as inputs. That way, you are not creating any dependencies on a UI framework for your utility library.

WP7 Application Life Time Funcitons

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.

Resources