Common.Xamarin.ViewModel 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Common.Xamarin.ViewModel --version 1.1.0                
NuGet\Install-Package Common.Xamarin.ViewModel -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Common.Xamarin.ViewModel" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Common.Xamarin.ViewModel --version 1.1.0                
#r "nuget: Common.Xamarin.ViewModel, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Common.Xamarin.ViewModel as a Cake Addin
#addin nuget:?package=Common.Xamarin.ViewModel&version=1.1.0

// Install Common.Xamarin.ViewModel as a Cake Tool
#tool nuget:?package=Common.Xamarin.ViewModel&version=1.1.0                

Common.Xamarin.ViewModel

Common Xamarin ViewModel to Help with Property Relationships

Side note: I uploaded the package pointing to Common.Xamarin.ViewModels. It's a minor mistake, I'll fix it if there is another release, but seems foolish to push just for a cosmetic change.

Purpose

This library was created to eliminate some of the boilerplating that is needed to have a useful viewmodel in Xamarin (or WPF).

Basic Usage

Create a viewmodel that extends BaseViewModel to have access to the built in features of the library.

public class ExampleViewModel : Common.Xamarin.ViewModel {
	
	//Basic Examples
	public string Title { 
		get => GetValue<string>(); 
		set => SetValue(value); 
	}

	public int Count {
		get => GetValue<int>();
		set => SetValue(value);
	}
}

Extended Usage (Relationships)


using Common.Xamarin.ViewModel.Attributes;

//...

public class RelationshipViewModel : Common.Xamarin.ViewModel {
	//Basic Examples
	//This value affects IsValid
	public string Title { 
		get => GetValue<string>(); 
		set => SetValue(value); 
	}

	//This Value Affects both Message and IsValid
	[Affects(nameof(Message))]
	public int Count {
		get => GetValue<int>();
		set => SetValue(value);
	}

	[AffectedBy(nameof(Title), nameof(Count))]
	public bool IsValid => Count > 0 && !string.IsNullOrEmpty(Title);

	//This one could also be AffectedBy Count.
	public string Message => $"The total count is: {Count}";
}

Additional

Triggers on Change

Technically you can implement triggers in a variety of ways including subscribing to the INotifyPropertyChanged Event, but that can usually be a little messy. Instead you can implement Method Calls in either the getter or the setter of the properties to handle if you want to manipulate the old or new data.

Example:

	///...
	public string Title { 
		get {
			var value = GetValue<string>();
			//Takes the newly stored value.  This getter is accessed on each Notified Change 
			//so it would execute after a change
			CallMethodWithNewValue(value); 
			return value;
		}
		set {
          //Optional if you need to compare the values or something along those lines
		  var oldValue = GetValue<string>(); 
		  //Called everytime there is a change in the value of this particular property.
		  CallMethodWithBothValues(oldValue, value);
		  SetValue(value); 
		}
	}

Implementing Events

If you want to enable the INavigationAware, IResetOnNavigation events than in the Class that extends your shell you can modify it like so:


    // Autogenerated code for Xamarin Forms Shell Navigation
    public partial class AppShell : Xamarin.Forms.Shell
    {
        public AppShell()
        {
            InitializeComponent();
            Routing.RegisterRoute(nameof(ItemDetailPage), typeof(ItemDetailPage));
            Routing.RegisterRoute(nameof(NewItemPage), typeof(NewItemPage));
        }

        private async void OnMenuItemClicked(object sender, EventArgs e)
        {
            await Current.GoToAsync("//LoginPage");
        }

        // ADD THE FOLLOWING CODE
        #region Common.Xamarin.ViewModel Extensions to Shell
        private Page _lastPage;

        protected override void OnNavigating(ShellNavigatingEventArgs args)
        {
            _lastPage = CurrentPage;
            base.OnNavigating(args);
        }

        protected override void OnNavigated(ShellNavigatedEventArgs args)
        {
            if (_lastPage?.BindingContext is IResetOnNavigation iR) iR.Reset();
            if (_lastPage?.BindingContext is INavigationAware iNA) iNA.OnNavigatedFrom();
            base.OnNavigated(args);
            if (CurrentPage?.BindingContext is INavigationAware NA) NA.OnNavigatedTo();
        }

        internal Page CurrentPage => 
             (Current?.CurrentItem?.CurrentItem as IShellSectionController)?.PresentedPage;
        #end region
    }
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net5.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Common.Xamarin.ViewModel:

Package Downloads
Common.Xamarin.ViewModel.ShellAware

Xamarin.Forms.Shell aware navigation to eliminate boilerplate code, allowing more complex transfer of data between viewmodels in navigation. Framework independent viewmodels that simplify logic.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.2 2,375 5/17/2021
1.1.1 358 5/17/2021
1.1.0 431 11/19/2020
1.0.5-DEV 365 11/19/2020
1.0.4-DEV 311 11/18/2020
1.0.3 1,065 6/5/2018
1.0.2 933 2/18/2018
1.0.1 986 12/29/2017
1.0.0 1,147 12/17/2017

- Rewrite of Common.Xamarin.ViewModel since I lost the old code due to international move.
- Has some breaking changes due to the rewrite.  `Set<T>` has been replaced with `SetValue<T>`.
- Open Sourced the Code on Github.
- Added Documentation