FreshMvvm 是一款专为 Xamarin.Forms 设计的超轻量级 Mvvm 框架,旨在实现简单、灵活和易用。
If you're looking for .NET MAUI version of FreshMvvm, aka FreshMvvm.Maui then please visit: https://github.com/XAM-Consulting/FreshMvvm.Maui
FreshMvvm is a super light Mvvm Framework designed specifically for Xamarin.Forms. It's designed to be Easy, Simple and Flexible.
Note
Different to standard naming conventions, FreshMvvm uses Page and PageModel instead of View and ViewModel, this is inline with Xamarin.Forms using PagesNow we can use both the ViewModel naming conventions.
I (Michael Ridland) was part-way into a Xamarin Traditional application when Xamarin.Forms was released. I wanted to move the project onto Xamarin.Forms but on that project I was using MvvmCross. At that time MvvmCross had no support for Xamarin.Forms, so I had the option of 1) adapting MvvmCross, 2) finding an alternative or 3) rolling my own Mvvm. The best part about MvvmCross was it's two-way databinding to the native iOS/Android controls but since Xamarin.Forms already had the Databinding builtin, that wasn't useful and the size with MvvmCross was an overhead when I didn't require it. I also wasn't able to find an alternative that I could easily move to. So that I could keep it simple and flexible, I ended up rolling my own Mvvm.
It's grown up from this post on rolling your own Mvvm for Xamarin.Forms. I try hard to keep the simplicity of rolling your own Mvvm for Xamarin.Forms.
It was never a plan to create a framework but after presenting my Mvvm solution at a few events, I found many people wanted it and seemed to be really interested in it. Also considering I've been using this Framework in all my projects from the start of Xamarin.Forms I know that it works, so I created FreshMvvm and that's how it was born.
This Framework, while simple, is also powerful and uses a Convention over Configuration style.
Note
Different to standard naming conventions, FreshMvvm uses Page and PageModel instead of View and ViewModel, this is inline with Xamarin.Forms using PagesNow we can use both the ViewModel naming conventions.
The Primary form of Navigation in FreshMvvm is PageModel to PageModel, this essentially means our views have no idea of Navigation.
So to Navigate between PageModels use:
await CoreMethods.PushPageModel(); // Pushes navigation stack
await CoreMethods.PushPageModel(null, true); // Pushes a Modal
await CoreMethods.PushPageModel (pm => pm.Quote = "Quote 23") // Pushes Navigation Stack and initializes the property 'Quote' with value 'Quote 23'The engine for Navigation in FreshMvvm is done via a simple interface, with methods for Push and Pop. Essentially these methods can control the Navigation of the application in any way they like.
public interface IFreshNavigationService
{
Task PushPage(Page page, FreshBasePageModel model, bool modal = false);
Task PopPage(bool modal = false);
}Within the PushPage and PopPage you can do any type of navigation that you like, this can be anything from a simple navigation to a advanced nested navigation.
The Framework contains some built in Navigation containers for the different types of Navigation.
var page = FreshPageModelResolver.ResolvePageModel ();
var basicNavContainer = new FreshNavigationContainer (page);
MainPage = basicNavContainer;var masterDetailNav = new FreshMasterDetailNavigationContainer ();
masterDetailNav.Init ("Menu");
masterDetailNav.AddPage ("Contacts", null);
masterDetailNav.AddPage ("Pages", null);
MainPage = masterDetailNav;var tabbedNavigation = new FreshTabbedNavigationContainer ();
tabbedNavigation.AddTab ("Contacts", null);
tabbedNavigation.AddTab ("Pages", null);
MainPage = tabbedNavigation;It's possible to setup any type of Navigation by implementing IFreshNavigationService.There's a sample of this in Sample Application named CustomImplementedNav.cs.
So that you don't need to include your own IOC container, FreshMvvm comes with a IOC container built in. It's using TinyIOC underneith, but with different naming to avoid conflicts.
To Register services in the container use Register:
FreshIOC.Container.Register();To obtain a service use Resolve:
FreshIOC.Container.Resolve();*This is also what drives constructor injection.
We now support a fluent API for setting the object lifetime of object inside the IOC Container.
// By default we register concrete types as
// multi-instance, and interfaces as singletons
FreshIOC.Container.Register(); // Multi-instance
FreshIOC.Container.Register(); // Singleton
// Fluent API allows us to change that behaviour
FreshIOC.Container.Register().AsSingleton(); // Singleton
FreshIOC.Container.Register().AsMultiInstance(); // Multi-instanceAs you can see below the IFreshIOC interface methods return the IRegisterOptions interface.
public interface IFreshIOC
{
object Resolve(Type resolveType);
IRegisterOptions Register(RegisterType instance) where RegisterType : class;
IRegisterOptions Register(RegisterType instance, string name) where RegisterType : class;
ResolveType Resolve() where ResolveType : class;
ResolveType Resolve(string name) where ResolveType : class;
IRegisterOptions Register ()
where RegisterType : class
where RegisterImplementation : class, RegisterType;
}The interface that's returned from the register methods is IRegisterOptions.
public interface IRegisterOptions
{
IRegisterOptions AsSingleton();
IRegisterOptions AsMultiInstance();
IRegisterOptions WithWeakReference();
IRegisterOptions WithStrongReference();
IRegisterOptions UsingConstructor(Expression> constructor);
}When PageModels are pushed services that are in the IOC container can be pushed into the Constructor.
FreshIOC.Container.Register();…Each PageModel has a property called 'CoreMethods' which is automatically filled when a PageModel is pushed, it's the basic functions that most apps need like Alerts, Pushing, Poping etc.
public interface IPageModelCoreMethods
{
Task DisplayAlert (string title, string message, string cancel);
Task DisplayActionSheet (string title, string cancel, string destruction, params string[] buttons);
Task DisplayAlert (string title, string message, string accept, string cancel);
Task PushPageModel(object data, bool modal = false) where T : FreshBasePageModel;
Task PopPageModel(bool modal = false);
Task PopPageModel(object data, bool modal = false);
Task PushPageModel() where T : FreshBasePageModel;
}PageModel Init PropertyChanged
…It’s always been possible to do any type of navigation in FreshMvvm, with custom or advanced scenarios were done by implementing a custom navigation service. Even with this ability people found it a little hard to do advanced navigation scenarios in FreshMvvm. After I reviewed all the support questions that came in for FreshMvvm I found that the basic issue people had was they wanted to be able to use our built in navigation containers multiple times, two primary examples are 1) within a master detail having a navigation stack in a master and another in the detail 2) The ability to push modally with a new navigation container. In order to support both these scenarios I concluded that the FreshMvvm required the ability to have named NavigationServices so that we could support multiple NavigationService’s.
Below we’re running two navigation stacks, in a single MasterDetail.
……There's some cases in Xamarin.Forms you might want to run multiple navigation stacks. A good example of this is when you have a navigation stack for the authentication and a stack for the primary area of your application.
To begin with we can setup some names for our navigation containers.
public class NavigationContainerNames
{
public const string AuthenticationContainer = "AuthenticationContainer";
public const string MainContainer = "MainContainer";
}Then we can create our two navigation containers and assign to the MainPage.
var loginPage = FreshMvvm.FreshPageModelResolver.ResolvePageModel();
var loginContainer = new FreshNavigationContainer(loginPage, NavigationContainerNames.AuthenticationContainer);
var myPitchListViewContainer = new FreshTabbedNavigationContainer(NavigationContainerNames.MainContainer);
MainPage = loginContainer;The Navigation Container will use the name passed as argument to register in this method
public FreshTabbedNavigationContainer(string navigationServiceName)
{
NavigationServiceName = navigationServiceName;
RegisterNavigation ();
}
protected void RegisterNavigation ()
{
FreshIOC.Container.Register (this, NavigationServiceName);
}Once we've set this up we can now switch out our navigation containers.
CoreMethods.SwitchOutRootNavigation(NavigationContainerNames.MainContainer);That name will be resolved in this method to find the correct Navigation Container
public void SwitchOutRootNavigation (string navigationServiceName)
{
IFreshNavigationService rootNavigation = FreshIOC.Container.Resolve (navigationServiceName);
}The second major request for FreshMvvm 1.0 was to allow custom IOC containers. In the case that your application already has a container that you want to leverage.
Using a custom IOC container is very simple in that you only need to implement a single interface.
public interface IFreshIOC
{
object Resolve(Type resolveType);
void Register(RegisterType instance) where RegisterType : class;
void Register(RegisterType instance, string name) where RegisterType : class;
ResolveType Resolve() where ResolveType : class;
ResolveType Resolve(string name) where ResolveType : class;
void Register ()
where RegisterType : class
where Reg暂无开放 Issues,或尚未同步最近议题。